Fix for method in FrameworkConfiguration, removal of unneccessary code
[core.git] / inc / classes / main / user / class_BaseUser.php
1 <?php
2 /**
3  * A general user class 
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 Core Developer Team
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 wether the username exists or not
116          *
117          * @return      $exists         Wether 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 wether the email exists or not
160          *
161          * @return      $exists         Wether 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                        Wether 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
245                 $this->getResultInstance()->rewind();
246
247                 // Search for it
248                 if ($this->getResultInstance()->find('pass_hash')) {
249                         // So does the hashes match?
250                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '/' . $this->getResultInstance()->getFoundValue() . '<br />';
251                         $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
252                 } // END - if
253
254                 // Return the status
255                 return $matches;
256         }
257
258         /**
259          * "Getter" for user's password hash
260          *
261          * @return      $passHash       User's password hash from database result
262          */
263         public final function getPasswordHash () {
264                 // Default is missing password hash
265                 $passHash = null;
266
267                 // Get a database entry
268                 $entry = $this->getDatabaseEntry();
269
270                 // Is the password hash there?
271                 if (isset($entry['pass_hash'])) {
272                         // Get it
273                         $passHash = $entry['pass_hash'];
274                 } // END - if
275
276                 // And return the hash
277                 return $passHash;
278         }
279
280         /**
281          * Getter for primary key value
282          *
283          * @return      $primaryValue   Value of the primary key based on database type
284          */
285         public final function getPrimaryKey () {
286                 // Get a user database wrapper
287                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
288
289                 // Get the primary key back from the wrapper
290                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
291
292                 // Get that field
293                 $primaryValue = $this->getField($primaryKey);
294
295                 // Return the value
296                 return $primaryValue;
297         }
298
299         /**
300          * Updates a given field with new value
301          *
302          * @param       $fieldName              Field to update
303          * @param       $fieldValue             New value to store
304          * @return      void
305          * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
306          */
307         public function updateDatabaseField ($fieldName, $fieldValue) {
308                 // Get a critieria instance
309                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
310
311                 // Add search criteria
312                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
313                 $searchInstance->setLimit(1);
314
315                 // Now get another criteria
316                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
317
318                 // Add criteria entry which we shall update
319                 $updateInstance->addCriteria($fieldName, $fieldValue);
320
321                 // Add the search criteria for searching for the right entry
322                 $updateInstance->setSearchInstance($searchInstance);
323
324                 // Set wrapper class name
325                 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
326
327                 // Remember the update in database result
328                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
329         }
330 }
331
332 // [EOF]
333 ?>