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 00:11:20 -0000
@@ -129,6 +129,13 @@ 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_uid  = (string)variable_get('cas_attributes_uid', '');
+    $cas_attributes_mail = (string)variable_get('cas_attributes_mail', '');
+    
+    // Roles
     $all_roles         = user_roles();
     $cas_roles         = array();
     foreach ($all_roles as $key => $value) {
@@ -194,8 +201,25 @@ function cas_login_check() {
       }
     }
     
+    if (method_exists("phpCAS","getAttributes")) {
+      $attributes = phpCAS::getAttributes();
+    }
+    else {
+      $cas_attributes = 0;
+    }
+
     $cas_name = phpCAS::getUser();
     
+    $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 +265,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 +289,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 +318,24 @@ function cas_login_check() {
     
     // final check to make sure we have a good user
     if ($user->uid && $user->uid > 0) {
+      // Add profile module attributes if enabled.
+      if ($cas_attributes && 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 +353,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 +469,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 +627,62 @@ 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_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_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',
