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