You are here

public function UserFloodControl::isAllowed in Drupal 9

Checks whether a user is allowed to proceed with the specified event.

Events can have thresholds saying that each user can only do that event a certain number of times in a time window. This function verifies that the current user has not exceeded this threshold.

Parameters

string $name: The name of an event.

int $threshold: The maximum number of times each user can do this event per time window.

int $window: (optional) Number of seconds in the time window for this event (default is 3600 seconds, or 1 hour).

string $identifier: (optional) Unique identifier of the current user. Defaults to the current user's IP address).

Return value

TRUE if the user is allowed to proceed. FALSE if they have exceeded the threshold and should not be allowed to proceed.

Overrides FloodInterface::isAllowed

File

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

Class

UserFloodControl
User Flood Control service.

Namespace

Drupal\user

Code

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;
}