class DatabaseBackend implements FloodInterface (View source)

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

Constants

TABLE_NAME

The database table name.

Properties

protected Connection $connection

The database connection used to store flood event information.

protected RequestStack $requestStack

The request stack.

Methods

__construct(Connection $connection, RequestStack $request_stack)

Construct the DatabaseBackend.

register(string $name, int $window = 3600, string $identifier = NULL)

Registers an event for the current visitor to the flood control mechanism.

doInsert(string $name, int $window, string $identifier)

Inserts an event into the flood table.

clear(string $name, string $identifier = NULL)

Makes the flood control mechanism forget an event for the current visitor.

true
isAllowed(string $name, int $threshold, int $window = 3600, string $identifier = NULL)

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

garbageCollection()

Cleans up expired flood events. This method is called automatically on cron run.

ensureTableExists()

Check if the flood table exists and create it if not.

catchException(Exception $e)

Act on an exception when flood might be stale.

schemaDefinition()

Defines the schema for the flood table.

Details

__construct(Connection $connection, RequestStack $request_stack)

Construct the DatabaseBackend.

Parameters

Connection $connection

The database connection which will be used to store the flood event information.

RequestStack $request_stack

The request stack used to retrieve the current request.

register(string $name, int $window = 3600, string $identifier = NULL)

Registers an event for the current visitor to the flood control mechanism.

Parameters

string $name

The name of an event. To prevent unintended name clashes, it is recommended to use the module name first in the event name, optionally followed by a dot and the actual event name (e.g. "mymodule.my_event").

int $window

(optional) Number of seconds before this event expires. Defaults to 3600 (1 hour). Typically uses the same value as the isAllowed() $window parameter. Expired events are purged on cron run to prevent the flood table from growing indefinitely.

string $identifier

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

protected doInsert(string $name, int $window, string $identifier)

Inserts an event into the flood table.

Parameters

string $name

The name of an event.

int $window

Number of seconds before this event expires.

string $identifier

Unique identifier of the current user.

See also

DatabaseBackend::register

clear(string $name, string $identifier = NULL)

Makes the flood control mechanism forget an event for the current visitor.

Parameters

string $name

The name of an event.

string $identifier

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

true isAllowed(string $name, int $threshold, int $window = 3600, string $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.

garbageCollection()

Cleans up expired flood events. This method is called automatically on cron run.

protected ensureTableExists()

Check if the flood table exists and create it if not.

protected catchException(Exception $e)

Act on an exception when flood might be stale.

If the table does not yet exist, that's fine, but if the table exists and yet the query failed, then the flood is stale and the exception needs to propagate.

Parameters

Exception $e

The exception.

Exceptions

Exception

schemaDefinition()

internal  
 

Defines the schema for the flood table.