W3cubDocs

/Drupal 8

public function DatabaseBackend::isAllowed

public DatabaseBackend::isAllowed($name, $threshold, $window = 3600, $identifier = NULL)

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/lib/Drupal/Core/Flood/DatabaseBackend.php, line 113

Class

DatabaseBackend
Defines the database flood backend. This is the default Drupal backend.

Namespace

Drupal\Core\Flood

Code

public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
  if (!isset($identifier)) {
    $identifier = $this->requestStack->getCurrentRequest()->getClientIp();
  }
  try {
    $number = $this->connection->select(static::TABLE_NAME, 'f')
      ->condition('event', $name)
      ->condition('identifier', $identifier)
      ->condition('timestamp', REQUEST_TIME - $window, '>')
      ->countQuery()
      ->execute()
      ->fetchField();
    return ($number < $threshold);
  }
  catch (\Exception $e) {
    $this->catchException($e);
    return TRUE;
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Flood!DatabaseBackend.php/function/DatabaseBackend::isAllowed/8.1.x