]> git.mxchange.org Git - shipsimu.git/blob - inc/classes/main/user/class_BaseUser.php
More stubs added, code "made" generic:
[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         /**
26          * Username of current user
27          */
28         private $userName = "";
29
30         /**
31          * Email of current user
32          */
33         private $email = "";
34
35         /**
36          * Protected constructor
37          *
38          * @param       $className      Name of the class
39          * @return      void
40          */
41         protected function __construct ($className) {
42                 // Call parent constructor
43                 parent::__construct($className);
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48         }
49
50         /**
51          * "Getter" for databse entry
52          *
53          * @return      $entry  An array with database entries
54          * @throws      NullPointerException    If the database result is not found
55          * @throws      InvalidDatabaseResultException  If the database result is invalid
56          */
57         private function getDatabaseEntry () {
58                 // Is there an instance?
59                 if (is_null($this->getResultInstance())) {
60                         // Throw new exception
61                         throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
62                 } // END - if
63
64                 // Rewind it
65                 $this->getResultInstance()->rewind();
66
67                 // Do we have an entry?
68                 if (!$this->getResultInstance()->valid()) {
69                         throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
70                 } // END - if
71
72                 // Get next entry
73                 $this->getResultInstance()->next();
74
75                 // Fetch it
76                 $entry = $this->getResultInstance()->current();
77
78                 // And return it
79                 return $entry;
80         }
81
82         /**
83          * Setter for username
84          *
85          * @param       $userName       The username to set
86          * @return      void
87          */
88         public final function setUserName ($userName) {
89                 $this->userName = $userName;
90         }
91
92         /**
93          * Setter for email
94          *
95          * @param       $email  The email to set
96          * @return      void
97          */
98         protected final function setEmail ($email) {
99                 $this->email = $email;
100         }
101
102         /**
103          * Getter for username
104          *
105          * @return      $userName       The username to get
106          */
107         public final function getUsername () {
108                 return $this->userName;
109         }
110
111         /**
112          * Getter for email
113          *
114          * @return      $email  The email to get
115          */
116         public final function getEmail () {
117                 return $this->email;
118         }
119
120         /**
121          * Determines wether the username exists or not
122          *
123          * @return      $exists         Wether the username exists
124          */
125         public function ifUsernameExists () {
126                 // By default the username does not exist
127                 $exists = false;
128
129                 // Is a previous result there?
130                 if (is_null($this->getResultInstance())) {
131                         // Get a UserDatabaseWrapper instance
132                         $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
133
134                         // Create a search criteria
135                         $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
136
137                         // Add the username as a criteria and set limit to one entry
138                         $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
139                         $criteriaInstance->setLimit(1);
140
141                         // Get a search result
142                         $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
143                 } else {
144                         // Rewind it
145                         $this->getResultInstance()->rewind();
146                 }
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 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                 } // END - if
191
192                 // Return the status
193                 return $exists;
194         }
195
196         /**
197          * Checks if the supplied password hash in request matches with the stored
198          * in database.
199          *
200          * @param       $requestInstance        A requestable class instance
201          * @return      $matches                        Wether the supplied password hash matches
202          */
203         public function ifPasswordHashMatches (Requestable $requestInstance) {
204                 // By default nothing matches... ;)
205                 $matches = false;
206
207                 // Get a UserDatabaseWrapper instance
208                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
209
210                 // Create a search criteria
211                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
212
213                 // Add the username as a criteria and set limit to one entry
214                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
215                 $criteriaInstance->setLimit(1);
216
217                 // Get a search resultInstance
218                 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
219
220                 // Search for it
221                 if ($this->getResultInstance()->next()) {
222                         // Get the current entry (can only be one!)
223                         $entry = $this->getResultInstance()->current();
224
225                         // So does the hashes match?
226                         //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
227                         $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
228                 } // END - if
229
230                 // Return the status
231                 return $matches;
232         }
233
234         /**
235          * "Getter" for user's password hash
236          *
237          * @return      $passHash       User's password hash from database result
238          */
239         public final function getPasswordHash () {
240                 // Default is missing password hash
241                 $passHash = null;
242
243                 // Get a database entry
244                 $entry = $this->getDatabaseEntry();
245
246                 // Is the password hash there?
247                 if (isset($entry['pass_hash'])) {
248                         // Get it
249                         $passHash = $entry['pass_hash'];
250                 }
251
252                 // And return the hash
253                 return $passHash;
254         }
255
256         /**
257          * Getter for field name
258          *
259          * @param       $fieldName              Field name which we shall get
260          * @return      $fieldValue             Field value from the user
261          * @todo        Do we need to secure this here against missing results?
262          */
263         public final function getField ($fieldName) {
264                 // Default field value
265                 $fieldValue = null;
266
267                 // Get current array
268                 $fieldArray = $this->getResultInstance()->current();
269
270                 // Does the field exist?
271                 if (isset($fieldArray[$fieldName])) {
272                         // Get it
273                         $fieldValue = $fieldArray[$fieldName];
274                 } // END - if
275
276                 // Return it
277                 return $fieldValue;
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 // [EOF]
301 ?>