You are here

protected function CasAttributesSubscriber::doRoleMapCheck in CAS Attributes 8

Same name and namespace in other branches
  1. 2.x src/Subscriber/CasAttributesSubscriber.php \Drupal\cas_attributes\Subscriber\CasAttributesSubscriber::doRoleMapCheck()

Determine which roles should be added/removed based on attributes.

Parameters

array $attributes: The attributes associated with the user.

Return value

array Array containing two keys:

  • add: the list of RIDs to add to the user
  • remove: the list of RIDs to remove from the user
2 calls to CasAttributesSubscriber::doRoleMapCheck()
CasAttributesSubscriber::onPreLogin in src/Subscriber/CasAttributesSubscriber.php
Subscribe to the CasPreLoginEvent.
CasAttributesSubscriber::onPreRegister in src/Subscriber/CasAttributesSubscriber.php
Subscribe to the CasPreRegisterEvent.

File

src/Subscriber/CasAttributesSubscriber.php, line 187

Class

CasAttributesSubscriber
Provides a CasAttributesSubscriber.

Namespace

Drupal\cas_attributes\Subscriber

Code

protected function doRoleMapCheck(array $attributes = NULL) {
  $role_map = $this->settings
    ->get('role.mappings');
  if (empty($role_map)) {
    return [
      'add' => [],
      'remove' => [],
    ];
  }
  $rolesToAdd = [];
  $rolesToRemove = [];
  foreach ($role_map as $condition) {

    // Attribute not found; don't map role.
    if (!isset($attributes[$condition['attribute']])) {
      continue;
    }
    $attributeValue = $attributes[$condition['attribute']];
    if (!is_array($attributeValue)) {
      $attributeValue = [
        $attributeValue,
      ];
    }
    $valueToMatch = $condition['value'];
    $matched = FALSE;
    switch ($condition['method']) {
      case 'exact_single':
        $matched = $this
          ->checkRoleMatchExactSingle($attributeValue, $valueToMatch);
        break;
      case 'exact_any':
        $matched = $this
          ->checkRoleMatchExactAny($attributeValue, $valueToMatch);
        break;
      case 'contains_any':
        $matched = $this
          ->checkRoleMatchContainsAny($attributeValue, $valueToMatch);
        break;
      case 'regex_any':
        $matched = $this
          ->checkRoleMatchRegexAny($attributeValue, $valueToMatch);
      default:
    }
    if (isset($condition['negate']) && $condition['negate']) {
      $matched = !$matched;
    }
    if ($matched) {
      $rolesToAdd[] = $condition['rid'];
    }
    elseif ($condition['remove_without_match']) {
      $rolesToRemove[] = $condition['rid'];
    }
  }
  return [
    'add' => $rolesToAdd,
    'remove' => $rolesToRemove,
  ];
}