From d06a694569dda878ad5a4c7b07c60fde706f1d13 Mon Sep 17 00:00:00 2001
From: Eric Rasmussen <erasmussen2@unl.edu>
Date: Fri, 21 Aug 2009 16:04:52 +0000
Subject: [PATCH] pre-populate current location

---
 cas_auth/start.php | 80 +++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 76 insertions(+), 4 deletions(-)

diff --git a/cas_auth/start.php b/cas_auth/start.php
index effb5dd0..b2e57dbd 100644
--- a/cas_auth/start.php
+++ b/cas_auth/start.php
@@ -42,7 +42,57 @@
 	 public $unlSISPermState;
 	 public $unlSISPermZip;
 	 public $unlSISMajor;
-	 public $unlEmailAlias;	 	
+	 public $unlEmailAlias;
+	 
+	/**
+     * Takes in a string from the LDAP directory, usually formatted like:
+     *     ### ___ UNL 68588-####
+     *    Where ### is the room number, ___ = Building Abbreviation, #### zip extension
+     *
+     * @param string
+     * @return array Associative array.
+     */
+    function formatPostalAddress()
+    {
+        /* this is a faculty postal address
+            Currently of the form:
+            ### ___ UNL 68588-####
+            Where ### is the room number, ___ = Building Abbreviation, #### zip extension
+        */
+        /**
+         * We assumed that the address format is: ### ___ UNL 68588-####.
+         * Some 'fortunate' people have addresses not in this format.
+         */
+        //RLIM
+        // treat UNL as the delimiter for the streetaddress and zip
+        if (strpos($this->postalAddress,'UNL')) {
+            $addressComponent = explode('UNL', $this->postalAddress);
+        } elseif (strpos($this->postalAddress,'UNO')) {
+            $addressComponent = explode('UNO', $this->postalAddress);
+        } elseif (strpos($this->postalAddress,'Omaha')) {
+            $addressComponent = explode('Omaha', $this->postalAddress);
+        } else {
+            $addressComponent = array($this->postalAddress);
+        }
+        $address['region']         = 'NE';
+        $address['street-address'] = trim($addressComponent[0]);
+        if (isset($addressComponent[1])) {
+            $address['postal-code'] = trim($addressComponent[1]);
+        } else {
+            $address['postal-code'] = '';
+        }
+        switch (substr($address['postal-code'],0,3)) {
+            case '681':
+                $address['locality'] = 'Omaha';
+            break;
+            case '685':
+            default:
+                $address['locality'] = 'Lincoln';
+            break;
+        }
+        
+        return $address;
+    }
 	}
 			
 
@@ -78,6 +128,7 @@
 				$cas_user = getUserCas();
 				if(ldapAuthenticate( $cas_user )) {
 					system_message(elgg_echo('loginok'));
+					$cas_user = str_replace('-','_',$cas_user);
 					forward("pg/profile/unl_" . $cas_user);
 				}
 				else register_error(elgg_echo('loginerror'));
@@ -285,7 +336,9 @@
 				{
 */					
 		
-					$pf_user_info = peoplefinderServices($username);
+					// we're making this copy for use in the peoplefinderservices call later
+					// we dont want to call peoplefinderservices here since we dont need to every time a SSO user logs in
+					$casusername = $username;
 					
 					//We're going to make every UNL SSO user have an elgg profile name as such: unl_erasmussen2
 					//and not allow friends of unl who register via elgg to pick names that begin with "unl_"
@@ -305,6 +358,8 @@
 					else
 					{
 						// Valid login but user doesn't exist
+						$pf_user_info = peoplefinderServices($casusername);
+						
 						//if ($user_create)
 						//{
 						//	$name  = $ldap_user_info['firstname'];
@@ -347,9 +402,26 @@
 							try {
 								if ($user_guid = register_user($username, 'generic', $name, $email, false, 0, '', true))
 								 {
+									$thisuser = get_user($user_guid);
 									
-									// Success, credentials valid and account has been created                                
-									return login(get_user($user_guid));
+									//pre-populate profile fields with data from Peoplefinder Services
+									$address = $pf_user_info->formatPostalAddress();
+									$thisuser->profile_country = 'USA';
+									$thisuser->profile_state = $address['region'];
+									$thisuser->profile_city = $address['locality'];
+									if($address['locality'] == 'Omaha') {
+										$thisuser->longitude = -95.9;
+										$thisuser->latitude = 41.25;
+									} else { //this is going to cover Lincoln and everyone else
+										$thisuser->longitude = -96.7;
+										$thisuser->latitude = 40.82;
+									}
+										
+						
+						
+									// Success, credentials valid and account has been created   
+									$_SESSION['last_forward_from'] = $CONFIG->url . 'mod/profile/edit.php?firstlogin=yes';                             
+									return login($thisuser);
 								} else {
 									register_error(elgg_echo("registerbad"));
 								}
-- 
GitLab