]> git.mxchange.org Git - core.git/blob - framework/main/classes/user/class_BaseUser.php
Continued:
[core.git] / framework / main / classes / user / class_BaseUser.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\User;
4
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;
13
14 /**
15  * A general user class
16  *
17  * @author              Roland Haeder <webmaster@shipsimu.org>
18  * @version             0.0.0
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
22  *
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.
27  *
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.
32  *
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/>.
35  */
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;
42
43         /**
44          * Username of current user
45          */
46         private $userName = '';
47
48         /**
49          * User id of current user
50          */
51         private $userId = 0;
52
53         /**
54          * Email of current user
55          */
56         private $email = '';
57
58         /**
59          * Protected constructor
60          *
61          * @param       $className      Name of the class
62          * @return      void
63          */
64         protected function __construct (string $className) {
65                 // Call parent constructor
66                 parent::__construct($className);
67         }
68
69         /**
70          * Setter for username
71          *
72          * @param       $userName       The username to set
73          * @return      void
74          */
75         public final function setUserName ($userName) {
76                 $this->userName = (string) $userName;
77         }
78
79         /**
80          * Getter for username
81          *
82          * @return      $userName       The username to get
83          */
84         public final function getUserName () {
85                 return $this->userName;
86         }
87
88         /**
89          * Setter for user id
90          *
91          * @param       $userId         The user id to set
92          * @return      void
93          * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
94          */
95         public final function setUserId ($userId) {
96                 $this->userId = $userId;
97         }
98
99         /**
100          * Getter for user id
101          *
102          * @return      $userId The user id to get
103          */
104         public final function getUserId () {
105                 return $this->userId;
106         }
107
108         /**
109          * Setter for email
110          *
111          * @param       $email  The email to set
112          * @return      void
113          */
114         protected final function setEmail ($email) {
115                 $this->email = (string) $email;
116         }
117
118         /**
119          * Getter for email
120          *
121          * @return      $email  The email to get
122          */
123         public final function getEmail () {
124                 return $this->email;
125         }
126
127         /**
128          * Determines whether the username exists or not
129          *
130          * @return      $exists         Whether the username exists
131          */
132         public function ifUsernameExists () {
133                 // By default the username does not exist
134                 $exists = false;
135
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');
140
141                         // Create a search criteria
142                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
143
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);
147
148                         // Get a search result
149                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
150
151                         // Set the index "solver"
152                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
153
154                         // And finally set it
155                         $this->setResultInstance($resultInstance);
156                 } // END - if
157
158                 // Rewind it
159                 $this->getResultInstance()->rewind();
160
161                 // Search for it
162                 if ($this->getResultInstance()->next()) {
163                         // Entry found
164                         $exists = true;
165                 } // END - if
166
167                 // Return the status
168                 return $exists;
169         }
170
171         /**
172          * Determines whether the email exists or not
173          *
174          * @return      $exists         Whether the email exists
175          */
176         public function ifEmailAddressExists () {
177                 // By default the email does not exist
178                 $exists = false;
179
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');
184
185                         // Create a search criteria
186                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
187
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);
191
192                         // Get a search result
193                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
194
195                         // Set the index "solver"
196                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
197
198                         // And finally set it
199                         $this->setResultInstance($resultInstance);
200                 } // END - if
201
202                 // Rewind it
203                 $this->getResultInstance()->rewind();
204
205                 // Search for it
206                 if ($this->getResultInstance()->next()) {
207                         // Entry found
208                         $exists = true;
209
210                         // Is the username set?
211                         if ($this->getUserName() == '') {
212                                 // Get current entry
213                                 $currEntry = $this->getResultInstance()->current();
214
215                                 // Set the username
216                                 $this->setUserName($currEntry['username']);
217                         } // END - if
218                 } // END - if
219
220                 // Return the status
221                 return $exists;
222         }
223
224         /**
225          * Checks if supplied password hash in request matches with the stored in
226          * database.
227          *
228          * @param       $requestInstance        A Requestable class instance
229          * @return      $matches                        Whether the supplied password hash matches
230          */
231         public function ifPasswordHashMatches (Requestable $requestInstance) {
232                 // By default nothing matches... ;)
233                 $matches = false;
234
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');
239
240                         // Create a search criteria
241                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
242
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);
246
247                         // Get a search result
248                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
249
250                         // Set the index "solver"
251                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
252
253                         // And finally set it
254                         $this->setResultInstance($resultInstance);
255                 } // END - if
256
257                 // Rewind it and advance to first entry
258                 $this->getResultInstance()->rewind();
259
260                 // This call set the result instance to a clean state
261                 $this->getResultInstance()->next();
262
263                 // Search for it
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());
268                 } // END - if
269
270                 // Return the status
271                 return $matches;
272         }
273
274         /**
275          * "Getter" for user's password hash
276          *
277          * @return      $passHash       User's password hash from database result
278          */
279         public final function getPasswordHash () {
280                 // Default is missing password hash
281                 $passHash = NULL;
282
283                 // Get a database entry
284                 $entry = $this->getDatabaseEntry();
285
286                 // Is the password hash there?
287                 if (isset($entry['pass_hash'])) {
288                         // Get it
289                         $passHash = $entry['pass_hash'];
290                 } // END - if
291
292                 // And return the hash
293                 return $passHash;
294         }
295
296         /**
297          * Getter for primary key value
298          *
299          * @return      $primaryValue   Value of the primary key based on database type
300          */
301         public final function getPrimaryKey () {
302                 // Get a user database wrapper
303                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
304
305                 // Get the primary key back from the wrapper
306                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
307
308                 // Get that field
309                 $primaryValue = $this->getField($primaryKey);
310
311                 // Return the value
312                 return $primaryValue;
313         }
314
315         /**
316          * Updates a given field with new value
317          *
318          * @param       $fieldName              Field to update
319          * @param       $fieldValue             New value to store
320          * @return      void
321          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
322          */
323         public function updateDatabaseField (string $fieldName, $fieldValue) {
324                 // Get a critieria instance
325                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
326
327                 // Add search criteria
328                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
329                 $searchInstance->setLimit(1);
330
331                 // Now get another criteria
332                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
333
334                 // Add criteria entry which we shall update
335                 $updateInstance->addCriteria($fieldName, $fieldValue);
336
337                 // Add the search criteria for searching for the right entry
338                 $updateInstance->setSearchInstance($searchInstance);
339
340                 // Set wrapper class name
341                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
342
343                 // Remember the update in database result
344                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
345         }
346
347         /**
348          * Checks whether the user status is 'confirmed'
349          *
350          * @return      $isConfirmed    Whether the user status is 'confirmed'
351          */
352         public function isConfirmed () {
353                 // Determine it
354                 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('user_status_confirmed'));
355
356                 // Return it
357                 return $isConfirmed;
358         }
359
360         /**
361          * Checks whether the user status is 'guest'
362          *
363          * @return      $isGuest        Whether the user status is 'guest'
364          */
365         public function isGuest () {
366                 // Determine it
367                 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('user_status_guest'));
368
369                 // Return it
370                 return $isGuest;
371         }
372
373 }