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