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