You are here

function simplesamlphp_auth_user in simpleSAMLphp Authentication 6.2

Same name and namespace in other branches
  1. 6.3 simplesamlphp_auth.module \simplesamlphp_auth_user()

Implementation of hook_user().

File

./simplesamlphp_auth.module, line 336
simpleSAMLphp authentication module for Drupal.

Code

function simplesamlphp_auth_user($op, &$edit, &$account, $category = NULL) {
  global $_simplesamlphp_auth_as;
  global $_simplesamlphp_auth_saml_attributes;
  if ($op == 'insert' && ($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) {
        $message = t('Username is missing.' . check_plain($e
          ->getMessage()));
        drupal_set_message(t($message), "error");
        watchdog('simplesamlphp', $message, WATCHDOG_CRITICAL);
      }
      db_query("UPDATE {users} SET name = '%s' WHERE uid = %d", $account->name, $account->uid);
      _simplesaml_auth_debug(t('Updating username [%acctname]', array(
        '%acctname' => $account->name,
      )));

      // Get mail from default attribute.
      try {
        $mail_address = _simplesamlphp_auth_get_mail();
      } catch (Exception $e) {
        $message = t('Email is missing.' . check_plain($e
          ->getMessage()));
        drupal_set_message(t($message), "error");
        watchdog('simplesamlphp', $message, WATCHDOG_CRITICAL);
      }
      if (!empty($mail_address)) {
        db_query("UPDATE {users} SET mail = '%s' WHERE uid = %d", $mail_address, $account->uid);
      }
      _simplesaml_auth_debug(t('Updating mail [%mailaddr]', array(
        '%mailaddr' => $mail_address,
      )));
    }
  }
  elseif ($op == 'logout') {
    if (!empty($_simplesamlphp_auth_saml_attributes)) {
      $config = SimpleSAML_Configuration::getInstance();
      $_simplesamlphp_auth_as
        ->logout(base_path());
    }
  }
  elseif ($op == "delete") {
    db_query("DELETE FROM {authmap} WHERE uid = %d AND authname = '%s' AND module = 'simplesamlphp_auth'", $account->uid, $account->name);
  }
}