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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2020 Core Developer Team
19 * @license GNU GPL 3.0 or any newer version
20 * @link http://www.shipsimu.org
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
35 abstract class BaseUser extends BaseFrameworkSystem implements Updateable {
36 // Exception constances
37 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
38 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
39 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
40 const EXCEPTION_USER_IS_GUEST = 0x153;
43 * Username of current user
45 private $userName = '';
48 * User id of current user
53 * Email of current user
58 * Protected constructor
60 * @param $className Name of the class
63 protected function __construct ($className) {
64 // Call parent constructor
65 parent::__construct($className);
71 * @param $userName The username to set
74 public final function setUserName ($userName) {
75 $this->userName = (string) $userName;
81 * @return $userName The username to get
83 public final function getUserName () {
84 return $this->userName;
90 * @param $userId The user id to set
92 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
94 public final function setUserId ($userId) {
95 $this->userId = $userId;
101 * @return $userId The user id to get
103 public final function getUserId () {
104 return $this->userId;
110 * @param $email The email to set
113 protected final function setEmail ($email) {
114 $this->email = (string) $email;
120 * @return $email The email to get
122 public final function getEmail () {
127 * Determines whether the username exists or not
129 * @return $exists Whether the username exists
131 public function ifUsernameExists () {
132 // By default the username does not exist
135 // Is a previous result there?
136 if (!$this->getResultInstance() instanceof SearchableResult) {
137 // Get a UserDatabaseWrapper instance
138 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
140 // Create a search criteria
141 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
143 // Add the username as a criteria and set limit to one entry
144 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
145 $criteriaInstance->setLimit(1);
147 // Get a search result
148 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
150 // Set the index "solver"
151 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
153 // And finally set it
154 $this->setResultInstance($resultInstance);
158 $this->getResultInstance()->rewind();
161 if ($this->getResultInstance()->next()) {
171 * Determines whether the email exists or not
173 * @return $exists Whether the email exists
175 public function ifEmailAddressExists () {
176 // By default the email does not exist
179 // Is a previous result there?
180 if (!$this->getResultInstance() instanceof SearchableResult) {
181 // Get a UserDatabaseWrapper instance
182 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
184 // Create a search criteria
185 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
187 // Add the username as a criteria and set limit to one entry
188 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
189 $criteriaInstance->setLimit(1);
191 // Get a search result
192 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
194 // Set the index "solver"
195 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
197 // And finally set it
198 $this->setResultInstance($resultInstance);
202 $this->getResultInstance()->rewind();
205 if ($this->getResultInstance()->next()) {
209 // Is the username set?
210 if ($this->getUserName() == '') {
212 $currEntry = $this->getResultInstance()->current();
215 $this->setUserName($currEntry['username']);
224 * Checks if supplied password hash in request matches with the stored in
227 * @param $requestInstance A Requestable class instance
228 * @return $matches Whether the supplied password hash matches
230 public function ifPasswordHashMatches (Requestable $requestInstance) {
231 // By default nothing matches... ;)
234 // Is a previous result there?
235 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
236 // Get a UserDatabaseWrapper instance
237 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
239 // Create a search criteria
240 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
242 // Add the username as a criteria and set limit to one entry
243 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
244 $criteriaInstance->setLimit(1);
246 // Get a search result
247 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
249 // Set the index "solver"
250 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
252 // And finally set it
253 $this->setResultInstance($resultInstance);
256 // Rewind it and advance to first entry
257 $this->getResultInstance()->rewind();
259 // This call set the result instance to a clean state
260 $this->getResultInstance()->next();
263 if ($this->getResultInstance()->find('pass_hash')) {
264 // So does the hashes match?
265 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
266 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
274 * "Getter" for user's password hash
276 * @return $passHash User's password hash from database result
278 public final function getPasswordHash () {
279 // Default is missing password hash
282 // Get a database entry
283 $entry = $this->getDatabaseEntry();
285 // Is the password hash there?
286 if (isset($entry['pass_hash'])) {
288 $passHash = $entry['pass_hash'];
291 // And return the hash
296 * Getter for primary key value
298 * @return $primaryValue Value of the primary key based on database type
300 public final function getPrimaryKey () {
301 // Get a user database wrapper
302 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
304 // Get the primary key back from the wrapper
305 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
308 $primaryValue = $this->getField($primaryKey);
311 return $primaryValue;
315 * Updates a given field with new value
317 * @param $fieldName Field to update
318 * @param $fieldValue New value to store
320 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
322 public function updateDatabaseField ($fieldName, $fieldValue) {
323 // Get a critieria instance
324 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
326 // Add search criteria
327 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
328 $searchInstance->setLimit(1);
330 // Now get another criteria
331 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
333 // Add criteria entry which we shall update
334 $updateInstance->addCriteria($fieldName, $fieldValue);
336 // Add the search criteria for searching for the right entry
337 $updateInstance->setSearchInstance($searchInstance);
339 // Set wrapper class name
340 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
342 // Remember the update in database result
343 $this->getResultInstance()->add2UpdateQueue($updateInstance);
347 * Checks whether the user status is 'confirmed'
349 * @return $isConfirmed Whether the user status is 'confirmed'
351 public function isConfirmed () {
353 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
360 * Checks whether the user status is 'guest'
362 * @return $isGuest Whether the user status is 'guest'
364 public function isGuest () {
366 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));