3 * A generic class for handling users
5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, this is free software
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.ship-simu.org
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 class User extends BaseUser implements ManageableUser, Registerable, Updateable {
26 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
27 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
28 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
31 * Protected constructor
33 * @param $className Name of the class
36 protected function __construct ($className = "") {
37 // Is the class name empty? Then this is not a specialized user class
38 if (empty($className)) $className = __CLASS__;
40 // Call parent constructor
41 parent::__construct($className);
43 // Set part description
44 $this->setObjectDescription("Generic user class");
46 // Create unique ID number
47 $this->generateUniqueId();
51 * Destructor to always flush updates
55 public function __destruct () {
56 // Flush any updated entries to the database
57 $this->flushPendingUpdates();
59 // Call parent destructor
64 * Creates an instance of this user class by a provided username. This
65 * factory method will check if the username is already taken and if not
66 * so it will throw an exception.
68 * @param $userName Username we need a class instance for
69 * @return $userInstance An instance of this user class
70 * @throws UsernameMissingException If the username does not exist
72 public final static function createUserByUsername ($userName) {
74 $userInstance = new User();
77 $userInstance->setUserName($userName);
79 // Check if the username exists
80 if (!$userInstance->ifUsernameExists()) {
81 // Throw an exception here
82 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
85 // Return the instance
90 * Creates an instance of this user class by a provided email address. This
91 * factory method will not check if the email address is there.
93 * @param $email Email address of the user
94 * @return $userInstance An instance of this user class
96 public final static function createUserByEmail ($email) {
98 $userInstance = new User();
101 $userInstance->setEmail($email);
103 // Return the instance
104 return $userInstance;
108 * Creates a user by a given request instance
110 * @param $requestInstance An instance of a Requestable class
111 * @return $userInstance An instance of this user class
113 public final static function createUserByRequest (Requestable $requestInstance) {
114 // Determine if by email or username
115 if (!is_null($requestInstance->getRequestElement('username'))) {
117 $userInstance = self::createUserByUserName($requestInstance->getRequestElement('username'));
118 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
120 $userInstance = self::createUserByEmail($requestInstance->getRequestElement('email'));
123 $userInstance = new User();
124 $userInstance->partialStub("We need to add more ways of creating user classes here.");
125 $userInstance->debugBackTrace();
129 // Return the prepared instance
130 return $userInstance;
134 * Adds data for later complete update
136 * @param $column Column we want to update
137 * @param $value New value to store in database
141 public function addUpdateData ($column, $value) {
142 $this->deprecatedMethod("Please use updateDatabaseField() instead!");
143 $this->updateDatabaseField($column, $value);
147 * Updates the last activity timestamp and last performed action in the
148 * database result. You should call flushPendingUpdates() to flush these updates
149 * to the database layer.
151 * @param $requestInstance A requestable class instance
154 public function updateLastActivity (Requestable $requestInstance) {
156 $lastAction = $requestInstance->getRequestElement('action');
158 // If there is no action use the default on
159 if (is_null($lastAction)) {
160 $lastAction = $this->getConfigInstance()->readConfig('login_default_action');
163 // Get a critieria instance
164 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
166 // Add search criteria
167 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
168 $searchInstance->setLimit(1);
170 // Now get another criteria
171 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
173 // And add our both entries
174 $updateInstance->addCriteria('last_activity', date("Y-m-d H:i:s", time()));
175 $updateInstance->addCriteria('last_action', $lastAction);
177 // Add the search criteria for searching for the right entry
178 $updateInstance->setSearchInstance($searchInstance);
180 // Remember the update in database result
181 $this->getResultInstance()->add2UpdateQueue($updateInstance);
185 * Flushs all pending updates to the database layer
189 public function flushPendingUpdates () {
190 // Do we have data to update?
191 if ($this->getResultInstance()->ifDataNeedsFlush()) {
192 // Get a database wrapper
193 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
195 // Yes, then send the whole result to the database layer
196 $wrapperInstance->doUpdateByResult($this->getResultInstance());