--- cas.module-head 2007-02-25 03:13:32.000000000 +0100
+++ modules/cas/cas.module 2007-02-25 03:46:41.000000000 +0100
@@ -8,6 +8,40 @@
require_once('CAS/CAS.php');
+/*
+** LDAPAuth interfacing - BEGIN
+*/
+
+/**
+* LDAP Auxiliary functions
+*/
+
+function _get_ldap_config_name ($user_name)
+{
+ include_once('modules/ldap_integration/ldapauth.module');
+ $user_found = false;
+ $result = db_query("SELECT name FROM {ldapauth} WHERE status = '%d' ORDER BY sid", 1);
+ while ($row = db_fetch_object($result)) {
+ // cycle thru the authentication schemes - first successful one wins
+ // instantiate ldap
+ _ldapauth_init($row->name);
+ $ldap_user_entry = _ldapauth_user_lookup($user_name);
+ if ($ldap_user_entry)
+ {
+ $user_found = true;
+ break;
+ }
+ }
+ if ($user_found)
+ return $row->name;
+ else
+ return false;
+}
+
+/*
+** LDAPAuth interfacing - END
+*/
+
/**
* Implementation of hook_init
* Traps a page load to see if authentication is required.
@@ -20,7 +54,7 @@
//do nothing because user is already logged into Drupal
}
elseif ( _cas_force_login()){
- $user_register = variable_get('user_register',1);
+ $cas_user_register = variable_get('cas_user_register',1);
$cas_authmap = variable_get('cas_authmap',0);
phpCAS::setDebug();
$server_version = (string)variable_get('cas_version', '2.0');
@@ -64,7 +98,7 @@
// If we don't have a user register them.
if(!$user->uid) {
- if($user_register == 1) {
+ if($cas_user_register == 1) {
$user_default = array("name" => $cas_name, "pass" => "", "init" => db_escape_string($cas_name), "status" => 1, "roles" => array(DRUPAL_AUTHENTICATED_RID));
if (!$cas_authmap)
$user_default['authname_cas'] = $cas_name;
@@ -82,12 +116,36 @@
if($user->uid && $user->uid > 0) { // final check to make sure we have a good user
{
+ /*
+ ** LDAPAuth interfacing - BEGIN
+ */
+
+ if (variable_get('cas_useldap_groups',''))
+ {
+ if ($ldap_config_name = _get_ldap_config_name($user->name))
+ {
+ _ldapauth_init($ldap_config_name);
+ include_once('modules/ldap_integration/ldapgroups.module');
+ $user->ldap_authentified = true;
+ ldapgroups_user_login($user);
+ }
+ }
+
+ /*
+ ** LDAPAuth interfacing - END
+ */
+
drupal_set_message("Logged in via CAS as " . $user->name . ".");
// We can't count on the menu because we're changing login states.
cas_login_page();
}
} // if we have a good user
+ else
+ {
+ session_destroy();
+ $user = drupal_anonymous_user();
+ }
} // End if user is already logged in else
}
@@ -120,7 +178,7 @@
if ($may_cache) {
$items[] = array(
'path' => 'admin/user/cas',
- 'title' => t('cas settings'),
+ 'title' => t('CAS settings'),
'description' => 'Configure central authentication services',
'callback' => 'drupal_get_form',
'callback arguments' => 'cas_admin_settings',
@@ -154,12 +212,17 @@
$form['account'] = array('#type' => 'fieldset', '#title' => t('User account settings'),'#collapsible' => true,'#collapsed' => true);
$form['account']['cas_authmap'] = array('#type' => 'checkbox', '#title' => t('Is Drupal also the CAS user repository?'), '#default_value' => variable_get('cas_authmap', 0), '#description' => t('In most cases, the answer will be no; an LDAP repository will be the source of CAS users. But in some cases, the Drupal user database could be used as the central user store for single sign-on. If this is the case, select this option.'));
$form['account']['cas_hijack_user'] = array('#type' => 'checkbox', '#title' => t('If CAS is not the user repository, should cas highjack users with the same name?'), '#default_value' => variable_get('cas_hijack_user', 0), '#description' => t('If you have pre-created regular accounts in cas that you want converted to mapped accounts, check this box. Otherwise cas will likely throw duplicate key violation errors on new users.'));
+ $form['account']['cas_user_register'] = array('#type' => 'checkbox', '#title' => t('Should Drupal user accounts be automatically created?'), '#default_value' => variable_get('cas_user_register', 1), '#description' => t('If a CAS user logs in, his Drupal account will automatically be created. If you don\'t check this option, you will have to pre-create accounts for the users you want to allow.'));
$form['account']['cas_domain'] = array('#type' => 'textfield', '#title' => t('Email Domain'), '#default_value' => variable_get('cas_domain', ''), '#size' => 30,'#maxlength' => 55, '#description' => t('Append this domain name to each new user in order generate his email address.'));
$form['account']['cas_hide_email'] = array('#type' => 'checkbox', '#title' => t('Users canot change email address'), '#default_value' => variable_get('cas_hide_email', 0), '#description' => t('Hide email address field on the edit user form'));
$form['account']['cas_hide_password'] = array('#type' => 'checkbox', '#title' => t('Users canot change password'), '#default_value' => variable_get('cas_hide_password', 0), '#description' => t('Hides password field on the edit user form'));
$form['pages'] = array('#type' => 'fieldset', '#title' => t('Redirection settings'),'#collapsible' => true, '#collapsed' => true);
$form['pages']['cas_access'] = array('#type' => 'radios', '#title' => t('Require Cas Login for '), '#default_value' => variable_get('cas_access',0), '#options' => array(t('specific pages'), t('all pages')));
$form['pages']['cas_pages'] = array('#type' => 'textarea', '#title' => t('Specific pages'), '#default_value' => variable_get('cas_pages',''), '#cols' => 40, '#rows' => 5, '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are 'blog' for the blog page and 'blog/*' for every personal blog. '<front>' is the front page."));
+ $form['ldap'] = array('#type' => 'fieldset', '#title' => t('LDAP settings'),'#collapsible' => true, '#collapsed' => true);
+ $form['ldap']['cas_useldap'] = array('#type' => 'checkbox', '#title' => t('Should we extract the user email from an LDAP directory?'), '#default_value' => variable_get('cas_useldap', 0), '#description' => t('Activate this option if you want to extract the user email from an LDAP directory. Ldapauth module must be enabled and configured.'));
+ $form['ldap']['cas_ldap_email_attribute'] = array('#type' => 'textfield', '#title' => t('Email attribute'), '#default_value' => variable_get('cas_ldap_email_attribute', 'mail'), '#size' => 30,'#maxlength' => 55, '#description' => t('LDAP entry attribute containing the email address.'));
+ $form['ldap']['cas_useldap_groups'] = array('#type' => 'checkbox', '#title' => t('Should we extract user groups from an LDAP directory?'), '#default_value' => variable_get('cas_useldap_groups', 0), '#description' => t('Activate this option if you want to extract the user groups from an LDAP directory. Ldapgroups module must be enabled and configured.'));
return system_settings_form($form);
}
/**
@@ -224,7 +287,7 @@
// NOTE: special care needs to be taken because on pages with forms,
// such as node and comment submission pages, the $edit variable
// might already be set.
- $output .= l('Login','cas');
+ $output .= l(t('Login'),'cas');
$output .= "\n";
$block['subject'] = t('User Login');
$block['content'] = $output;
@@ -268,10 +331,40 @@
switch ($form_id){
case 'user_edit':
//make the email field hidden and force the value to the default.
- if (variable_get('cas_hide_email',0) && variable_get('cas_domain',''))
+ if (variable_get('cas_hide_email',0))
{
- $form['account']['mail']['#type']='hidden';
- $form['account']['mail']['#value']= $form['account']['name']['#default_value'].'@'.variable_get('cas_domain','');
+ if (variable_get('cas_domain',''))
+ {
+ $form['account']['mail']['#type']='hidden';
+ $form['account']['mail']['#value']= $form['account']['name']['#default_value'].'@'.variable_get('cas_domain','');
+ }
+
+ /*
+ ** LDAPAuth interfacing - BEGIN
+ */
+
+ if (variable_get('cas_useldap',''))
+ {
+ global $ldap, $user;
+ if ($ldap_config_name = _get_ldap_config_name($user->name))
+ {
+ _ldapauth_init($ldap_config_name);
+ $ldap->connect();
+ $cas_ldap_email_attribute = (string)variable_get('cas_ldap_email_attribute','mail');
+ $ldap_entries = $ldap->search($ldap->getOption('basedn'), $ldap->getOption('user_attr').'='.$user->name, array($cas_ldap_email_attribute));
+ if ($ldap_entries['count']==1 && isset($ldap_entries[0][$cas_ldap_email_attribute][0]))
+ if (trim($ldap_entries[0][$cas_ldap_email_attribute][0])!='')
+ {
+ $form['account']['mail']['#type']='hidden';
+ $form['account']['mail']['#value']= $ldap_entries[0][$cas_ldap_email_attribute][0];
+ }
+ }
+ }
+
+ /*
+ ** LDAPAuth interfacing - END
+ */
+
}
//Remove the password fields from the form.