You are here

function shib_auth_save_mail in Shibboleth Authentication 6.4

Same name and namespace in other branches
  1. 7.4 shib_auth.module \shib_auth_save_mail()

Unfortunately if we called user_save() on updating email, we would possibly lose profile fields, so we are forced to hack with the {users} table

Parameters

$account user account to modify: @param $mail new mail address @return modified user object or FALSE on error, just like user_save would do

2 calls to shib_auth_save_mail()
shib_auth_save_authmap in ./shib_auth.module
Saves an entry into shib_authmap and also saves mail if changed A row in the authmap contains the drupal user id, the targeted id from Shibboleth, the IdP name, the date the user was created, and user consent version number. @uname the username got…
shib_login_authmap in ./shib_auth.module
Login an user based on the shib_authmap informations @uname the username got from IdP @uid drupal user id @umail_single the e-mail address @alreadyloggedin is true if the user has already logged in

File

./shib_auth.module, line 1160
Drupal Shibboleth authentication module.

Code

function shib_auth_save_mail($account, $mail) {

  // Basic parameter checks to avoid do anything fatal
  if (!is_object($account) || !$account->uid || $account->uid <= 1) {
    return FALSE;
  }
  if (!valid_email_address($mail)) {
    shib_auth_error('E-mail address is invalid');
    return FALSE;
  }

  // If the mail hasn't changed, do not do anything
  if ($account->mail && $account->mail == $mail) {
    return $account;
  }

  // We deliberately do not call user_module_invoke('update',..) here, because
  // profile module seems to take it too seriously and wipes out profile fields
  // Cross fingers, do the update
  $success = db_query("UPDATE {users} SET mail = '%s' WHERE uid = %d", $mail, $account->uid);
  if (!$success) {

    // Failed for some reason?
    shib_auth_error('Error saving mail');
    return FALSE;
  }

  // Refresh user object
  $account->mail = $mail;

  // We can call things hooked on after_update
  $null = NULL;
  user_module_invoke('after_update', $null, $account);
  return $account;
}