3 namespace Org\Mxchange\CoreFramework\User;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Database\Frontend\User\UserDatabaseWrapper;
8 use Org\Mxchange\CoreFramework\Database\Updateable;
9 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\Request\Requestable;
12 use Org\Mxchange\CoreFramework\Result\Search\SearchableResult;
15 * A general user class
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 abstract class BaseUser extends BaseFrameworkSystem implements Updateable {
37 // Exception constances
38 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
39 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
40 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
41 const EXCEPTION_USER_IS_GUEST = 0x153;
44 * Username of current user
46 private $userName = '';
49 * User id of current user
54 * Email of current user
59 * Protected constructor
61 * @param $className Name of the class
64 protected function __construct (string $className) {
65 // Call parent constructor
66 parent::__construct($className);
72 * @param $userName The username to set
75 public final function setUserName ($userName) {
76 $this->userName = (string) $userName;
82 * @return $userName The username to get
84 public final function getUserName () {
85 return $this->userName;
91 * @param $userId The user id to set
93 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
95 public final function setUserId ($userId) {
96 $this->userId = $userId;
102 * @return $userId The user id to get
104 public final function getUserId () {
105 return $this->userId;
111 * @param $email The email to set
114 protected final function setEmail ($email) {
115 $this->email = (string) $email;
121 * @return $email The email to get
123 public final function getEmail () {
128 * Determines whether the username exists or not
130 * @return $exists Whether the username exists
132 public function ifUsernameExists () {
133 // By default the username does not exist
136 // Is a previous result there?
137 if (!$this->getResultInstance() instanceof SearchableResult) {
138 // Get a UserDatabaseWrapper instance
139 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
141 // Create a search criteria
142 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
144 // Add the username as a criteria and set limit to one entry
145 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
146 $criteriaInstance->setLimit(1);
148 // Get a search result
149 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
151 // Set the index "solver"
152 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
154 // And finally set it
155 $this->setResultInstance($resultInstance);
159 $this->getResultInstance()->rewind();
162 if ($this->getResultInstance()->next()) {
172 * Determines whether the email exists or not
174 * @return $exists Whether the email exists
176 public function ifEmailAddressExists () {
177 // By default the email does not exist
180 // Is a previous result there?
181 if (!$this->getResultInstance() instanceof SearchableResult) {
182 // Get a UserDatabaseWrapper instance
183 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
185 // Create a search criteria
186 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
188 // Add the username as a criteria and set limit to one entry
189 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
190 $criteriaInstance->setLimit(1);
192 // Get a search result
193 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
195 // Set the index "solver"
196 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
198 // And finally set it
199 $this->setResultInstance($resultInstance);
203 $this->getResultInstance()->rewind();
206 if ($this->getResultInstance()->next()) {
210 // Is the username set?
211 if ($this->getUserName() == '') {
213 $currEntry = $this->getResultInstance()->current();
216 $this->setUserName($currEntry['username']);
225 * Checks if supplied password hash in request matches with the stored in
228 * @param $requestInstance A Requestable class instance
229 * @return $matches Whether the supplied password hash matches
231 public function ifPasswordHashMatches (Requestable $requestInstance) {
232 // By default nothing matches... ;)
235 // Is a previous result there?
236 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
237 // Get a UserDatabaseWrapper instance
238 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
240 // Create a search criteria
241 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
243 // Add the username as a criteria and set limit to one entry
244 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
245 $criteriaInstance->setLimit(1);
247 // Get a search result
248 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
250 // Set the index "solver"
251 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
253 // And finally set it
254 $this->setResultInstance($resultInstance);
257 // Rewind it and advance to first entry
258 $this->getResultInstance()->rewind();
260 // This call set the result instance to a clean state
261 $this->getResultInstance()->next();
264 if ($this->getResultInstance()->find('pass_hash')) {
265 // So does the hashes match?
266 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
267 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
275 * "Getter" for user's password hash
277 * @return $passHash User's password hash from database result
279 public final function getPasswordHash () {
280 // Default is missing password hash
283 // Get a database entry
284 $entry = $this->getDatabaseEntry();
286 // Is the password hash there?
287 if (isset($entry['pass_hash'])) {
289 $passHash = $entry['pass_hash'];
292 // And return the hash
297 * Getter for primary key value
299 * @return $primaryValue Value of the primary key based on database type
301 public final function getPrimaryKey () {
302 // Get a user database wrapper
303 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
305 // Get the primary key back from the wrapper
306 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
309 $primaryValue = $this->getField($primaryKey);
312 return $primaryValue;
316 * Updates a given field with new value
318 * @param $fieldName Field to update
319 * @param $fieldValue New value to store
321 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
323 public function updateDatabaseField (string $fieldName, $fieldValue) {
324 // Get a critieria instance
325 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
327 // Add search criteria
328 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
329 $searchInstance->setLimit(1);
331 // Now get another criteria
332 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
334 // Add criteria entry which we shall update
335 $updateInstance->addCriteria($fieldName, $fieldValue);
337 // Add the search criteria for searching for the right entry
338 $updateInstance->setSearchInstance($searchInstance);
340 // Set wrapper class name
341 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
343 // Remember the update in database result
344 $this->getResultInstance()->add2UpdateQueue($updateInstance);
348 * Checks whether the user status is 'confirmed'
350 * @return $isConfirmed Whether the user status is 'confirmed'
352 public function isConfirmed () {
354 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('user_status_confirmed'));
361 * Checks whether the user status is 'guest'
363 * @return $isGuest Whether the user status is 'guest'
365 public function isGuest () {
367 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('user_status_guest'));