View source
<?php
require_once 'simple_ldap_sso.inc';
function simple_ldap_sso_help($path, $arg) {
switch ($path) {
case 'admin/config/people/simple_ldap/sso':
global $cookie_domain;
$t_args = array(
'%cookie_domain' => $cookie_domain,
);
$output = '<p>';
$output .= t('<strong>The current cookie domain is %cookie_domain.</strong>', $t_args) . ' ' . t('The cookie domain must be the same for all sites for single-sign-on to work.') . ' ' . t('To set the cookie domain, see your settings.php file inside of your Drupal directory.');
$output .= '</p>';
return $output;
case 'admin/help#simple_ldap_sso':
return '';
}
}
function simple_ldap_sso_init() {
global $user;
if (!user_is_logged_in() || $user->uid == 1 || !simple_ldap_sso_configured() || !simple_ldap_sso_does_session_need_validation()) {
return;
}
if (!simple_ldap_sso_get_cookie_data()) {
simple_ldap_sso_abort();
return;
}
$limit = variable_get('simple_ldap_sso_flood_limit', 3);
$window = variable_get('simple_ldap_sso_flood_window', 3600);
if ($limit != 0 && !flood_is_allowed(SIMPLE_LDAP_SSO_FLOOD, $limit, $window)) {
simple_ldap_sso_abort();
return;
}
if (!simple_ldap_sso_validate_ldap()) {
simple_ldap_sso_abort();
return;
}
simple_ldap_sso_session_is_valid();
if (simple_ldap_sso_user_needs_sync() && simple_ldap_user_variable_get('simple_ldap_user_source') == 'ldap') {
$account = user_load($user->uid);
simple_ldap_user_sync_user($account);
}
}
function simple_ldap_sso_menu() {
$items['admin/config/people/simple_ldap/sso'] = array(
'title' => 'Single Sign On',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'simple_ldap_sso_admin',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'simple_ldap_sso.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 3,
);
return $items;
}
function simple_ldap_sso_user_login(array &$edit, stdClass $account) {
if ($account->uid == 1 || !simple_ldap_sso_configured()) {
return;
}
drupal_register_shutdown_function('simple_ldap_sso_login_shutdown');
simple_ldap_sso_session_is_valid();
}
function simple_ldap_sso_login_shutdown() {
global $is_https, $user;
$column = $is_https ? 'ssid' : 'sid';
$query = db_select('sessions')
->fields('sessions')
->condition($column, session_id());
$data = $query
->execute()
->fetchAssoc();
$data['name'] = $user->name;
$data['session'] = '';
$sid = $data[$column];
try {
simple_ldap_sso_ldap_save_sid($user->name, $sid);
simple_ldap_sso_set_cookie($data);
$message = 'SSO session created for @name.';
$t_args = array(
'@name' => $user->name,
);
watchdog('simple_ldap_sso', $message, $t_args, WATCHDOG_NOTICE);
} catch (Exception $e) {
$message = 'Unable to set SSO session for user %name. Error: @e';
$t_args = array(
'%name' => $user->name,
'@e' => (string) $e,
);
watchdog(__FUNCTION__, $message, $t_args, WATCHDOG_WARNING);
}
}
function simple_ldap_sso_user_logout(stdClass $account) {
if ($account->uid == 1 || !simple_ldap_sso_configured()) {
return;
}
simple_ldap_sso_delete_cookie();
simple_ldap_sso_ldap_delete_sid($account->name);
}