This documentation does not apply to Drupal 8 and higher. See the new documentation instead.

When using the CAS module for authentication, users still need to have valid Drupal user accounts. The accounts may be created automatically when the user first visits your site ("Automatically create Drupal accounts?") or manually added by an administrator.

To associate a CAS username to an existing user, simply navigate to the user edit page for that user and enter their CAS username in the CAS username field. Only individuals with the Administer users permission will be able to modify the assigned CAS username.

Multiple CAS usernames may be associated with the same Drupal user. To control additional assigned usernames, use the link below the CAS username field.

The CAS username field is present on the register new user page (if the administrator has the proper permissions).

Lastly, there is a "New CAS user" page (accessible from Admin >> People >> Users) which prompts only for the CAS username. Creating a new user through this form is equivalent to that same user visiting the site for the first time with the "Automatically create Drupal accounts?" option enabled. Using this method may be desirable if using a module like CAS Attributes to use LDAP to fetch additional information about the user.

Adding a CAS username for all existing Drupal users

You can associate a CAS username (matching the Drupal username) to all users using a SQL query similar to:

INSERT INTO cas_user (uid, cas_name)
SELECT u.uid AS uid, u.name as cas_name
FROM users u
WHERE uid <> 0 AND NOT EXISTS (SELECT cas_name FROM cas_user c WHERE c.cas_name = u.name);

Add CAS usernames to existing Drupal accounts on login

There is no configurable option to associate a CAS username with an existing Drupal account during the login process. This was offered as an option in the past, however now we require explicit administrator action. (Note that we do allow a new Drupal user account to be created an associated with the CAS username).

If you truly need this behavior, the functionality could be mimicked using the hook_cas_user_alter() hook.

function CUSTOM_cas_user_alter(&$cas_user) {
  $cas_name = $cas_user['name'];

  if (cas_user_load_by_name($cas_name) === FALSE) {
    // No existing user could be found by CAS username.

    $account = user_load(array('name' => $cas_name));
    if ($account && $account->uid) {
      // We found an existing user with the same username, add a CAS username.
      cas_user_operations_create_username(array($account->uid));
    }
  }
}

Comments

kclarkson’s picture

I created a custom module with the information provided and here is the error I received:

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of /srv/bindings/275d77266e1f471eb2b9f9f92b2b0e1a/code/includes/entity.inc).

A new account could not be created for kclarks2. The username is already in use on this site.

Kaleem S. Clarkson
www.kaleemclarkson.com