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 BaseUser extends BaseFrameworkSystem {
26 * Username of current user
28 private $userName = "";
31 * Email of current user
36 * Protected constructor
38 * @param $className Name of the class
41 protected function __construct ($className) {
42 // Call parent constructor
43 parent::__construct($className);
46 $this->removeNumberFormaters();
47 $this->removeSystemArray();
51 * "Getter" for databse entry
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
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);
65 $this->getResultInstance()->rewind();
67 // Do we have an entry?
68 if (!$this->getResultInstance()->valid()) {
69 throw new InvalidDatabaseResultException(array($this, $this->getResultInstance()), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
73 $this->getResultInstance()->next();
76 $entry = $this->getResultInstance()->current();
85 * @param $userName The username to set
88 public final function setUserName ($userName) {
89 $this->userName = $userName;
95 * @param $email The email to set
98 protected final function setEmail ($email) {
99 $this->email = $email;
103 * Getter for username
105 * @return $userName The username to get
107 public final function getUsername () {
108 return $this->userName;
114 * @return $email The email to get
116 public final function getEmail () {
121 * Determines wether the username exists or not
123 * @return $exists Wether the username exists
125 public function ifUsernameExists () {
126 // By default the username does not exist
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');
134 // Create a search criteria
135 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
141 // Get a search result
142 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
145 $this->getResultInstance()->rewind();
149 if ($this->getResultInstance()->next()) {
159 * Determines wether the email exists or not
161 * @return $exists Wether the email exists
163 public function ifEmailAddressExists () {
164 // By default the email does not exist
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');
172 // Create a search criteria
173 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
179 // Get a search resultInstance
180 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
183 $this->getResultInstance()->rewind();
187 if ($this->getResultInstance()->next()) {
197 * Checks if the supplied password hash in request matches with the stored
200 * @param $requestInstance A requestable class instance
201 * @return $matches Wether the supplied password hash matches
203 public function ifPasswordHashMatches (Requestable $requestInstance) {
204 // By default nothing matches... ;)
207 // Get a UserDatabaseWrapper instance
208 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
210 // Create a search criteria
211 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
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);
217 // Get a search resultInstance
218 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
221 if ($this->getResultInstance()->next()) {
222 // Get the current entry (can only be one!)
223 $entry = $this->getResultInstance()->current();
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']);
235 * "Getter" for user's password hash
237 * @return $passHash User's password hash from database result
239 public final function getPasswordHash () {
240 // Default is missing password hash
243 // Get a database entry
244 $entry = $this->getDatabaseEntry();
246 // Is the password hash there?
247 if (isset($entry['pass_hash'])) {
249 $passHash = $entry['pass_hash'];
252 // And return the hash
257 * Getter for field name
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?
263 public final function getField ($fieldName) {
264 // Default field value
268 $fieldArray = $this->getResultInstance()->current();
270 // Does the field exist?
271 if (isset($fieldArray[$fieldName])) {
273 $fieldValue = $fieldArray[$fieldName];
281 * Getter for primary key value
283 * @return $primaryValue Value of the primary key based on database type
285 public final function getPrimaryKey () {
286 // Get a user database wrapper
287 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
289 // Get the primary key back from the wrapper
290 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
293 $primaryValue = $this->getField($primaryKey);
296 return $primaryValue;