UserFloodControl.php in Drupal 10
File
core/modules/user/src/UserFloodControl.php
View source
<?php
namespace Drupal\user;
use Drupal\user\Event\UserEvents;
use Drupal\user\Event\UserFloodEvent;
use Drupal\Core\Flood\FloodInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class UserFloodControl implements UserFloodControlInterface {
protected $flood;
protected $eventDispatcher;
protected $requestStack;
public function __construct(FloodInterface $flood, EventDispatcherInterface $event_dispatcher, RequestStack $request_stack) {
$this->flood = $flood;
$this->eventDispatcher = $event_dispatcher;
$this->requestStack = $request_stack;
}
public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
if ($this->flood
->isAllowed($name, $threshold, $window, $identifier)) {
return TRUE;
}
$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;
}
public function register($name, $window = 3600, $identifier = NULL) {
return $this->flood
->register($name, $window, $identifier);
}
public function clear($name, $identifier = NULL) {
return $this->flood
->clear($name, $identifier);
}
public function garbageCollection() {
return $this->flood
->garbageCollection();
}
}