You are here

class UserFloodSubscriber in Drupal 9

Same name and namespace in other branches
  1. 10 core/modules/user/src/EventSubscriber/UserFloodSubscriber.php \Drupal\user\EventSubscriber\UserFloodSubscriber

Logs details of User Flood Control events.

Hierarchy

  • class \Drupal\user\EventSubscriber\UserFloodSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of UserFloodSubscriber

1 string reference to 'UserFloodSubscriber'
user.services.yml in core/modules/user/user.services.yml
core/modules/user/user.services.yml
1 service uses UserFloodSubscriber
user.flood_subscriber in core/modules/user/user.services.yml
Drupal\user\EventSubscriber\UserFloodSubscriber

File

core/modules/user/src/EventSubscriber/UserFloodSubscriber.php, line 14

Namespace

Drupal\user\EventSubscriber
View source
class UserFloodSubscriber implements EventSubscriberInterface {

  /**
   * The default logger service.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * Constructs a UserFloodSubscriber.
   *
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   */
  public function __construct(LoggerInterface $logger = NULL) {
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[UserEvents::FLOOD_BLOCKED_USER][] = [
      'blockedUser',
    ];
    $events[UserEvents::FLOOD_BLOCKED_IP][] = [
      'blockedIp',
    ];
    return $events;
  }

  /**
   * An attempt to login has been blocked based on user name.
   *
   * @param \Drupal\user\Event\UserFloodEvent $floodEvent
   *   The flood event.
   */
  public function blockedUser(UserFloodEvent $floodEvent) {
    if (Settings::get('log_user_flood', TRUE)) {
      $uid = $floodEvent
        ->getUid();
      if ($floodEvent
        ->hasIp()) {
        $ip = $floodEvent
          ->getIp();
        $this->logger
          ->notice('Flood control blocked login attempt for uid %uid from %ip', [
          '%uid' => $uid,
          '%ip' => $ip,
        ]);
        return;
      }
      $this->logger
        ->notice('Flood control blocked login attempt for uid %uid', [
        '%uid' => $uid,
      ]);
    }
  }

  /**
   * An attempt to login has been blocked based on IP.
   *
   * @param \Drupal\user\Event\UserFloodEvent $floodEvent
   *   The flood event.
   */
  public function blockedIp(UserFloodEvent $floodEvent) {
    if (Settings::get('log_user_flood', TRUE)) {
      $this->logger
        ->notice('Flood control blocked login attempt from %ip', [
        '%ip' => $floodEvent
          ->getIp(),
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UserFloodSubscriber::$logger protected property The default logger service.
UserFloodSubscriber::blockedIp public function An attempt to login has been blocked based on IP.
UserFloodSubscriber::blockedUser public function An attempt to login has been blocked based on user name.
UserFloodSubscriber::getSubscribedEvents public static function
UserFloodSubscriber::__construct public function Constructs a UserFloodSubscriber.