You are here

function hook_cas_user_presave in CAS 6.3

Same name and namespace in other branches
  1. 7 cas.api.php \hook_cas_user_presave()

A CAS user has authenticated and the login is about to be finalized.

This allows modules to react to a CAS user logging in and alter their account properties. For example, modules may want to synchronize Drupal user roles or profile information with LDAP properties.

If you would like to synchronize information only for new accounts, you may examine the value of $account->login which will be 0 if the user has never logged in before.

The 'cas_user' key in $edit contains all information returned from hook_cas_user_alter().

The CAS module promises to call user_save() and user_login_finalize() with this $edit data.

Parameters

$edit: An array of values corresponding to the Drupal user to be created.

$account: A Druapl user object.

File

./cas.api.php, line 88
Documentation for CAS API.

Code

function hook_cas_user_presave(&$edit, $account) {
  $cas_name = $edit['cas_user']['name'];

  // Look up the user's real name using LDAP.
  $ldap_connection = ldap_connect('ldap.example.com', 389);
  $ldap_result = ldap_search($ldap_connection, 'ou=people', 'uid=' . $cas_name, array(
    'cn',
  ), 0, 1);
  $entries = ldap_get_entries($ldap_connection, $ldap_result);
  $attributes = $entries[0];
  if (!empty($attributes['cn'])) {
    $edit['name'] = $attributes['cn'];
  }
}