class UserFloodControl

Same name and namespace in other branches
  1. 10 core/modules/user/src/UserFloodControl.php \Drupal\user\UserFloodControl
  2. 11.x core/modules/user/src/UserFloodControl.php \Drupal\user\UserFloodControl

User Flood Control service.

@see: \Drupal\Core\Flood\DatabaseBackend

Hierarchy

Expanded class hierarchy of UserFloodControl

1 string reference to 'UserFloodControl'
user.services.yml in core/modules/user/user.services.yml
core/modules/user/user.services.yml
1 service uses UserFloodControl
user.flood_control in core/modules/user/user.services.yml
Drupal\user\UserFloodControl

File

core/modules/user/src/UserFloodControl.php, line 16

Namespace

Drupal\user
View source
class UserFloodControl implements UserFloodControlInterface {
    
    /**
     * The decorated flood service.
     *
     * @var \Drupal\Core\Flood\FloodInterface
     */
    protected $flood;
    
    /**
     * Event dispatcher.
     *
     * @var \Symfony\Contracts\EventDispatcher\EventDispatcherInterface
     */
    protected $eventDispatcher;
    
    /**
     * The request stack.
     *
     * @var \Symfony\Component\HttpFoundation\RequestStack
     */
    protected $requestStack;
    
    /**
     * Construct the UserFloodControl.
     *
     * @param \Drupal\Core\Flood\FloodInterface $flood
     *   The flood service.
     * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $event_dispatcher
     *   The event dispatcher service.
     * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
     *   The request stack used to retrieve the current request.
     */
    public function __construct(FloodInterface $flood, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) {
        $this->flood = $flood;
        $this->eventDispatcher = $event_dispatcher;
        $this->requestStack = $request_stack;
    }
    
    /**
     * {@inheritdoc}
     */
    public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
        if ($this->flood
            ->isAllowed($name, $threshold, $window, $identifier)) {
            return TRUE;
        }
        // Register flood control blocked login event.
        $event_map['user.failed_login_ip'] = UserEvents::FLOOD_BLOCKED_IP;
        $event_map['user.failed_login_user'] = UserEvents::FLOOD_BLOCKED_USER;
        $event_map['user.http_login'] = UserEvents::FLOOD_BLOCKED_USER;
        if (isset($event_map[$name])) {
            if (empty($identifier)) {
                $identifier = $this->requestStack
                    ->getCurrentRequest()
                    ->getClientIp();
            }
            $event = new UserFloodEvent($name, $threshold, $window, $identifier);
            $this->eventDispatcher
                ->dispatch($event, $event_map[$name]);
        }
        return FALSE;
    }
    
    /**
     * {@inheritdoc}
     */
    public function register($name, $window = 3600, $identifier = NULL) {
        return $this->flood
            ->register($name, $window, $identifier);
    }
    
    /**
     * {@inheritdoc}
     */
    public function clear($name, $identifier = NULL) {
        return $this->flood
            ->clear($name, $identifier);
    }
    
    /**
     * {@inheritdoc}
     */
    public function garbageCollection() {
        return $this->flood
            ->garbageCollection();
    }

}

Members

Title Sort descending Modifiers Object type Summary
UserFloodControl::$eventDispatcher protected property Event dispatcher.
UserFloodControl::$flood protected property The decorated flood service.
UserFloodControl::$requestStack protected property The request stack.
UserFloodControl::clear public function
UserFloodControl::garbageCollection public function
UserFloodControl::isAllowed public function
UserFloodControl::register public function
UserFloodControl::__construct public function Construct the UserFloodControl.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.