Continued:
[core.git] / inc / main / classes / user / class_BaseUser.php
1 <?php
2 // Own namespace
3 namespace CoreFramework\User;
4
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
7
8 /**
9  * A general user class
10  *
11  * @author              Roland Haeder <webmaster@shipsimu.org>
12  * @version             0.0.0
13  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14  * @license             GNU GPL 3.0 or any newer version
15  * @link                http://www.shipsimu.org
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program. If not, see <http://www.gnu.org/licenses/>.
29  */
30 class BaseUser extends BaseFrameworkSystem implements Updateable {
31         // Exception constances
32         const EXCEPTION_USERNAME_NOT_FOUND   = 0x150;
33         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
34         const EXCEPTION_USER_PASS_MISMATCH   = 0x152;
35         const EXCEPTION_USER_IS_GUEST        = 0x153;
36
37         /**
38          * Username of current user
39          */
40         private $userName = '';
41
42         /**
43          * User id of current user
44          */
45         private $userId = 0;
46
47         /**
48          * Email of current user
49          */
50         private $email = '';
51
52         /**
53          * Protected constructor
54          *
55          * @param       $className      Name of the class
56          * @return      void
57          */
58         protected function __construct ($className) {
59                 // Call parent constructor
60                 parent::__construct($className);
61         }
62
63         /**
64          * Setter for username
65          *
66          * @param       $userName       The username to set
67          * @return      void
68          */
69         public final function setUserName ($userName) {
70                 $this->userName = (string) $userName;
71         }
72
73         /**
74          * Getter for username
75          *
76          * @return      $userName       The username to get
77          */
78         public final function getUserName () {
79                 return $this->userName;
80         }
81
82         /**
83          * Setter for user id
84          *
85          * @param       $userId         The user id to set
86          * @return      void
87          * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
88          */
89         public final function setUserId ($userId) {
90                 $this->userId = $userId;
91         }
92
93         /**
94          * Getter for user id
95          *
96          * @return      $userId The user id to get
97          */
98         public final function getUserId () {
99                 return $this->userId;
100         }
101
102         /**
103          * Setter for email
104          *
105          * @param       $email  The email to set
106          * @return      void
107          */
108         protected final function setEmail ($email) {
109                 $this->email = (string) $email;
110         }
111
112         /**
113          * Getter for email
114          *
115          * @return      $email  The email to get
116          */
117         public final function getEmail () {
118                 return $this->email;
119         }
120
121         /**
122          * Determines whether the username exists or not
123          *
124          * @return      $exists         Whether the username exists
125          */
126         public function ifUsernameExists () {
127                 // By default the username does not exist
128                 $exists = FALSE;
129
130                 // Is a previous result there?
131                 if (!$this->getResultInstance() instanceof SearchableResult) {
132                         // Get a UserDatabaseWrapper instance
133                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
134
135                         // Create a search criteria
136                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
137
138                         // Add the username as a criteria and set limit to one entry
139                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
140                         $criteriaInstance->setLimit(1);
141
142                         // Get a search result
143                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
144
145                         // Set the index "solver"
146                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
147
148                         // And finally set it
149                         $this->setResultInstance($resultInstance);
150                 } // END - if
151
152                 // Rewind it
153                 $this->getResultInstance()->rewind();
154
155                 // Search for it
156                 if ($this->getResultInstance()->next()) {
157                         // Entry found
158                         $exists = TRUE;
159                 } // END - if
160
161                 // Return the status
162                 return $exists;
163         }
164
165         /**
166          * Determines whether the email exists or not
167          *
168          * @return      $exists         Whether the email exists
169          */
170         public function ifEmailAddressExists () {
171                 // By default the email does not exist
172                 $exists = FALSE;
173
174                 // Is a previous result there?
175                 if (!$this->getResultInstance() instanceof SearchableResult) {
176                         // Get a UserDatabaseWrapper instance
177                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
178
179                         // Create a search criteria
180                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
181
182                         // Add the username as a criteria and set limit to one entry
183                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
184                         $criteriaInstance->setLimit(1);
185
186                         // Get a search result
187                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
188
189                         // Set the index "solver"
190                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
191
192                         // And finally set it
193                         $this->setResultInstance($resultInstance);
194                 } // END - if
195
196                 // Rewind it
197                 $this->getResultInstance()->rewind();
198
199                 // Search for it
200                 if ($this->getResultInstance()->next()) {
201                         // Entry found
202                         $exists = TRUE;
203
204                         // Is the username set?
205                         if ($this->getUserName() == '') {
206                                 // Get current entry
207                                 $currEntry = $this->getResultInstance()->current();
208
209                                 // Set the username
210                                 $this->setUserName($currEntry['username']);
211                         } // END - if
212                 } // END - if
213
214                 // Return the status
215                 return $exists;
216         }
217
218         /**
219          * Checks if supplied password hash in request matches with the stored in
220          * database.
221          *
222          * @param       $requestInstance        A requestable class instance
223          * @return      $matches                        Whether the supplied password hash matches
224          */
225         public function ifPasswordHashMatches (Requestable $requestInstance) {
226                 // By default nothing matches... ;)
227                 $matches = FALSE;
228
229                 // Is a previous result there?
230                 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
231                         // Get a UserDatabaseWrapper instance
232                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
233
234                         // Create a search criteria
235                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
236
237                         // Add the username as a criteria and set limit to one entry
238                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
239                         $criteriaInstance->setLimit(1);
240
241                         // Get a search result
242                         $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
243
244                         // Set the index "solver"
245                         $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
246
247                         // And finally set it
248                         $this->setResultInstance($resultInstance);
249                 } // END - if
250
251                 // Rewind it and advance to first entry
252                 $this->getResultInstance()->rewind();
253
254                 // This call set the result instance to a clean state
255                 $this->getResultInstance()->next();
256
257                 // Search for it
258                 if ($this->getResultInstance()->find('pass_hash')) {
259                         // So does the hashes match?
260                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
261                         $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
262                 } // END - if
263
264                 // Return the status
265                 return $matches;
266         }
267
268         /**
269          * "Getter" for user's password hash
270          *
271          * @return      $passHash       User's password hash from database result
272          */
273         public final function getPasswordHash () {
274                 // Default is missing password hash
275                 $passHash = NULL;
276
277                 // Get a database entry
278                 $entry = $this->getDatabaseEntry();
279
280                 // Is the password hash there?
281                 if (isset($entry['pass_hash'])) {
282                         // Get it
283                         $passHash = $entry['pass_hash'];
284                 } // END - if
285
286                 // And return the hash
287                 return $passHash;
288         }
289
290         /**
291          * Getter for primary key value
292          *
293          * @return      $primaryValue   Value of the primary key based on database type
294          */
295         public final function getPrimaryKey () {
296                 // Get a user database wrapper
297                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
298
299                 // Get the primary key back from the wrapper
300                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
301
302                 // Get that field
303                 $primaryValue = $this->getField($primaryKey);
304
305                 // Return the value
306                 return $primaryValue;
307         }
308
309         /**
310          * Updates a given field with new value
311          *
312          * @param       $fieldName              Field to update
313          * @param       $fieldValue             New value to store
314          * @return      void
315          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
316          */
317         public function updateDatabaseField ($fieldName, $fieldValue) {
318                 // Get a critieria instance
319                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
320
321                 // Add search criteria
322                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
323                 $searchInstance->setLimit(1);
324
325                 // Now get another criteria
326                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
327
328                 // Add criteria entry which we shall update
329                 $updateInstance->addCriteria($fieldName, $fieldValue);
330
331                 // Add the search criteria for searching for the right entry
332                 $updateInstance->setSearchInstance($searchInstance);
333
334                 // Set wrapper class name
335                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
336
337                 // Remember the update in database result
338                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
339         }
340
341         /**
342          * Checks whether the user status is 'confirmed'
343          *
344          * @return      $isConfirmed    Whether the user status is 'confirmed'
345          */
346         public function isConfirmed () {
347                 // Determine it
348                 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
349
350                 // Return it
351                 return $isConfirmed;
352         }
353
354         /**
355          * Checks whether the user status is 'guest'
356          *
357          * @return      $isGuest        Whether the user status is 'guest'
358          */
359         public function isGuest () {
360                 // Determine it
361                 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));
362
363                 // Return it
364                 return $isGuest;
365         }
366
367 }