You are here

function saml_sp_drupal_login_update_user_attributes in SAML Service Provider 3.x

Same name and namespace in other branches
  1. 8.3 modules/saml_sp_drupal_login/saml_sp_drupal_login.module \saml_sp_drupal_login_update_user_attributes()
  2. 4.x modules/saml_sp_drupal_login/saml_sp_drupal_login.module \saml_sp_drupal_login_update_user_attributes()

Updates user attributes from SAML data after successful login.

@TODO: All of it.

Parameters

\Drupal\user\UserInterface $user: The logged-in user.

string $email: The user's email address.

array $attributes: Other attributes returned from the IdP.

1 call to saml_sp_drupal_login_update_user_attributes()
saml_sp_drupal_login__saml_authenticate in modules/saml_sp_drupal_login/saml_sp_drupal_login.module
SAML authentication callback.

File

modules/saml_sp_drupal_login/saml_sp_drupal_login.module, line 415
SAML Drupal Login.

Code

function saml_sp_drupal_login_update_user_attributes(UserInterface $user, $email, array $attributes) {

  // Default language is the site default.
  $language = \Drupal::languageManager()
    ->getCurrentLanguage()
    ->getId();

  // If language attribute is set on IdP, then use that language.
  if (isset($attributes['language'])) {
    $language = $attributes['language'][0];
  }

  /*
    // @codingStandardsIgnoreStart
    // Update email address if it has changed on IdP.
    if (\Drupal::config('saml_sp_drupal_login.config')->get('update_email') && $user->mail != $email) {
      \Drupal::logger('saml_sp')->notice('Updating email address from %old_email to %new_email for UID %uid', array('%old_email' => $user->mail, '%new_email' => $email, '%uid' => $user->uid));
      $wrapper = entity_metadata_wrapper('user', $user);
      $wrapper->mail->set($email);
      $wrapper->save();
      // Showing message for user about the update which happened on IdP.
      $message = t('Your email address is now @new_email', array('@new_email' => $email));
      \Drupal::messenger()->addMessage($message);
    }
    // Update language if it has changed on IdP.
    if (\Drupal::config('saml_sp_drupal_login.config')->get('update_language') && $account->language != $language) {
      \Drupal::logger('saml_sp')->notice('Updating language from %old_lang to %new_lang for UID %uid', array('%old_lang' => $user->language, '%new_lang' => $language, '%uid' => $user->uid));
      $wrapper = entity_metadata_wrapper('user', $user);
      $wrapper->language->set($language);
      $wrapper->save();
    }
    // @codingStandardsIgnoreEnd
    /**/
}