Login and auth classes added. WARNING: All class config entries must end with _class!
[shipsimu.git] / inc / classes / main / user / class_User.php
1 <?php
2 /**
3  * A generic class for handling users
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 User extends BaseFrameworkSystem implements ManageableUser, Registerable {
25         /**
26          * Username of current user
27          */
28         private $userName = "";
29
30         /**
31          * Email of current user
32          */
33         private $email = "";
34
35         // Exceptions
36         const EXCEPTION_USERNAME_NOT_FOUND   = 0x060;
37         const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x061;
38         const EXCEPTION_USER_PASS_MISMATCH   = 0x062;
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct ($class = "") {
46                 // Is the class name empty? Then this is not a specialized user class
47                 if (empty($class)) $class = __CLASS__;
48
49                 // Call parent constructor
50                 parent::__construct($class);
51
52                 // Set part description
53                 $this->setObjectDescription("Generic user class");
54
55                 // Create unique ID number
56                 $this->generateUniqueId();
57
58                 // Clean up a little
59                 $this->removeNumberFormaters();
60                 $this->removeSystemArray();
61         }
62
63         /**
64          * Creates an instance of this user class by a provided username. This
65          * factory method will check if the username is already taken and if not
66          * so it will throw an exception.
67          *
68          * @param       $userName               Username we need a class instance for
69          * @return      $userInstance   An instance of this user class
70          * @throws      UsernameMissingException        If the username does not exist
71          */
72         public final static function createUserByUsername ($userName) {
73                 // Get a new instance
74                 $userInstance = new User();
75
76                 // Set the username
77                 $userInstance->setUserName($userName);
78
79                 // Check if the username exists
80                 if (!$userInstance->ifUsernameExists()) {
81                         // Throw an exception here
82                         throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
83                 }
84
85                 // Return the instance
86                 return $userInstance;
87         }
88
89         /**
90          * Creates an instance of this user class by a provided email address. This
91          * factory method will not check if the email address is there.
92          *
93          * @param       $email                  Email address of the user
94          * @return      $userInstance   An instance of this user class
95          */
96         public final static function createUserByEmail ($email) {
97                 // Get a new instance
98                 $userInstance = new User();
99
100                 // Set the username
101                 $userInstance->setEmail($email);
102
103                 // Return the instance
104                 return $userInstance;
105         }
106
107         /**
108          * Setter for username
109          *
110          * @param       $userName       The username to set
111          * @return      void
112          */
113         public final function setUserName ($userName) {
114                 $this->userName = $userName;
115         }
116
117         /**
118          * Setter for email
119          *
120          * @param       $email  The email to set
121          * @return      void
122          */
123         protected final function setEmail ($email) {
124                 $this->email = $email;
125         }
126
127         /**
128          * Getter for username
129          *
130          * @return      $userName       The username to get
131          */
132         public final function getUsername () {
133                 return $this->userName;
134         }
135
136         /**
137          * Getter for email
138          *
139          * @return      $email  The email to get
140          */
141         public final function getEmail () {
142                 return $this->email;
143         }
144
145         /**
146          * Determines wether the username exists or not
147          *
148          * @return      $exists         Wether the username exists
149          */
150         public function ifUsernameExists () {
151                 // By default the username does not exist
152                 $exists = false;
153
154                 // Get a UserDatabaseWrapper instance
155                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
156
157                 // Create a search criteria
158                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
159
160                 // Add the username as a criteria and set limit to one entry
161                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
162                 $criteriaInstance->setLimit(1);
163
164                 // Get a search result
165                 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
166
167                 // Search for it
168                 if ($result->next()) {
169                         // Entry found
170                         $exists = true;
171                 } // END - if
172
173                 // Return the status
174                 return $exists;
175         }
176
177         /**
178          * Determines wether the email exists or not
179          *
180          * @return      $exists         Wether the email exists
181          */
182         public function ifEmailAddressExists () {
183                 // By default the email does not exist
184                 $exists = false;
185
186                 // Get a UserDatabaseWrapper instance
187                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
188
189                 // Create a search criteria
190                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
191
192                 // Add the username as a criteria and set limit to one entry
193                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
194                 $criteriaInstance->setLimit(1);
195
196                 // Get a search result
197                 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
198
199                 // Search for it
200                 if ($result->next()) {
201                         // Entry found
202                         $exists = true;
203                 } // END - if
204
205                 // Return the status
206                 return $exists;
207         }
208
209         /**
210          * Checks if the supplied password hash in request matches with the stored
211          * in database.
212          *
213          * @param       $requestInstance        A requestable class instance
214          * @return      $matches                        Wether the supplied password hash matches
215          */
216         public function ifPasswordHashMatches (Requestable $requestInstance) {
217                 // By default nothing matches... ;)
218                 $matches = false;
219
220                 // Get a UserDatabaseWrapper instance
221                 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
222
223                 // Create a search criteria
224                 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
225
226                 // Add the username as a criteria and set limit to one entry
227                 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
228                 $criteriaInstance->setLimit(1);
229
230                 // Get a search result
231                 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
232
233                 // Search for it
234                 if ($result->next()) {
235                         // Get the current entry (can only be one!)
236                         $entry = $result->current();
237
238                         // So does the hashes match?
239                         $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
240                 } // END - if
241
242                 // Return the status
243                 return $matches;
244         }
245
246         /**
247          * Adds data for later complete update
248          *
249          * @param       $column         Column we want to update
250          * @param       $value          New value to store in database
251          * @return      void
252          */
253         public function addUpdateData ($column, $value) {
254                 $this->partialStub("Column={$column}, value={$value}");
255         }
256 }
257
258 // [EOF]
259 ?>