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 * Instance of the database result
28 private $resultInstance = null;
31 * Username of current user
33 private $userName = "";
36 * Email of current user
41 const EXCEPTION_USERNAME_NOT_FOUND = 0x060;
42 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x061;
43 const EXCEPTION_USER_PASS_MISMATCH = 0x062;
46 * Protected constructor
50 protected function __construct ($class = "") {
51 // Is the class name empty? Then this is not a specialized user class
52 if (empty($class)) $class = __CLASS__;
54 // Call parent constructor
55 parent::__construct($class);
57 // Set part description
58 $this->setObjectDescription("Generic user class");
60 // Create unique ID number
61 $this->generateUniqueId();
64 $this->removeNumberFormaters();
65 $this->removeSystemArray();
69 * Creates an instance of this user class by a provided username. This
70 * factory method will check if the username is already taken and if not
71 * so it will throw an exception.
73 * @param $userName Username we need a class instance for
74 * @return $userInstance An instance of this user class
75 * @throws UsernameMissingException If the username does not exist
77 public final static function createUserByUsername ($userName) {
79 $userInstance = new User();
82 $userInstance->setUserName($userName);
84 // Check if the username exists
85 if (!$userInstance->ifUsernameExists()) {
86 // Throw an exception here
87 throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
90 // Return the instance
95 * Creates an instance of this user class by a provided email address. This
96 * factory method will not check if the email address is there.
98 * @param $email Email address of the user
99 * @return $userInstance An instance of this user class
101 public final static function createUserByEmail ($email) {
102 // Get a new instance
103 $userInstance = new User();
106 $userInstance->setEmail($email);
108 // Return the instance
109 return $userInstance;
113 * "Getter" for databse entry
115 * @return $entry An array with database entries
116 * @throws NullPointerException If the database result is not found
117 * @throws InvalidDatabaseResultException If the database result is invalid
119 private function getDatabaseEntry () {
120 // Is there an instance?
121 if (is_null($this->resultInstance)) {
122 // Throw new exception
123 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
127 $this->resultInstance->rewind();
129 // Do we have an entry?
130 if (!$this->resultInstance->valid()) {
131 throw new InvalidDatabaseResultException(array($this, $this->resultInstance), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
135 $this->resultInstance->next();
138 $entry = $this->resultInstance->current();
145 * Setter for username
147 * @param $userName The username to set
150 public final function setUserName ($userName) {
151 $this->userName = $userName;
157 * @param $email The email to set
160 protected final function setEmail ($email) {
161 $this->email = $email;
165 * Getter for username
167 * @return $userName The username to get
169 public final function getUsername () {
170 return $this->userName;
176 * @return $email The email to get
178 public final function getEmail () {
183 * Determines wether the username exists or not
185 * @return $exists Wether the username exists
187 public function ifUsernameExists () {
188 // By default the username does not exist
191 // Is a previous result there?
192 if (is_null($this->resultInstance)) {
193 // Get a UserDatabaseWrapper instance
194 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
196 // Create a search criteria
197 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
199 // Add the username as a criteria and set limit to one entry
200 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
201 $criteriaInstance->setLimit(1);
203 // Get a search result
204 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
207 $this->resultInstance->rewind();
211 if ($this->resultInstance->next()) {
221 * Determines wether the email exists or not
223 * @return $exists Wether the email exists
225 public function ifEmailAddressExists () {
226 // By default the email does not exist
229 // Is a previous result there?
230 if (is_null($this->resultInstance)) {
231 // Get a UserDatabaseWrapper instance
232 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
234 // Create a search criteria
235 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
237 // Add the username as a criteria and set limit to one entry
238 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
239 $criteriaInstance->setLimit(1);
241 // Get a search resultInstance
242 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
245 $this->resultInstance->rewind();
249 if ($this->resultInstance->next()) {
259 * Checks if the supplied password hash in request matches with the stored
262 * @param $requestInstance A requestable class instance
263 * @return $matches Wether the supplied password hash matches
265 public function ifPasswordHashMatches (Requestable $requestInstance) {
266 // By default nothing matches... ;)
269 // Get a UserDatabaseWrapper instance
270 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
272 // Create a search criteria
273 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
275 // Add the username as a criteria and set limit to one entry
276 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
277 $criteriaInstance->setLimit(1);
279 // Get a search resultInstance
280 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
283 if ($this->resultInstance->next()) {
284 // Get the current entry (can only be one!)
285 $entry = $this->resultInstance->current();
287 // So does the hashes match?
288 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
296 * Adds data for later complete update
298 * @param $column Column we want to update
299 * @param $value New value to store in database
302 public function addUpdateData ($column, $value) {
303 $this->partialStub("Column={$column}, value={$value}");
307 * "Getter" for user's password hash
309 * @return $passHash User's password hash from database result
311 public function getPasswordHash () {
312 // Default is missing password hash
315 // Get a database entry
316 $entry = $this->getDatabaseEntry();
318 // Is the password hash there?
319 if (isset($entry['pass_hash'])) {
321 $passHash = $entry['pass_hash'];
324 // And return the hash