3 namespace Org\Mxchange\CoreFramework\User;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Database\Frontend\User\UserDatabaseWrapper;
7 use Org\Mxchange\CoreFramework\Database\Updateable;
8 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
10 use Org\Mxchange\CoreFramework\Request\Requestable;
11 use Org\Mxchange\CoreFramework\Result\Search\SearchableResult;
14 * A general user class
16 * @author Roland Haeder <webmaster@shipsimu.org>
18 <<<<<<< HEAD:framework/main/classes/user/class_BaseUser.php
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
22 >>>>>>> Some updates::inc/main/classes/user/class_BaseUser.php
23 * @license GNU GPL 3.0 or any newer version
24 * @link http://www.shipsimu.org
26 * This program is free software: you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation, either version 3 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program. If not, see <http://www.gnu.org/licenses/>.
39 abstract class BaseUser extends BaseFrameworkSystem implements Updateable {
40 // Exception constances
41 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
42 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
43 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
44 const EXCEPTION_USER_IS_GUEST = 0x153;
47 * Username of current user
49 private $userName = '';
52 * User id of current user
57 * Email of current user
62 * Protected constructor
64 * @param $className Name of the class
67 protected function __construct ($className) {
68 // Call parent constructor
69 parent::__construct($className);
75 * @param $userName The username to set
78 public final function setUserName ($userName) {
79 $this->userName = (string) $userName;
85 * @return $userName The username to get
87 public final function getUserName () {
88 return $this->userName;
94 * @param $userId The user id to set
96 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
98 public final function setUserId ($userId) {
99 $this->userId = $userId;
105 * @return $userId The user id to get
107 public final function getUserId () {
108 return $this->userId;
114 * @param $email The email to set
117 protected final function setEmail ($email) {
118 $this->email = (string) $email;
124 * @return $email The email to get
126 public final function getEmail () {
131 * Determines whether the username exists or not
133 * @return $exists Whether the username exists
135 public function ifUsernameExists () {
136 // By default the username does not exist
139 // Is a previous result there?
140 if (!$this->getResultInstance() instanceof SearchableResult) {
141 // Get a UserDatabaseWrapper instance
142 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
144 // Create a search criteria
145 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
147 // Add the username as a criteria and set limit to one entry
148 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
149 $criteriaInstance->setLimit(1);
151 // Get a search result
152 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
154 // Set the index "solver"
155 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
157 // And finally set it
158 $this->setResultInstance($resultInstance);
162 $this->getResultInstance()->rewind();
165 if ($this->getResultInstance()->next()) {
175 * Determines whether the email exists or not
177 * @return $exists Whether the email exists
179 public function ifEmailAddressExists () {
180 // By default the email does not exist
183 // Is a previous result there?
184 if (!$this->getResultInstance() instanceof SearchableResult) {
185 // Get a UserDatabaseWrapper instance
186 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
188 // Create a search criteria
189 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
191 // Add the username as a criteria and set limit to one entry
192 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
193 $criteriaInstance->setLimit(1);
195 // Get a search result
196 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
198 // Set the index "solver"
199 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
201 // And finally set it
202 $this->setResultInstance($resultInstance);
206 $this->getResultInstance()->rewind();
209 if ($this->getResultInstance()->next()) {
213 // Is the username set?
214 if ($this->getUserName() == '') {
216 $currEntry = $this->getResultInstance()->current();
219 $this->setUserName($currEntry['username']);
228 * Checks if supplied password hash in request matches with the stored in
231 * @param $requestInstance A Requestable class instance
232 * @return $matches Whether the supplied password hash matches
234 public function ifPasswordHashMatches (Requestable $requestInstance) {
235 // By default nothing matches... ;)
238 // Is a previous result there?
239 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
240 // Get a UserDatabaseWrapper instance
241 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
243 // Create a search criteria
244 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
246 // Add the username as a criteria and set limit to one entry
247 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
248 $criteriaInstance->setLimit(1);
250 // Get a search result
251 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
253 // Set the index "solver"
254 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
256 // And finally set it
257 $this->setResultInstance($resultInstance);
260 // Rewind it and advance to first entry
261 $this->getResultInstance()->rewind();
263 // This call set the result instance to a clean state
264 $this->getResultInstance()->next();
267 if ($this->getResultInstance()->find('pass_hash')) {
268 // So does the hashes match?
269 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
270 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
278 * "Getter" for user's password hash
280 * @return $passHash User's password hash from database result
282 public final function getPasswordHash () {
283 // Default is missing password hash
286 // Get a database entry
287 $entry = $this->getDatabaseEntry();
289 // Is the password hash there?
290 if (isset($entry['pass_hash'])) {
292 $passHash = $entry['pass_hash'];
295 // And return the hash
300 * Getter for primary key value
302 * @return $primaryValue Value of the primary key based on database type
304 public final function getPrimaryKey () {
305 // Get a user database wrapper
306 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
308 // Get the primary key back from the wrapper
309 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
312 $primaryValue = $this->getField($primaryKey);
315 return $primaryValue;
319 * Updates a given field with new value
321 * @param $fieldName Field to update
322 * @param $fieldValue New value to store
324 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
326 public function updateDatabaseField ($fieldName, $fieldValue) {
327 // Get a critieria instance
328 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
330 // Add search criteria
331 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
332 $searchInstance->setLimit(1);
334 // Now get another criteria
335 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
337 // Add criteria entry which we shall update
338 $updateInstance->addCriteria($fieldName, $fieldValue);
340 // Add the search criteria for searching for the right entry
341 $updateInstance->setSearchInstance($searchInstance);
343 // Set wrapper class name
344 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
346 // Remember the update in database result
347 $this->getResultInstance()->add2UpdateQueue($updateInstance);
351 * Checks whether the user status is 'confirmed'
353 * @return $isConfirmed Whether the user status is 'confirmed'
355 public function isConfirmed () {
357 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
364 * Checks whether the user status is 'guest'
366 * @return $isGuest Whether the user status is 'guest'
368 public function isGuest () {
370 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));