ClientIp.php in User restrictions 8
File
src/Plugin/UserRestrictionType/ClientIp.php
View source
<?php
namespace Drupal\user_restrictions\Plugin\UserRestrictionType;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class ClientIp extends UserRestrictionTypeBase {
protected $requestStack;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_manager, RequestStack $request_stack, LoggerInterface $logger) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_manager, $logger);
$this->requestStack = $request_stack;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('request_stack'), $container
->get('logger.channel.user_restrictions'));
}
public function matches(array $data) {
$client_ip = $this->requestStack
->getCurrentRequest()
->getClientIp();
$restriction = parent::matchesValue($client_ip);
if ($restriction) {
$this->logger
->notice('Restricted client IP %client_ip matching %restriction has been blocked.', [
'%client_ip' => $client_ip,
'%restriction' => $restriction
->toLink($restriction
->label(), 'edit-form'),
]);
}
return $restriction;
}
public function getErrorMessage() {
return $this
->t('Accessing the site from the IP %value is not allowed.', [
'%value' => $this->requestStack
->getCurrentRequest()
->getClientIp(),
]);
}
}
Classes
Name |
Description |
ClientIp |
Defines a restriction type by client IP. |