class RoleFirewall in Restrict Login or Role Access by IP Address 8.4
Class RoleFirewall.
@package Drupal\restrict_by_ip
Hierarchy
- class \Drupal\restrict_by_ip\RoleFirewall implements RoleFirewallInterface
Expanded class hierarchy of RoleFirewall
1 string reference to 'RoleFirewall'
1 service uses RoleFirewall
File
- src/
RoleFirewall.php, line 20 - Contains Drupal\restrict_by_ip\RoleFirewall.
Namespace
Drupal\restrict_by_ipView source
class RoleFirewall implements RoleFirewallInterface {
protected $ipTools;
protected $config;
protected $entityManager;
public function __construct(IPToolsInterface $ip_tools, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_manager) {
$this->ipTools = $ip_tools;
$this->config = $config_factory
->get('restrict_by_ip.settings');
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/
public function rolesToRemove() {
$roles = $this
->getAllRoles();
$user_ip = $this->ipTools
->getUserIP();
$remove_roles = [];
foreach ($roles as $name) {
$role_data = $this->config
->get('role.' . $name);
if (strlen($role_data) == 0) {
continue;
}
$ranges = explode(';', $role_data);
foreach ($ranges as $range) {
try {
$this->ipTools
->validateCIDR($user_ip, $range);
} catch (IPOutOfRangeException $e) {
$remove_roles[] = $name;
}
}
}
return $remove_roles;
}
/**
* Get list of all available roles.
*
* @return array
* Array of role IDs.
*/
private function getAllRoles() {
$entities = $this->entityManager
->getStorage('user_role')
->loadMultiple();
$roles = [];
foreach ($entities as $role) {
$roles[] = $role
->id();
}
return $roles;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
RoleFirewall:: |
protected | property | ||
RoleFirewall:: |
protected | property | ||
RoleFirewall:: |
protected | property | ||
RoleFirewall:: |
private | function | Get list of all available roles. | |
RoleFirewall:: |
public | function |
Checks which roles should be removed based on IP whitelists. Overrides RoleFirewallInterface:: |
|
RoleFirewall:: |
public | function |