5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 implements Updateable {
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;
29 const EXCEPTION_USER_IS_GUEST = 0x153;
32 * Username of current user
34 private $userName = '';
37 * User id of current user
42 * Email of current user
47 * Protected constructor
49 * @param $className Name of the class
52 protected function __construct ($className) {
53 // Call parent constructor
54 parent::__construct($className);
60 * @param $userName The username to set
63 public final function setUserName ($userName) {
64 $this->userName = (string) $userName;
70 * @return $userName The username to get
72 public final function getUserName () {
73 return $this->userName;
79 * @param $userId The user id to set
81 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
83 public final function setUserId ($userId) {
84 $this->userId = $userId;
90 * @return $userId The user id to get
92 public final function getUserId () {
99 * @param $email The email to set
102 protected final function setEmail ($email) {
103 $this->email = (string) $email;
109 * @return $email The email to get
111 public final function getEmail () {
116 * Determines whether the username exists or not
118 * @return $exists Whether the username exists
120 public function ifUsernameExists () {
121 // By default the username does not exist
124 // Is a previous result there?
125 if (!$this->getResultInstance() instanceof SearchableResult) {
126 // Get a UserDatabaseWrapper instance
127 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
129 // Create a search criteria
130 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
132 // Add the username as a criteria and set limit to one entry
133 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
134 $criteriaInstance->setLimit(1);
136 // Get a search result
137 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
139 // Set the index "solver"
140 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
142 // And finally set it
143 $this->setResultInstance($resultInstance);
147 $this->getResultInstance()->rewind();
150 if ($this->getResultInstance()->next()) {
160 * Determines whether the email exists or not
162 * @return $exists Whether the email exists
164 public function ifEmailAddressExists () {
165 // By default the email does not exist
168 // Is a previous result there?
169 if (!$this->getResultInstance() instanceof SearchableResult) {
170 // Get a UserDatabaseWrapper instance
171 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
173 // Create a search criteria
174 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
176 // Add the username as a criteria and set limit to one entry
177 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
178 $criteriaInstance->setLimit(1);
180 // Get a search result
181 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
183 // Set the index "solver"
184 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
186 // And finally set it
187 $this->setResultInstance($resultInstance);
191 $this->getResultInstance()->rewind();
194 if ($this->getResultInstance()->next()) {
198 // Is the username set?
199 if ($this->getUserName() == '') {
201 $currEntry = $this->getResultInstance()->current();
204 $this->setUserName($currEntry['username']);
213 * Checks if supplied password hash in request matches with the stored in
216 * @param $requestInstance A requestable class instance
217 * @return $matches Whether the supplied password hash matches
219 public function ifPasswordHashMatches (Requestable $requestInstance) {
220 // By default nothing matches... ;)
223 // Is a previous result there?
224 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
225 // Get a UserDatabaseWrapper instance
226 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
228 // Create a search criteria
229 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
231 // Add the username as a criteria and set limit to one entry
232 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
233 $criteriaInstance->setLimit(1);
235 // Get a search result
236 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
238 // Set the index "solver"
239 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
241 // And finally set it
242 $this->setResultInstance($resultInstance);
245 // Rewind it and advance to first entry
246 $this->getResultInstance()->rewind();
248 // This call set the result instance to a clean state
249 $this->getResultInstance()->next();
252 if ($this->getResultInstance()->find('pass_hash')) {
253 // So does the hashes match?
254 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
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 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
311 public function updateDatabaseField ($fieldName, $fieldValue) {
312 // Get a critieria instance
313 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
315 // Add search criteria
316 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
317 $searchInstance->setLimit(1);
319 // Now get another criteria
320 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
322 // Add criteria entry which we shall update
323 $updateInstance->addCriteria($fieldName, $fieldValue);
325 // Add the search criteria for searching for the right entry
326 $updateInstance->setSearchInstance($searchInstance);
328 // Set wrapper class name
329 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
331 // Remember the update in database result
332 $this->getResultInstance()->add2UpdateQueue($updateInstance);
336 * Checks whether the user status is 'confirmed'
338 * @return $isConfirmed Whether the user status is 'confirmed'
340 public function isConfirmed () {
342 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
349 * Checks whether the user status is 'guest'
351 * @return $isGuest Whether the user status is 'guest'
353 public function isGuest () {
355 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));