diff --git a/simplesamlphp_auth.module b/simplesamlphp_auth.module index b3f3ed1..e326ddc 100644 --- a/simplesamlphp_auth.module +++ b/simplesamlphp_auth.module @@ -352,24 +352,30 @@ function simplesamlphp_auth_init() { function simplesamlphp_auth_user_insert(&$edit, $account, $category = NULL) { global $_simplesamlphp_auth_as; global $_simplesamlphp_auth_saml_attributes; + global $user; if (!_simplesamlphp_auth_isEnabled()) { // Exit without initializing. return; } - if ($category = 'account') { - // If user registration has a valid session... - if ($_simplesamlphp_auth_as->isAuthenticated()) { - // Get name from default attributes. - try { - _simplesaml_auth_debug(t('Registering user [%acctname]', array('%acctname' => $account->name))); - $account->name = _simplesamlphp_auth_get_default_name($account->uid); - } - catch (Exception $e) { - drupal_set_message(t('Your user name was not provided by your identity provider (IDP).'), "error"); - watchdog('simplesamlphp_auth', $e->getMessage(), NULL, WATCHDOG_CRITICAL); - } + if ($category != 'account') { + return; + } + + // If user registration has a valid session... + if (!$_simplesamlphp_auth_as->isAuthenticated()) { + return; + } + + // There are cases where the logged in user is an admin and is creating + // accounts. In such cases we don't want to use his name and email; + // we want to keep the supplied values. + if ($user->uid == 0 || $user->uid === $account->uid) { + // Get name from default attributes. + try { + _simplesaml_auth_debug(t('Registering user [%acctname]', array('%acctname' => $account->name))); + $account->name = _simplesamlphp_auth_get_default_name($account->uid); db_update('users') ->fields(array('name' => $account->name)) @@ -377,30 +383,42 @@ function simplesamlphp_auth_user_insert(&$edit, $account, $category = NULL) { ->execute(); _simplesaml_auth_debug(t('Updating username [%acctname]', array('%acctname' => $account->name))); + } + catch (Exception $e) { + drupal_set_message(t('Your user name was not provided by your identity provider (IDP).'), "error"); + watchdog('simplesamlphp_auth', $e->getMessage(), NULL, WATCHDOG_CRITICAL); + } + } - // Get mail from default attribute. - try { - $mail_address = _simplesamlphp_auth_get_mail(); - } - catch (Exception $e) { - drupal_set_message(t('Your e-mail address was not provided by your identity provider (IDP).'), "error"); - watchdog('simplesamlphp_auth', $e->getMessage(), NULL, WATCHDOG_CRITICAL); - } + $mail_address = ''; + if ($user->uid == 0 || $user->uid === $account->uid) { + // Get mail from default attribute. + try { + $mail_address = _simplesamlphp_auth_get_mail(); + } + catch (Exception $e) { + drupal_set_message(t('Your e-mail address was not provided by your identity provider (IDP).'), "error"); + watchdog('simplesamlphp_auth', $e->getMessage(), NULL, WATCHDOG_CRITICAL); + } + } + else { + if (!empty($account->mail)) { + $mail_address = $account->mail; + } + } - if (!empty($mail_address)) { - db_update('users') - ->fields(array('mail' => $mail_address)) - ->condition('uid', $account->uid) - ->execute(); - } - - if (module_exists('rules')) { - rules_invoke_event('simplesamlphp_auth_rules_event_register', $account); - } + if (!empty($mail_address)) { + db_update('users') + ->fields(array('mail' => $mail_address)) + ->condition('uid', $account->uid) + ->execute(); + } - _simplesaml_auth_debug(t('Updating mail [%mailaddr]', array('%mailaddr' => $mail_address))); - } + if (module_exists('rules')) { + rules_invoke_event('simplesamlphp_auth_rules_event_register', $account); } + + _simplesaml_auth_debug(t('Updating mail [%mailaddr]', array('%mailaddr' => $mail_address))); } /**