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