You are here

function cas_roles_cas_user_alter in CAS roles 7.2

Same name and namespace in other branches
  1. 7 cas_roles.module \cas_roles_cas_user_alter()

Implements hook_cas_user_alter().

File

./cas_roles.module, line 70
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_cas_user_alter(&$cas_user) {
  $require_a_role_create = variable_get('cas_roles_require_a_role_create', FALSE);
  $require_a_role_login = variable_get('cas_roles_require_a_role_login', FALSE);
  if (!($require_a_role_create || $require_a_role_login)) {

    // Both options are not set, so there is no need to continue.
    return;
  }
  $relations = variable_get('cas_roles_relations', array());
  $roles = variable_get('cas_roles_roles');
  $role_candidates = cas_roles_candidates($cas_user, $roles);
  $one_matches = FALSE;

  // Check if one of the candidate roles matches a regular expression.
  foreach (user_roles(TRUE) as $role) {
    if (array_key_exists($role, $relations) && $relations[$role]) {
      $matches = preg_grep($relations[$role], $role_candidates);
      if (!empty($matches)) {
        $one_matches = TRUE;
        break;
      }
    }
  }
  if ($require_a_role_create && !$one_matches) {
    $cas_user['register'] = FALSE;

    // drupal_set_message(t('You can not log in on this site.'), 'error');
  }
  if ($require_a_role_login && !$one_matches) {
    $cas_user['login'] = FALSE;

    // drupal_set_message(t('You can not log in on this site.'), 'error');
  }
}