3 * A generic class for handling users
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class User extends BaseFrameworkSystem implements ManageableUser, Registerable {
26 * Username of current user
28 private $userName = "";
31 * Email of current user
36 const EXCEPTION_USERNAME_NOT_FOUND = 0x060;
37 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x061;
38 const EXCEPTION_USER_PASS_MISMATCH = 0x062;
41 * Protected constructor
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__;
49 // Call parent constructor
50 parent::__construct($class);
53 $this->removeNumberFormaters();
54 $this->removeSystemArray();
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.
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
66 public final static function createUserByUsername ($userName) {
68 $userInstance = new User();
71 $userInstance->setUserName($userName);
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);
79 // Return the instance
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.
87 * @param $email Email address of the user
88 * @return $userInstance An instance of this user class
90 public final static function createUserByEmail ($email) {
92 $userInstance = new User();
95 $userInstance->setEmail($email);
97 // Return the instance
102 * Setter for username
104 * @param $userName The username to set
107 public final function setUserName ($userName) {
108 $this->userName = $userName;
114 * @param $email The email to set
117 protected final function setEmail ($email) {
118 $this->email = $email;
122 * Getter for username
124 * @return $userName The username to get
126 public final function getUsername () {
127 return $this->userName;
133 * @return $email The email to get
135 public final function getEmail () {
140 * Determines wether the username exists or not
142 * @return $exists Wether the username exists
144 public function ifUsernameExists () {
145 // By default the username does not exist
148 // Get a UserDatabaseWrapper instance
149 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
151 // Create a search criteria
152 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
158 // Get a search result
159 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
162 if ($result->next()) {
172 * Determines wether the email exists or not
174 * @return $exists Wether the email exists
176 public function ifEmailAddressExists () {
177 // By default the email does not exist
180 // Get a UserDatabaseWrapper instance
181 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
183 // Create a search criteria
184 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
190 // Get a search result
191 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
194 if ($result->next()) {
204 * Checks if the supplied password hash in request matches with the stored
207 * @param $requestInstance A requestable class instance
208 * @return $matches Wether the supplied password hash matches
210 public function ifPasswordHashMatches (Requestable $requestInstance) {
211 // By default nothing matches... ;)
214 // Get a UserDatabaseWrapper instance
215 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
217 // Create a search criteria
218 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
224 // Get a search result
225 $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
228 if ($result->next()) {
229 // Get the current entry (can only be one!)
230 $entry = $result->current();
232 // So does the hashes match?
233 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
241 * Adds data for later complete update
243 * @param $column Column we want to update
244 * @param $value New value to store in database
247 public function addUpdateData ($column, $value) {
248 $this->partialStub("Column={$column}, value={$value}");