You are here

function _simplesaml_auth_login_register in simpleSAMLphp Authentication 7.3

Performs login and/or register actions for SAML authenticated users.

1 call to _simplesaml_auth_login_register()
simplesamlphp_auth_loginpage in ./simplesamlphp_auth.pages.inc
Returns markup for SimpleSAMLphp login page.

File

./simplesamlphp_auth.inc, line 11
Contains non-hook implementations.

Code

function _simplesaml_auth_login_register() {
  global $user;
  global $_simplesamlphp_auth_as;

  // Check if the user is logged in via SAML (but not Drupal) and is also allowed
  // to log in by other contrib modules. Please note that no error messaging is done in
  // this hook invocation - each contrib module should do its own messaging.
  if ($_simplesamlphp_auth_as
    ->isAuthenticated() && _simplesamlphp_auth_allow_user_by_attribute()) {

    // Get unique identifier from saml attributes.
    $authname = _simplesamlphp_auth_get_authname();
    if (variable_get('simplesamlphp_auth_debug', 0)) {
      watchdog('simplesamlphp_auth', 'Authname is [%authname] userid is [%uid]', array(
        '%authname' => $authname,
        '%uid' => $user->uid,
      ), WATCHDOG_DEBUG);
    }
    if (!empty($authname)) {

      // User is logged in with SAML authentication and we got the unique
      // identifier, so try to log into Drupal.
      if (variable_get('simplesamlphp_auth_debug', 0)) {
        watchdog('simplesamlphp_auth', 'Loading Drupal user [%authname]', array(
          '%authname' => $authname,
        ), WATCHDOG_DEBUG);
      }

      // Retrieve user mapping and attempt to log the user in.
      $ext_user = user_external_load($authname);

      // If we did not find a Drupal user, register a new one.
      if (!$ext_user) {

        // Check if a local drupal account exists (to auto-enable SAML).
        $local_user = user_load_by_name($authname);
        if ($local_user && variable_get('simplesamlphp_auth_autoenablesaml', FALSE)) {
          user_set_authmaps($local_user, array(
            'authname_simplesamlphp_auth' => $authname,
          ));
          $ext_user = $local_user;
        }
        else {
          $ext_user = _simplesaml_auth_user_register($authname);
        }
      }

      // Provides opportunity to perform additional prelogin authentication.
      $attributes = simplesamlphp_auth_get_attributes();
      foreach (module_implements('simplesamlphp_auth_pre_login') as $module) {
        module_invoke($module, 'simplesamlphp_auth_pre_login', $attributes, $ext_user);
      }

      // Log the user in.
      _simplesaml_auth_user_login($ext_user);
    }
  }
}