abstract class RateLimit in RESTful 7.2
Same name in this branch
- 7.2 src/Annotation/RateLimit.php \Drupal\restful\Annotation\RateLimit
- 7.2 src/RateLimit/Entity/RateLimit.php \Drupal\restful\RateLimit\Entity\RateLimit
- 7.2 src/Plugin/rate_limit/RateLimit.php \Drupal\restful\Plugin\rate_limit\RateLimit
Hierarchy
- class \Drupal\restful\Plugin\rate_limit\RateLimit extends \Drupal\Component\Plugin\PluginBase implements RateLimitInterface
Expanded class hierarchy of RateLimit
1 file declares its use of RateLimit
- RateLimitManager.php in src/
RateLimit/ RateLimitManager.php - Contains \Drupal\restful\RateLimit\RateLimitManager
File
- src/
Plugin/ rate_limit/ RateLimit.php, line 14 - Contains \Drupal\restful\Plugin\rate_limit\RateLimit
Namespace
Drupal\restful\Plugin\rate_limitView source
abstract class RateLimit extends PluginBase implements RateLimitInterface {
/**
* @var array
*
* Array of maximum limit of requests across all endpoints per role.
*/
protected $limits = array();
/**
* @var \DateInterval
*
* Period after which the rate limit is expired.
*/
protected $period;
/**
* @var \RestfulBase
*
* The resource this object is limiting access to.
*/
protected $resource;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
try {
$this->period = new \DateInterval($configuration['period']);
} catch (\Exception $e) {
throw new ServerConfigurationException(sprintf('Invalid rate limit period: %s. Should be a valid format of \\DateInterval.', $configuration['period']));
}
$this->limits = $configuration['limits'];
$this->resource = $configuration['resource'];
}
/**
* {@inheritdoc}
*/
public function setLimit($limits) {
$this->limits = $limits;
}
/**
* {@inheritdoc}
*/
public function getLimit($account = NULL) {
// If the user is anonymous.
if (empty($account->roles)) {
return $this->limits['anonymous user'];
}
// If the user is logged then return the best limit for all the roles the
// user has.
$max_limit = 0;
foreach ($account->roles as $rid => $role) {
if (!isset($this->limits[$role])) {
// No limit configured for this role.
continue;
}
if ($this->limits[$role] < $max_limit && $this->limits[$role] != RateLimitManager::UNLIMITED_RATE_LIMIT) {
// The limit is smaller than one previously found.
continue;
}
// This is the highest limit for the current user given all their roles.
$max_limit = $this->limits[$role];
}
return $max_limit;
}
/**
* {@inheritdoc}
*/
public function setPeriod(\DateInterval $period) {
$this->period = $period;
}
/**
* {@inheritdoc}
*/
public function getPeriod() {
return $this->period;
}
/**
* {@inheritdoc}
*/
public function generateIdentifier($account = NULL) {
$identifier = $this->resource
->getResourceName() . PluginBase::DERIVATIVE_SEPARATOR;
if ($this
->getPluginId() == 'global') {
// Don't split the id by resource if the event is global.
$identifier = '';
}
$identifier .= $this
->getPluginId() . PluginBase::DERIVATIVE_SEPARATOR;
$identifier .= empty($account->uid) ? ip_address() : $account->uid;
return $identifier;
}
/**
* {@inheritdoc}
*/
public function loadRateLimitEntity($account = NULL) {
$query = new \EntityFieldQuery();
$results = $query
->entityCondition('entity_type', 'rate_limit')
->entityCondition('bundle', $this
->getPluginId())
->propertyCondition('identifier', $this
->generateIdentifier($account))
->execute();
if (empty($results['rate_limit'])) {
return NULL;
}
$rlid = key($results['rate_limit']);
return entity_load_single('rate_limit', $rlid);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RateLimit:: |
protected | property | Array of maximum limit of requests across all endpoints per role. | |
RateLimit:: |
protected | property | Period after which the rate limit is expired. | |
RateLimit:: |
protected | property | The resource this object is limiting access to. | |
RateLimit:: |
public | function |
Generates an identifier for the event and the request. Overrides RateLimitInterface:: |
1 |
RateLimit:: |
public | function |
Get the rate limit. Returns the highest rate limit for the current account. Overrides RateLimitInterface:: |
1 |
RateLimit:: |
public | function |
Get the rate limit period. Overrides RateLimitInterface:: |
|
RateLimit:: |
public | function |
Load rate limit entity. Overrides RateLimitInterface:: |
|
RateLimit:: |
public | function |
Set the rate limit. Overrides RateLimitInterface:: |
|
RateLimit:: |
public | function |
Set the rate limit period. Overrides RateLimitInterface:: |
|
RateLimit:: |
public | function | 1 | |
RateLimitInterface:: |
public | function | Checks if the current request meets the event for the implementing class. | 2 |