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 BaseUser extends BaseFrameworkSystem {
25 // Exception constances
26 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
27 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
28 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
31 * Username of current user
33 private $userName = "";
36 * User id of current user
41 * Email of current user
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct ($className) {
52 // Call parent constructor
53 parent::__construct($className);
56 $this->removeNumberFormaters();
57 $this->removeSystemArray();
63 * @param $userName The username to set
66 public final function setUserName ($userName) {
67 $this->userName = (string) $userName;
73 * @return $userName The username to get
75 public final function getUserName () {
76 return $this->userName;
82 * @param $userId The user id to set
84 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
86 public final function setUserId ($userId) {
87 $this->userId = $userId;
93 * @return $userId The user id to get
95 public final function getUserId () {
102 * @param $email The email to set
105 protected final function setEmail ($email) {
106 $this->email = (string) $email;
112 * @return $email The email to get
114 public final function getEmail () {
119 * Determines wether the username exists or not
121 * @return $exists Wether the username exists
123 public function ifUsernameExists () {
124 // By default the username does not exist
127 // Is a previous result there?
128 if (is_null($this->getResultInstance())) {
129 // Get a UserDatabaseWrapper instance
130 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
132 // Create a search criteria
133 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
135 // Add the username as a criteria and set limit to one entry
136 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
137 $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
138 $criteriaInstance->setLimit(1);
140 // Get a search result
141 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
144 $this->getResultInstance()->rewind();
148 if ($this->getResultInstance()->next()) {
158 * Determines wether the email exists or not
160 * @return $exists Wether the email exists
162 public function ifEmailAddressExists () {
163 // By default the email does not exist
166 // Is a previous result there?
167 if (is_null($this->getResultInstance())) {
168 // Get a UserDatabaseWrapper instance
169 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
171 // Create a search criteria
172 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
174 // Add the username as a criteria and set limit to one entry
175 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
176 $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
177 $criteriaInstance->setLimit(1);
179 // Get a search resultInstance
180 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
183 $this->getResultInstance()->rewind();
187 if ($this->getResultInstance()->next()) {
191 // Is the username set?
192 if ($this->getUserName() == "") {
194 $currEntry = $this->getResultInstance()->current();
197 $this->setUserName($currEntry['username']);
206 * Checks if the supplied password hash in request matches with the stored
209 * @param $requestInstance A requestable class instance
210 * @return $matches Wether the supplied password hash matches
212 public function ifPasswordHashMatches (Requestable $requestInstance) {
213 // By default nothing matches... ;)
216 // Get a UserDatabaseWrapper instance
217 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
219 // Create a search criteria
220 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
222 // Add the username as a criteria and set limit to one entry
223 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
224 $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
225 $criteriaInstance->setLimit(1);
227 // Get a search resultInstance
228 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
231 if ($this->getResultInstance()->next()) {
232 // Get the current entry (can only be one!)
233 $entry = $this->getResultInstance()->current();
235 // So does the hashes match?
236 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
237 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
245 * "Getter" for user's password hash
247 * @return $passHash User's password hash from database result
249 public final function getPasswordHash () {
250 // Default is missing password hash
253 // Get a database entry
254 $entry = $this->getDatabaseEntry();
256 // Is the password hash there?
257 if (isset($entry['pass_hash'])) {
259 $passHash = $entry['pass_hash'];
262 // And return the hash
267 * Getter for primary key value
269 * @return $primaryValue Value of the primary key based on database type
271 public final function getPrimaryKey () {
272 // Get a user database wrapper
273 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
275 // Get the primary key back from the wrapper
276 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
279 $primaryValue = $this->getField($primaryKey);
282 return $primaryValue;
286 * Updates a given field with new value
288 * @param $fieldName Field to update
289 * @param $fieldValue New value to store
291 * @throws DatabaseUpdateSupportException If this class does not support database updates
293 public function updateDatabaseField ($fieldName, $fieldValue) {
294 // Is updating database fields allowed by interface?
295 if (!$this instanceof Updateable) {
296 // Update not supported!
297 throw new DatabaseUpdateSupportException($this, self::EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED);
300 // Get a critieria instance
301 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
303 // Add search criteria
304 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
305 $searchInstance->setLimit(1);
307 // Now get another criteria
308 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
310 // And add our both entries
311 $updateInstance->addCriteria($fieldName, $fieldValue);
313 // Add the search criteria for searching for the right entry
314 $updateInstance->setSearchInstance($searchInstance);
316 // Remember the update in database result
317 $this->getResultInstance()->add2UpdateQueue($updateInstance);