You are here

function cas_user_update in CAS 7

Implements hook_user_update().

When a user is updated, change their CAS username if provided.

File

./cas.module, line 586
Enables users to authenticate via a Central Authentication Service (CAS) Cas will currently work if the auto registration is turned on and will create user accounts automatically.

Code

function cas_user_update(&$edit, $account, $category) {
  if (!array_key_exists('cas_name', $edit)) {

    // If the cas_name key is not provided, there is nothing to do.
    return;
  }
  $cas_name = $edit['cas_name'];

  // See if the user currently has any CAS names.
  reset($account->cas_names);
  if ($aid = key($account->cas_names)) {

    // The user already has CAS username(s) set.
    if (empty($cas_name)) {

      // Remove a CAS username.
      db_delete('cas_user')
        ->condition('uid', $account->uid)
        ->condition('aid', $aid)
        ->execute();
    }
    else {

      // Change a CAS username.
      if ($cas_name != $account->cas_names[$aid]) {
        db_update('cas_user')
          ->fields(array(
          'cas_name' => $cas_name,
        ))
          ->condition('uid', $account->uid)
          ->condition('aid', $aid)
          ->execute();
      }
    }
  }
  else {

    // No current CAS usernames.
    if (!empty($cas_name)) {

      // Add a CAS username.
      db_insert('cas_user')
        ->fields(array(
        'uid' => $account->uid,
        'cas_name' => $cas_name,
      ))
        ->execute();
    }
  }

  // Update $account to reflect changes.
  $users = array(
    $account->uid => $account,
  );
  cas_user_load($users);
}