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