You are here

function cas_roles_candidates in CAS roles 7

Same name and namespace in other branches
  1. 7.2 cas_roles.module \cas_roles_candidates()

Translate attributes to role candidates.

2 calls to cas_roles_candidates()
cas_roles_cas_user_alter in ./cas_roles.module
Implements hook_cas_user_alter().
cas_roles_cas_user_presave in ./cas_roles.module
Implements hook_cas_user_presave().

File

./cas_roles.module, line 138
Allows user account and profile attributes to be automatically populated using tokens. Provides basic tokens for attributes returned by the CAS server.

Code

function cas_roles_candidates($cas_user) {
  $role_candidates =& drupal_static(__FUNCTION__);
  if (!isset($role_candidates)) {
    $roles = variable_get('cas_roles_roles');

    // The users CAS attributes from the CAS module.
    if (!isset($cas_user['attributes'])) {
      return array();
    }
    $cas_attributes = $cas_user['attributes'];

    // Get the name of the attributes.
    $tokens = token_scan($roles);

    // There is no cas token, hence no matching.
    if (!is_array($tokens) || !array_key_exists('cas', $tokens)) {
      return array();
    }
    $tokens = token_find_with_prefix($tokens['cas'], 'attribute');

    // An Array with the relevant CAS attribute arrays.
    $arr = array();
    foreach ($tokens as $name => $original) {

      // Custom token treatment to use the specified sub element or array
      // ignore the case of the keys.
      $chain = explode(':', strtolower($name));
      $branch = array_change_key_case($cas_attributes);
      $found = TRUE;
      foreach ($chain as $link) {
        if (isset($branch[$link])) {
          if (is_array($branch[$link])) {

            // Change the keys of the branch if it is an array.
            $branch[$link] = array_change_key_case($branch[$link]);
          }
          $branch = $branch[$link];
        }
        else {
          $found = FALSE;
        }
      }
      if ($found) {
        $arr[$original] = $branch;
      }
    }

    // Assemble the patterns.
    // at the end of this step $role_candidates will be the exploded attributes.
    $role_candidates = array(
      $roles,
    );
    foreach ($arr as $token => $elements) {
      $new_candidates = array();
      foreach ($role_candidates as $pattern) {
        $new_pattern = array();
        _cas_roles_recursive_str_replace($new_pattern, $token, $elements, $pattern);
        $new_candidates = array_merge($new_candidates, $new_pattern);
      }
      $role_candidates = $new_candidates;
    }

    // Replace all the tokens including the cas tokens if cas_attributes exists.
    if (module_exists('cas_attributes')) {
      $data = array(
        'cas' => $cas_user['name'],
      );
    }
    else {
      $data = array();
    }
    foreach ($role_candidates as $key => $pattern) {
      $pattern = trim(token_replace($pattern, $data, array(
        'clear' => TRUE,
      )));
      $role_candidates[$key] = html_entity_decode($pattern);
    }
    $role_candidates = array_unique($role_candidates);
  }
  return $role_candidates;
}