You are here

class DatabaseBackend in Zircon Profile 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Flood/DatabaseBackend.php \Drupal\Core\Flood\DatabaseBackend
  2. 8 core/lib/Drupal/Core/Cache/DatabaseBackend.php \Drupal\Core\Cache\DatabaseBackend
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Flood/DatabaseBackend.php \Drupal\Core\Flood\DatabaseBackend

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

Hierarchy

Expanded class hierarchy of DatabaseBackend

1 string reference to 'DatabaseBackend'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses DatabaseBackend
flood in core/core.services.yml
Drupal\Core\Flood\DatabaseBackend

File

core/lib/Drupal/Core/Flood/DatabaseBackend.php, line 16
Contains \Drupal\Core\Flood\DatabaseBackend.

Namespace

Drupal\Core\Flood
View source
class DatabaseBackend implements FloodInterface {

  /**
   * The database connection used to store flood event information.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Construct the DatabaseBackend.
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The database connection which will be used to store the flood event
   *   information.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack used to retrieve the current request.
   */
  public function __construct(Connection $connection, RequestStack $request_stack) {
    $this->connection = $connection;
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public function register($name, $window = 3600, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->requestStack
        ->getCurrentRequest()
        ->getClientIp();
    }
    $this->connection
      ->insert('flood')
      ->fields(array(
      'event' => $name,
      'identifier' => $identifier,
      'timestamp' => REQUEST_TIME,
      'expiration' => REQUEST_TIME + $window,
    ))
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function clear($name, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->requestStack
        ->getCurrentRequest()
        ->getClientIp();
    }
    $this->connection
      ->delete('flood')
      ->condition('event', $name)
      ->condition('identifier', $identifier)
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function isAllowed($name, $threshold, $window = 3600, $identifier = NULL) {
    if (!isset($identifier)) {
      $identifier = $this->requestStack
        ->getCurrentRequest()
        ->getClientIp();
    }
    $number = $this->connection
      ->select('flood', 'f')
      ->condition('event', $name)
      ->condition('identifier', $identifier)
      ->condition('timestamp', REQUEST_TIME - $window, '>')
      ->countQuery()
      ->execute()
      ->fetchField();
    return $number < $threshold;
  }

  /**
   * {@inheritdoc}
   */
  public function garbageCollection() {
    return $this->connection
      ->delete('flood')
      ->condition('expiration', REQUEST_TIME, '<')
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DatabaseBackend::$connection protected property The database connection used to store flood event information.
DatabaseBackend::$requestStack protected property The request stack.
DatabaseBackend::clear public function Makes the flood control mechanism forget an event for the current visitor. Overrides FloodInterface::clear
DatabaseBackend::garbageCollection public function Cleans up expired flood events. This method is called automatically on cron run. Overrides FloodInterface::garbageCollection
DatabaseBackend::isAllowed public function Checks whether a user is allowed to proceed with the specified event. Overrides FloodInterface::isAllowed
DatabaseBackend::register public function Registers an event for the current visitor to the flood control mechanism. Overrides FloodInterface::register
DatabaseBackend::__construct public function Construct the DatabaseBackend.