Index: cas.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/cas/cas.module,v retrieving revision 1.57 diff -u -p -r1.57 cas.module --- cas.module 5 Jun 2009 17:07:19 -0000 1.57 +++ cas.module 11 Aug 2009 22:34:35 -0000 @@ -129,6 +129,12 @@ function cas_login_check() { $cas_domain = (string)variable_get('cas_domain', ''); $cas_cert_verify = (string)variable_get('cas_cert_verify', CAS_NO_VERIFY); $cas_cert = (string)variable_get('cas_cert', ''); + + // Attributes + $cas_attributes = (int)variable_get('cas_attributes', 0); + $cas_attributes_mail = (string)variable_get('cas_attributes_mail', ''); + + // Roles $all_roles = user_roles(); $cas_roles = array(); foreach ($all_roles as $key => $value) { @@ -194,6 +200,13 @@ function cas_login_check() { } } + if (method_exists("phpCAS","getAttributes")) { + $attributes = phpCAS::getAttributes(); + } + else { + $cas_attributes = 0; + } + $cas_name = phpCAS::getUser(); /* @@ -265,6 +278,12 @@ function cas_login_check() { if (!$cas_authmap) $user_default['authname_cas'] = $cas_name; if ($cas_domain) $user_default['mail'] = $cas_name .'@'. $cas_domain; + if ($cas_attributes) { + if ($cas_attributes_mail) { + $user_default['mail'] = $attributes[$cas_attributes_mail]; + } + } + // Become user 1 to be able to save profile information session_save_session(FALSE); $admin = array('uid' => 1); @@ -288,6 +307,36 @@ function cas_login_check() { // final check to make sure we have a good user if ($user->uid && $user->uid > 0) { + + // Always apply attributes from cas to stay up to date. + if ($cas_attributes) { + + // Apply mail attribute only if the email is hidden. + if (variable_get('cas_hide_email', 0)) { + if ($cas_attributes_mail) { + db_query("UPDATE {users} SET mail = '%s' WHERE uid = %d", $attributes[$cas_attributes_mail], $user->uid); + } + } + + //Profile Module + if (module_exists('profile')) { + $profile_fields = db_query('SELECT `fid`, `title`, `name` FROM {profile_fields} ORDER BY `weight` ASC'); + if ($profile_fields != FALSE) { + while ($row = db_fetch_array($profile_fields)) { + $attrib = (string)variable_get('cas_attributes_' . $row['name'],''); + if (!empty($attrib)) { + if (db_result(db_query("SELECT COUNT(*) FROM {profile_values} WHERE `fid` = %d AND `uid` = %d", $row['fid'], $user->uid))) { + db_query("UPDATE {profile_values} SET value = '%s' WHERE fid = %d AND uid = %d", $attributes[$attrib], $row['fid'], $user->uid); + } + else { + db_query("INSERT INTO {profile_values} (fid, uid, value) VALUES (%d, %d, '%s')", $row['fid'], $user->uid, $attributes[$attrib]); + } + } + } + } + } + } + /* ** LDAPAuth interfacing - BEGIN */ @@ -305,13 +354,13 @@ function cas_login_check() { ** LDAPAuth interfacing - END */ - /* - ** CAS Sigle Sign Out - BEGIN - */ - - if (variable_get('cas_signout', FALSE)) { - _cas_single_sign_out_save_token($user, $cas_service_ticket); - } + /* + ** CAS Sigle Sign Out - BEGIN + */ + + if (variable_get('cas_signout', FALSE)) { + _cas_single_sign_out_save_token($user, $cas_service_ticket); + } /* * CAS Single Sign Out - END @@ -421,7 +470,7 @@ function cas_admin_settings() { '#type' => 'radios', '#title' => t('CAS version'), '#default_value' => variable_get('cas_version', '2.0'), - '#options' => array('1.0' => '1.0', '2.0' => '2.0'), + '#options' => array('1.0' => '1.0', '2.0' => '2.0', 'S1' => 'SAML_VERSION_1_1',), ); $form['server']['cas_server'] = array( @@ -579,6 +628,54 @@ function cas_admin_settings() { '#description' => t('This value can be used to establish a role automatically for all CAS users. As an example, if you are also using the simple_ldap module, you can use this role to establish a tie between CAS and LDAP-populated data. i.e. Users with the role of \'cas:user\' should have their LDAP data updated automatically.'), ); + if (method_exists("phpCAS", "getAttributes")) { + + $form['attributes'] = array( + '#type' => 'fieldset', + '#title' => t('User attribute settings'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $form['attributes']['cas_attributes'] = array( + '#type' => 'checkbox', + '#title' => t('Use user attributes'), + '#default_value' => variable_get('cas_attributes', 0), + '#description' => t('If your installation of CAS is set to send back user attributes and your phpCAS supports receiving them, use these options to assign the attributes to drupal user fields. To use multiple attributes in a single field, enter a comma separated list. Their values will be concatenated with a space separator (e.g. FirstName,LastName).'), + ); + + $form['attributes']['cas_attributes_mail'] = array( + '#type' => 'textfield', + '#title' => t('Email'), + '#default_value' => variable_get('cas_attributes_mail', ''), + '#size' => 30, + '#description' => t('The user\'s email address'), + ); + + if (module_exists('profile')) { + $form['attributes']['profile'] = array( + '#type' => 'fieldset', + '#title' => t('Profile Module Fields'), + '#collapsible' => TRUE, + '#collapsed' => TRUE, + ); + + $profile_fields = db_query('SELECT `title`, `name` FROM {profile_fields} ORDER BY `weight` ASC'); + if ($profile_fields != FALSE) { + while ($row = db_fetch_array($profile_fields)) { + $form['attributes']['profile']['cas_attributes_' . $row['name']] = array( + '#type' => 'textfield', + '#title' => t($row['title']), + '#default_value' => variable_get('cas_attributes_' . $row['name'], ''), + '#size' => 30, + ); + } + } + + } + + + } $form['pages'] = array( '#type' => 'fieldset',