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