You are here

function shib_auth_init in Shibboleth Authentication 6

Same name and namespace in other branches
  1. 5.3 shib_auth.module \shib_auth_init()
  2. 5.2 shib_auth.module \shib_auth_init()
  3. 6.4 shib_auth.module \shib_auth_init()
  4. 6.2 shib_auth.module \shib_auth_init()
  5. 6.3 shib_auth.module \shib_auth_init()
  6. 7.4 shib_auth.module \shib_auth_init()

Create a new user based on informations from the Shibboleth handler if it's necessary or log in.

File

./shib_auth.module, line 23

Code

function shib_auth_init() {
  global $user;
  $unameVar = variable_get('shib_auth_username_variable', 'REMOTE_USER');
  $umailVar = variable_get('shib_auth_username_email', 'HTTP_SHIB_MAIL');

  // If
  // - The user isn't logged in
  // - There is Shibboleth authentication in the background
  // - The settings are fine and there has been a valid username setted up
  // - The settings are fine and there has been a valid user email address setted up
  if (!$user->uid && $_SERVER['HTTP_SHIB_IDENTITY_PROVIDER']) {
    if ($_SERVER[$unameVar] && $_SERVER[$umailVar]) {
      user_external_login_register($_SERVER[$unameVar], "shib_auth");
    }
    else {
      drupal_set_message(t("Username or e-mail address is missing. Maybe the Shibboleth configuration is not perfect."), "error");
    }
  }
  if ($user->uid && $_SERVER['HTTP_SHIB_IDENTITY_PROVIDER']) {
    $account = user_save($user, array(
      'mail' => $_SERVER[$umailVar],
    ));

    // Terminate if an error occured during user_save().
    if (!$account) {
      drupal_set_message(t("Error saving user account."), 'error');
      return;
    }
    $user = $account;
  }
}