示例#1
0
 /**
  * Register the user in session and with the user tables in the database
  * and then forwards them to the return url
  * 
  * @param User $user  [optional] user object
  */
 public function register(User $user = null)
 {
     // if passed in externally
     if ($user != null) {
         $this->user = $user;
     }
     // data map
     $datamap_users = new Users();
     $datamap_records = new SavedRecords();
     // if the user was previously active under a local username
     // then reassign any saved records to the new username
     $old_username = $this->request->getSessionData("username");
     $old_role = $this->request->getSessionData("role");
     if ($old_role == "local") {
         $datamap_records->reassignRecords($old_username, $this->user->username);
     }
     // add or update user in the database
     // get any values in the db not specified here and populates user
     $this->user = $datamap_users->touchUser($this->user);
     // @todo: reconcile this code with User code
     // should we just save user object in session?
     // set main properties in session
     $admins = explode(',', $this->registry->getConfig('ADMIN_USERS'));
     if (in_array($this->user->username, $admins)) {
         $this->request->setSessionData("user_admin", true);
     }
     $this->request->setSessionData("username", $this->user->username);
     $this->request->setSessionData("role", $this->role);
     // store user's additional properties in session, so they can be used by
     // controller, and included in xml for views.
     $this->request->setSessionData("user_properties", $this->user->properties());
     // groups too empty array not null please.
     $this->request->setSessionData("user_groups", $this->user->usergroups);
     // set this object's id in session
     $this->request->setSessionData("auth", $this->id);
     // now forward them to the return url
     return $this->redirectTo($this->return_url);
 }
示例#2
0
 /**
  * Update the user table to include the last date of login and any other
  * specified attributes. Creates new user if neccesary.
  * If any attributes in User are set other than
  * username, those will also be written to db over-riding anything that may
  * have been there.  Returns User filled out with information matching
  * db. 
  *
  * @param User $user
  * @return User $user
  */
 public function touchUser(User $user)
 {
     // array to pass to db updating routines. Make an array out of our
     // properties.
     $update_values = array();
     foreach ($user->properties() as $key => $value) {
         if ($value != '') {
             $update_values[":" . $key] = $value;
         }
     }
     // don't use usergroups though.
     unset($update_values[":usergroups"]);
     $update_values[":last_login"] = date("Y-m-d H:i:s");
     $this->beginTransaction();
     $strSQL = "SELECT * FROM xerxes_users WHERE username = :username";
     $arrResults = $this->select($strSQL, array(":username" => $user->username));
     if (count($arrResults) == 1) {
         // user already exists in database, so update the last_login time and
         // use any data specified in our User record to overwrite. Start
         // with what's already there, overwrite with anything provided in
         // the User object.
         $db_values = $arrResults[0];
         foreach ($db_values as $key => $value) {
             if (!(is_null($value) || is_numeric($key))) {
                 $dbKey = ":" . $key;
                 // merge with currently specified values
                 if (!array_key_exists($dbKey, $update_values)) {
                     $update_values[$dbKey] = $value;
                     $user->{$key} = $value;
                     // update user
                 }
             }
         }
         $strSQL = "UPDATE xerxes_users SET";
         foreach (array_keys($update_values) as $key) {
             $strSQL .= ' ' . str_replace(':', '', $key) . '=' . $key . ',';
         }
         $strSQL = substr($strSQL, 0, -1);
         // get last comma
         $strSQL .= " WHERE username = :username";
         $status = $this->update($strSQL, $update_values);
     } else {
         // add 'em otherwise
         $keys = array();
         foreach (array_keys($update_values) as $key) {
             $keys[] = str_replace(':', '', $key);
         }
         $strSQL = 'INSERT INTO xerxes_users (' . implode(',', $keys) . ')';
         $strSQL .= ' VALUES (' . implode(',', array_keys($update_values)) . ')';
         $status = $this->insert($strSQL, $update_values);
     }
     // let's make our group assignments match, unless the group
     // assignments have been marked null which means to keep any existing ones
     // only.
     if (is_null($user->usergroups)) {
         // fetch what's in the db and use that please.
         $fetched = $this->select("SELECT usergroup FROM xerxes_user_usergroups WHERE username = :username", array(":username" => $user->username));
         if (count($fetched)) {
             $user->usergroups = $fetched[0];
         } else {
             $user->usergroups = array();
         }
     } else {
         $status = $this->delete("DELETE FROM xerxes_user_usergroups WHERE username = :username", array(":username" => $user->username));
         foreach ($user->usergroups as $usergroup) {
             $status = $this->insert("INSERT INTO xerxes_user_usergroups (username, usergroup) " . "VALUES (:username, :usergroup)", array(":username" => $user->username, ":usergroup" => $usergroup));
         }
     }
     $this->commit();
     return $user;
 }