5 * @author Roland Haeder <webmaster@ship-simu.org>
7 * @copyright Copyright (c) 2007 - 2009 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->setLimit(1);
139 // Get a search result
140 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
142 // Set the index "solver"
143 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
145 // And finally set it
146 $this->setResultInstance($resultInstance);
150 $this->getResultInstance()->rewind();
153 if ($this->getResultInstance()->next()) {
163 * Determines wether the email exists or not
165 * @return $exists Wether the email exists
167 public function ifEmailAddressExists () {
168 // By default the email does not exist
171 // Is a previous result there?
172 if (is_null($this->getResultInstance())) {
173 // Get a UserDatabaseWrapper instance
174 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
176 // Create a search criteria
177 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
179 // Add the username as a criteria and set limit to one entry
180 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
181 $criteriaInstance->setLimit(1);
183 // Get a search result
184 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
186 // Set the index "solver"
187 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
189 // And finally set it
190 $this->setResultInstance($resultInstance);
194 $this->getResultInstance()->rewind();
197 if ($this->getResultInstance()->next()) {
201 // Is the username set?
202 if ($this->getUserName() == "") {
204 $currEntry = $this->getResultInstance()->current();
207 $this->setUserName($currEntry['username']);
216 * Checks if supplied password hash in request matches with the stored in
219 * @param $requestInstance A requestable class instance
220 * @return $matches Wether the supplied password hash matches
222 public function ifPasswordHashMatches (Requestable $requestInstance) {
223 // By default nothing matches... ;)
226 // Is a previous result there?
227 if (is_null($this->getResultInstance())) {
228 // Get a UserDatabaseWrapper instance
229 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
231 // Create a search criteria
232 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
234 // Add the username as a criteria and set limit to one entry
235 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
236 $criteriaInstance->setLimit(1);
238 // Get a search result
239 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
241 // Set the index "solver"
242 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
244 // And finally set it
245 $this->setResultInstance($resultInstance);
249 $this->getResultInstance()->rewind();
252 if ($this->getResultInstance()->find('pass_hash')) {
253 // So does the hashes match?
254 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
255 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
263 * "Getter" for user's password hash
265 * @return $passHash User's password hash from database result
267 public final function getPasswordHash () {
268 // Default is missing password hash
271 // Get a database entry
272 $entry = $this->getDatabaseEntry();
274 // Is the password hash there?
275 if (isset($entry['pass_hash'])) {
277 $passHash = $entry['pass_hash'];
280 // And return the hash
285 * Getter for primary key value
287 * @return $primaryValue Value of the primary key based on database type
289 public final function getPrimaryKey () {
290 // Get a user database wrapper
291 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
293 // Get the primary key back from the wrapper
294 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
297 $primaryValue = $this->getField($primaryKey);
300 return $primaryValue;
304 * Updates a given field with new value
306 * @param $fieldName Field to update
307 * @param $fieldValue New value to store
309 * @throws DatabaseUpdateSupportException If this class does not support database updates
310 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
312 public function updateDatabaseField ($fieldName, $fieldValue) {
313 // Is updating database fields allowed by interface?
314 if (!$this instanceof Updateable) {
315 // Update not supported!
316 throw new DatabaseUpdateSupportException($this, self::EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED);
319 // Get a critieria instance
320 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
322 // Add search criteria
323 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
324 $searchInstance->setLimit(1);
326 // Now get another criteria
327 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
329 // Add criteria entry which we shall update
330 $updateInstance->addCriteria($fieldName, $fieldValue);
332 // Add the search criteria for searching for the right entry
333 $updateInstance->setSearchInstance($searchInstance);
335 // Set wrapper class name
336 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
338 // Remember the update in database result
339 $this->getResultInstance()->add2UpdateQueue($updateInstance);