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	29 Jul 2009 22:08:35 -0000
@@ -129,6 +129,10 @@ 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', '');
+    $cas_attributes      = (int)variable_get('cas_attributes', 0);
+    $cas_attributes_uid  = (string)variable_get('cas_attributes_uid', '');
+    $cas_attributes_name = (string)variable_get('cas_attributes_name', '');
+    $cas_attributes_mail = (string)variable_get('cas_attributes_mail', '');
     $all_roles         = user_roles();
     $cas_roles         = array();
     foreach ($all_roles as $key => $value) {
@@ -194,8 +198,29 @@ function cas_login_check() {
       }
     }
     
+    if (method_exists("phpCAS","getAttributes")) {
+      $attributes = phpCAS::getAttributes();
+    }
+    else {
+      $cas_attributes = 0;
+    }
+    
     $cas_name = phpCAS::getUser();
     
+    if ($cas_attributes && $cas_attributes_uid) {
+      $cas_name = cas_attributes_concat($cas_attributes_uid, $attributes);
+    }
+    
+    $cas_uid = "name";
+    if ($cas_attributes && $cas_attributes_uid) {
+      if ($cas_authmap) {
+        $cas_uid = "init";
+      }
+      else {
+        $cas_uid = "uid";
+      }
+    }
+    
     /*
      * Invoke hook_auth_transform($op, &$username)
      *
@@ -241,13 +266,13 @@ function cas_login_check() {
     // try to log into Drupal
     if ($cas_authmap) {
       // users are coming from Drupal; no need to use the external_load and the authmap
-      $user = user_load(array("name" => $cas_name));
+      $user = user_load(array($cas_uid => $cas_name));
     }
     else {
       // users are external; use authmap table for associating external users
       $user = user_external_load($cas_name);
       if (!$user->uid && variable_get('cas_hijack_user', 0)) {
-        $user = user_load(array("name" => $cas_name));
+        $user = user_load(array($cas_uid => $cas_name));
         if ($user->uid) user_set_authmaps($user, array('authname_cas' => $cas_name));
       }
     }
@@ -265,6 +290,15 @@ 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_name) {
+            $user_default['name'] = cas_attributes_concat($cas_attributes_name, $attributes);
+          }
+          if ($cas_attributes_mail) {
+            $user_default['mail'] = cas_attributes_concat($cas_attributes_mail, $attributes);
+          }
+        }
+        
         // Become user 1 to be able to save profile information
         session_save_session(FALSE);
         $admin = array('uid' => 1);
@@ -579,6 +613,55 @@ 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_concat'] = array(
+      '#type' => 'textfield',
+      '#title' => t('concatenation string'),
+      '#default_value' => variable_get('cas_attributes_concat', ' '),
+      '#size' => 30,
+      '#description' => t('String to use when concatenating attributes'),
+    );
+  
+    $form['attributes']['cas_attributes_uid'] = array(
+      '#type' => 'textfield',
+      '#title' => t('uid'),
+      '#default_value' => variable_get('cas_attributes_uid', ''),
+      '#size' => 30,
+      '#description' => t('drupal\'s internal user id'),
+    );
+  
+    $form['attributes']['cas_attributes_name'] = array(
+      '#type' => 'textfield',
+      '#title' => t('name'),
+      '#default_value' => variable_get('cas_attributes_name', ''),
+      '#size' => 30,
+      '#description' => t('A display name for the user'),
+    );
+  
+    $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'),
+    );
+
+  }
   
   $form['pages'] = array(
     '#type' => 'fieldset',
@@ -1015,6 +1098,17 @@ function _cas_single_sign_out_save_token
 ** CAS Sigle Sign Out - END
 */
 
+/**
+ * Helper function to concatenate attribute strings.
+ *
+ * @return string
+ *   The values of the input attributes concatenated by the value in cas_attributes_concat.
+ */
+function cas_attributes_concat($attribute_names, $attribute_values) {
+  return implode(variable_get('cas_attributes_concat', " "), array_intersect_key($attribute_values, array_flip(split(",", $attribute_names))));
+}
+
+
 function _cas_external_user_is_blocked($name) {
   $deny = db_fetch_object(db_query("SELECT u.name FROM {users} u JOIN {authmap} m ON u.uid=m.uid WHERE status = 0 AND m.authname = LOWER('%s')", $name));
   return $deny;
