You are here

class RoleFirewall in Restrict Login or Role Access by IP Address 8.4

Class RoleFirewall.

@package Drupal\restrict_by_ip

Hierarchy

Expanded class hierarchy of RoleFirewall

1 string reference to 'RoleFirewall'
restrict_by_ip.services.yml in ./restrict_by_ip.services.yml
restrict_by_ip.services.yml
1 service uses RoleFirewall
restrict_by_ip.role_firewall in ./restrict_by_ip.services.yml
Drupal\restrict_by_ip\RoleFirewall

File

src/RoleFirewall.php, line 20
Contains Drupal\restrict_by_ip\RoleFirewall.

Namespace

Drupal\restrict_by_ip
View 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

Namesort descending Modifiers Type Description Overrides
RoleFirewall::$config protected property
RoleFirewall::$entityManager protected property
RoleFirewall::$ipTools protected property
RoleFirewall::getAllRoles private function Get list of all available roles.
RoleFirewall::rolesToRemove public function Checks which roles should be removed based on IP whitelists. Overrides RoleFirewallInterface::rolesToRemove
RoleFirewall::__construct public function