b3fc24d717409210fba85d3165f371ca220a658b
[shipsimu.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, this is free software
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 {
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                 // Clean up a little
56                 $this->removeNumberFormaters();
57                 $this->removeSystemArray();
58         }
59
60         /**
61          * Setter for username
62          *
63          * @param       $userName       The username to set
64          * @return      void
65          */
66         public final function setUserName ($userName) {
67                 $this->userName = (string) $userName;
68         }
69
70         /**
71          * Getter for username
72          *
73          * @return      $userName       The username to get
74          */
75         public final function getUserName () {
76                 return $this->userName;
77         }
78
79         /**
80          * Setter for user id
81          *
82          * @param       $userId         The user id to set
83          * @return      void
84          * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
85          */
86         public final function setUserId ($userId) {
87                 $this->userId = $userId;
88         }
89
90         /**
91          * Getter for user id
92          *
93          * @return      $userId The user id to get
94          */
95         public final function getUserId () {
96                 return $this->userId;
97         }
98
99         /**
100          * Setter for email
101          *
102          * @param       $email  The email to set
103          * @return      void
104          */
105         protected final function setEmail ($email) {
106                 $this->email = (string) $email;
107         }
108
109         /**
110          * Getter for email
111          *
112          * @return      $email  The email to get
113          */
114         public final function getEmail () {
115                 return $this->email;
116         }
117
118         /**
119          * Determines wether the username exists or not
120          *
121          * @return      $exists         Wether the username exists
122          */
123         public function ifUsernameExists () {
124                 // By default the username does not exist
125                 $exists = false;
126
127                 // Is a previous result there?
128                 if (is_null($this->getResultInstance())) {
129                         // Get a UserDatabaseWrapper instance
130                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
131
132                         // Create a search criteria
133                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
134
135                         // Add the username as a criteria and set limit to one entry
136                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
137                         $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
138                         $criteriaInstance->setLimit(1);
139
140                         // Get a search result
141                         $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
142                 } else {
143                         // Rewind it
144                         $this->getResultInstance()->rewind();
145                 }
146
147                 // Search for it
148                 if ($this->getResultInstance()->next()) {
149                         // Entry found
150                         $exists = true;
151                 } // END - if
152
153                 // Return the status
154                 return $exists;
155         }
156
157         /**
158          * Determines wether the email exists or not
159          *
160          * @return      $exists         Wether the email exists
161          */
162         public function ifEmailAddressExists () {
163                 // By default the email does not exist
164                 $exists = false;
165
166                 // Is a previous result there?
167                 if (is_null($this->getResultInstance())) {
168                         // Get a UserDatabaseWrapper instance
169                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
170
171                         // Create a search criteria
172                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
173
174                         // Add the username as a criteria and set limit to one entry
175                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
176                         $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
177                         $criteriaInstance->setLimit(1);
178
179                         // Get a search resultInstance
180                         $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
181                 } else {
182                         // Rewind it
183                         $this->getResultInstance()->rewind();
184                 }
185
186                 // Search for it
187                 if ($this->getResultInstance()->next()) {
188                         // Entry found
189                         $exists = true;
190
191                         // Is the username set?
192                         if ($this->getUserName() == "") {
193                                 // Get current entry
194                                 $currEntry = $this->getResultInstance()->current();
195
196                                 // Set the username
197                                 $this->setUserName($currEntry['username']);
198                         } // END - if
199                 } // END - if
200
201                 // Return the status
202                 return $exists;
203         }
204
205         /**
206          * Checks if the supplied password hash in request matches with the stored
207          * in database.
208          *
209          * @param       $requestInstance        A requestable class instance
210          * @return      $matches                        Wether the supplied password hash matches
211          */
212         public function ifPasswordHashMatches (Requestable $requestInstance) {
213                 // By default nothing matches... ;)
214                 $matches = false;
215
216                 // Get a UserDatabaseWrapper instance
217                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
218
219                 // Create a search criteria
220                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
221
222                 // Add the username as a criteria and set limit to one entry
223                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
224                 $criteriaInstance->setIndexSolution(UserDatabaseWrapper::DB_COLUMN_USERID, array($this, 'setUserId'));
225                 $criteriaInstance->setLimit(1);
226
227                 // Get a search resultInstance
228                 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
229
230                 // Search for it
231                 if ($this->getResultInstance()->next()) {
232                         // Get the current entry (can only be one!)
233                         $entry = $this->getResultInstance()->current();
234
235                         // So does the hashes match?
236                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
237                         $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
238                 } // END - if
239
240                 // Return the status
241                 return $matches;
242         }
243
244         /**
245          * "Getter" for user's password hash
246          *
247          * @return      $passHash       User's password hash from database result
248          */
249         public final function getPasswordHash () {
250                 // Default is missing password hash
251                 $passHash = null;
252
253                 // Get a database entry
254                 $entry = $this->getDatabaseEntry();
255
256                 // Is the password hash there?
257                 if (isset($entry['pass_hash'])) {
258                         // Get it
259                         $passHash = $entry['pass_hash'];
260                 } // END - if
261
262                 // And return the hash
263                 return $passHash;
264         }
265
266         /**
267          * Getter for primary key value
268          *
269          * @return      $primaryValue   Value of the primary key based on database type
270          */
271         public final function getPrimaryKey () {
272                 // Get a user database wrapper
273                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
274
275                 // Get the primary key back from the wrapper
276                 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
277
278                 // Get that field
279                 $primaryValue = $this->getField($primaryKey);
280
281                 // Return the value
282                 return $primaryValue;
283         }
284
285         /**
286          * Updates a given field with new value
287          *
288          * @param       $fieldName              Field to update
289          * @param       $fieldValue             New value to store
290          * @return      void
291          * @throws      DatabaseUpdateSupportException  If this class does not support database updates
292          */
293         public function updateDatabaseField ($fieldName, $fieldValue) {
294                 // Is updating database fields allowed by interface?
295                 if (!$this instanceof Updateable) {
296                         // Update not supported!
297                         throw new DatabaseUpdateSupportException($this, self::EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED);
298                 } // END - if
299
300                 // Get a critieria instance
301                 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
302
303                 // Add search criteria
304                 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
305                 $searchInstance->setLimit(1);
306
307                 // Now get another criteria
308                 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
309
310                 // And add our both entries
311                 $updateInstance->addCriteria($fieldName, $fieldValue);
312
313                 // Add the search criteria for searching for the right entry
314                 $updateInstance->setSearchInstance($searchInstance);
315
316                 // Remember the update in database result
317                 $this->getResultInstance()->add2UpdateQueue($updateInstance);
318         }
319 }
320
321 // [EOF]
322 ?>