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