class IpRule in Access Filter 8
Filter rule using IP address.
Plugin annotation
@AccessFilterRule(
id = "ip",
description = @Translation("IP address."),
examples = {
"- { type: ip, action: deny, address: '*' }",
"- { type: ip, action: deny, address: 192.168.1.100 }",
"- { type: ip, action: deny, address: 192.168.1.0/24 }",
"- { type: ip, action: allow, address: 192.168.1.10-192.168.1.20 }"
}
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\access_filter\Plugin\AccessFilter\Rule\IpRule implements RuleInterface, ContainerFactoryPluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of IpRule
File
- src/
Plugin/ AccessFilter/ Rule/ IpRule.php, line 26
Namespace
Drupal\access_filter\Plugin\AccessFilter\RuleView source
class IpRule extends PluginBase implements RuleInterface, ContainerFactoryPluginInterface {
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public function summary() {
$action = $this->configuration['action'];
if ($action == 'allow') {
return $this
->t('Allow from @ip', [
'@ip' => $this->configuration['address'],
]);
}
elseif ($action == 'deny') {
return $this
->t('Deny from @ip', [
'@ip' => $this->configuration['address'],
]);
}
else {
return $this
->t('Invalid configuration.');
}
}
/**
* {@inheritdoc}
*/
public function validateConfiguration(array $configuration) {
$errors = [];
if (!isset($configuration['address']) || !strlen($configuration['address'])) {
$errors[] = $this
->t("'@property' is required.", [
'@property' => 'address',
]);
}
return $errors;
}
/**
* {@inheritdoc}
*/
public function check(Request $request) {
$pattern = isset($this->configuration['address']) ? $this->configuration['address'] : '*';
if ($pattern == '*') {
$is_ip_matched = TRUE;
}
else {
$ip = $request
->getClientIp();
$ip_long = ip2long($ip);
$patterns = explode('-', $pattern);
if (isset($patterns[1])) {
// Check as 2 IP address range format.
$is_ip_matched = $ip_long >= ip2long($patterns[0]) && $ip_long <= ip2long($patterns[1]);
}
else {
// Check as single IP address and subnet format.
$check = explode('/', $pattern);
if (!isset($check[1])) {
$check[1] = 32;
}
$network_long = ip2long($check[0]);
$mask_long = bindec(str_repeat('1', $check[1]) . str_repeat('0', 32 - $check[1]));
$is_ip_matched = ($ip_long & $mask_long) == $network_long;
}
}
if ($is_ip_matched) {
if ($this->configuration['action'] == 'allow') {
return AccessResult::allowed();
}
return AccessResult::forbidden();
}
return AccessResult::neutral();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
IpRule:: |
public | function |
Checks the current access by the rule. Overrides RuleInterface:: |
|
IpRule:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
IpRule:: |
public | function |
Gets summary text for the rule. Overrides RuleInterface:: |
|
IpRule:: |
public | function |
Validates configuration data. Overrides RuleInterface:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 92 |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |