3 namespace CoreFramework\User;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Request\Requestable;
11 * A general user class
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class BaseUser extends BaseFrameworkSystem implements Updateable {
33 // Exception constances
34 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
35 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
36 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
37 const EXCEPTION_USER_IS_GUEST = 0x153;
40 * Username of current user
42 private $userName = '';
45 * User id of current user
50 * Email of current user
55 * Protected constructor
57 * @param $className Name of the class
60 protected function __construct ($className) {
61 // Call parent constructor
62 parent::__construct($className);
68 * @param $userName The username to set
71 public final function setUserName ($userName) {
72 $this->userName = (string) $userName;
78 * @return $userName The username to get
80 public final function getUserName () {
81 return $this->userName;
87 * @param $userId The user id to set
89 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
91 public final function setUserId ($userId) {
92 $this->userId = $userId;
98 * @return $userId The user id to get
100 public final function getUserId () {
101 return $this->userId;
107 * @param $email The email to set
110 protected final function setEmail ($email) {
111 $this->email = (string) $email;
117 * @return $email The email to get
119 public final function getEmail () {
124 * Determines whether the username exists or not
126 * @return $exists Whether the username exists
128 public function ifUsernameExists () {
129 // By default the username does not exist
132 // Is a previous result there?
133 if (!$this->getResultInstance() instanceof SearchableResult) {
134 // Get a UserDatabaseWrapper instance
135 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
137 // Create a search criteria
138 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
140 // Add the username as a criteria and set limit to one entry
141 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
142 $criteriaInstance->setLimit(1);
144 // Get a search result
145 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
147 // Set the index "solver"
148 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
150 // And finally set it
151 $this->setResultInstance($resultInstance);
155 $this->getResultInstance()->rewind();
158 if ($this->getResultInstance()->next()) {
168 * Determines whether the email exists or not
170 * @return $exists Whether the email exists
172 public function ifEmailAddressExists () {
173 // By default the email does not exist
176 // Is a previous result there?
177 if (!$this->getResultInstance() instanceof SearchableResult) {
178 // Get a UserDatabaseWrapper instance
179 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
181 // Create a search criteria
182 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
184 // Add the username as a criteria and set limit to one entry
185 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
186 $criteriaInstance->setLimit(1);
188 // Get a search result
189 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
191 // Set the index "solver"
192 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
194 // And finally set it
195 $this->setResultInstance($resultInstance);
199 $this->getResultInstance()->rewind();
202 if ($this->getResultInstance()->next()) {
206 // Is the username set?
207 if ($this->getUserName() == '') {
209 $currEntry = $this->getResultInstance()->current();
212 $this->setUserName($currEntry['username']);
221 * Checks if supplied password hash in request matches with the stored in
224 * @param $requestInstance A Requestable class instance
225 * @return $matches Whether the supplied password hash matches
227 public function ifPasswordHashMatches (Requestable $requestInstance) {
228 // By default nothing matches... ;)
231 // Is a previous result there?
232 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
233 // Get a UserDatabaseWrapper instance
234 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
236 // Create a search criteria
237 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
239 // Add the username as a criteria and set limit to one entry
240 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
241 $criteriaInstance->setLimit(1);
243 // Get a search result
244 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
246 // Set the index "solver"
247 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
249 // And finally set it
250 $this->setResultInstance($resultInstance);
253 // Rewind it and advance to first entry
254 $this->getResultInstance()->rewind();
256 // This call set the result instance to a clean state
257 $this->getResultInstance()->next();
260 if ($this->getResultInstance()->find('pass_hash')) {
261 // So does the hashes match?
262 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
263 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
271 * "Getter" for user's password hash
273 * @return $passHash User's password hash from database result
275 public final function getPasswordHash () {
276 // Default is missing password hash
279 // Get a database entry
280 $entry = $this->getDatabaseEntry();
282 // Is the password hash there?
283 if (isset($entry['pass_hash'])) {
285 $passHash = $entry['pass_hash'];
288 // And return the hash
293 * Getter for primary key value
295 * @return $primaryValue Value of the primary key based on database type
297 public final function getPrimaryKey () {
298 // Get a user database wrapper
299 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
301 // Get the primary key back from the wrapper
302 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
305 $primaryValue = $this->getField($primaryKey);
308 return $primaryValue;
312 * Updates a given field with new value
314 * @param $fieldName Field to update
315 * @param $fieldValue New value to store
317 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
319 public function updateDatabaseField ($fieldName, $fieldValue) {
320 // Get a critieria instance
321 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
323 // Add search criteria
324 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
325 $searchInstance->setLimit(1);
327 // Now get another criteria
328 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
330 // Add criteria entry which we shall update
331 $updateInstance->addCriteria($fieldName, $fieldValue);
333 // Add the search criteria for searching for the right entry
334 $updateInstance->setSearchInstance($searchInstance);
336 // Set wrapper class name
337 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
339 // Remember the update in database result
340 $this->getResultInstance()->add2UpdateQueue($updateInstance);
344 * Checks whether the user status is 'confirmed'
346 * @return $isConfirmed Whether the user status is 'confirmed'
348 public function isConfirmed () {
350 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
357 * Checks whether the user status is 'guest'
359 * @return $isGuest Whether the user status is 'guest'
361 public function isGuest () {
363 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));