You are here

class RateLimitGlobal in RESTful 7.2

Class RateLimitGlobal @package Drupal\restful\Plugin\rate_limit

Plugin annotation


@RateLimit(
  id = "global",
  label = "Global limitation",
  description = "This keeps a count across all the handlers.",
)

Hierarchy

Expanded class hierarchy of RateLimitGlobal

File

src/Plugin/rate_limit/RateLimitGlobal.php, line 22
Contains \Drupal\restful\Plugin\rate_limit\RateLimitGlobal

Namespace

Drupal\restful\Plugin\rate_limit
View source
class RateLimitGlobal extends RateLimit {

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $limit = variable_get('restful_global_rate_limit', 0);
    foreach (user_roles() as $rid => $role_info) {
      $this->limits[$rid] = $limit;
    }
    $this->period = new \DateInterval(variable_get('restful_global_rate_period', 'P1D'));
  }

  /**
   * {@inheritdoc}
   */
  public function generateIdentifier($account = NULL) {
    $identifier = '';
    $identifier .= $this
      ->getPluginId() . PluginBase::DERIVATIVE_SEPARATOR;
    $identifier .= empty($account->uid) ? ip_address() : $account->uid;
    return $identifier;
  }

  /**
   * {@inheritdoc}
   *
   * All limits are the same for the global limit. Return the first one.
   */
  public function getLimit($account = NULL) {
    return reset($this->limits);
  }

  /**
   * {@inheritdoc}
   *
   * Only track the global limit for the current user if the variable is on.
   */
  public function isRequestedEvent(RequestInterface $request) {
    return $this
      ->getLimit() > 0;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RateLimit::$limits protected property Array of maximum limit of requests across all endpoints per role.
RateLimit::$period protected property Period after which the rate limit is expired.
RateLimit::$resource protected property The resource this object is limiting access to.
RateLimit::getPeriod public function Get the rate limit period. Overrides RateLimitInterface::getPeriod
RateLimit::loadRateLimitEntity public function Load rate limit entity. Overrides RateLimitInterface::loadRateLimitEntity
RateLimit::setLimit public function Set the rate limit. Overrides RateLimitInterface::setLimit
RateLimit::setPeriod public function Set the rate limit period. Overrides RateLimitInterface::setPeriod
RateLimitGlobal::generateIdentifier public function Generates an identifier for the event and the request. Overrides RateLimit::generateIdentifier
RateLimitGlobal::getLimit public function All limits are the same for the global limit. Return the first one. Overrides RateLimit::getLimit
RateLimitGlobal::isRequestedEvent public function Only track the global limit for the current user if the variable is on. Overrides RateLimitInterface::isRequestedEvent
RateLimitGlobal::__construct public function Overrides RateLimit::__construct