/**
* Implementation of hook_user().
*/
function simplesamlphp_auth_user($op, &$edit, &$account, $category = NULL) {
global $as;
global $saml_attributes;
if ($op == 'insert' && $category = 'account') {
//if user registration has a valid session
if ($as->isAuthenticated()) {
//Get name from default attributes
try {
_simplesaml_auth_debug('Registering user [' . $account->name . ']');
$account->name = _simplesamlphp_auth_get_default_name($account->uid);
} catch (Exception $e) {
$message = t('Username is missing.' . $e->getMessage());
drupal_set_message($message, "error");
watchdog('simplesamlphp', $message, WATCHDOG_CRITICAL);
}
db_query("UPDATE {users} SET name = '%s' WHERE uid = %d", $account->name, $account->uid);
_simplesaml_auth_debug('Updating username [' . $account->name . ']');
//Get mail from default attribute
try {
$mail_address = _simplesamlphp_auth_get_mail();
} catch (Exception $e) {
$message = t('Email is missing.' . $e->getMessage());
drupal_set_message($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('Updating mail [' . $mail_address . ']');
}
} elseif ($op == 'logout') {
global $as;
global $saml_attributes;
if (!empty($saml_attributes)) {
$config = SimpleSAML_Configuration::getInstance();
$msg = 'with_slo';
try {
$slo = $config->getString('SingleLogoutService');
} catch(Exception $e) {
$msg = "no_slo";
}
$as->logout('/?msg=' . $msg);
}
} else if ($op == "delete") {
db_query("DELETE FROM {authmap} WHERE uid = %d AND authname = '%s' AND module = 'simplesamlphp_auth'", $account->uid, $account->name);
}
}
function _simplesamlphp_auth_get_default_name($account) {
global $as;
global $saml_attributes;
$default_name = '';
/* Check if valid local session exists.. */
if ($as->isAuthenticated()) {
$attributes = $saml_attributes;
switch (variable_get('simplesamlphp_auth_user_name', 'eduPersonPrincipalName')) {
case "eduPersonPrincipalName":
if (!isset($attributes['eduPersonPrincipalName'])) {
throw new Exception(t('eduPersonPrincipalName was not set for your user.'));
}
$default_name = $attributes['eduPersonPrincipalName'][0];
break;
case "smartname-fullname-eduPersonPrincipalName":
if (!isset($attributes['smartname-fullname'])) {
throw new Exception(t('smartname-fullname was not set for your user.'));
}
if (!isset($attributes['eduPersonPrincipalName'])) {
throw new Exception(t('eduPersonPrincipalName was not set for your user.'));
}
$default_name = $attributes['smartname-fullname'][0] . " [" . $attributes['eduPersonPrincipalName'][0]. "]";
break;
case "smartname-fullname-drupaluid":
if (!isset($attributes['smartname-fullname'])) {
throw new Exception(t('smartname-fullname was not set for your user.'));
}
$default_name = $attributes['smartname-fullname'][0]. " [" . $account->uid . "]";
break;
case "smartname-fullname":
if (!isset($attributes['smartname-fullname'])) {
throw new Exception(t('smartname-fullname was not set for your user.'));
}
$default_name = $attributes['smartname-fullname'][0];
break;
default:
throw new Exception(t('error in simplesamlphp_auth.module: no valid name attribute set'));
}
}
return $default_name;
}