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()) {
191 // Is the username set?
192 if ($this->getUserName() == "") {
194 $currEntry = $this->getResultInstance()->current();
197 $this->setUserName($currEntry['username']);
206 * Checks if the supplied password hash in request matches with the stored
209 * @param $requestInstance A requestable class instance
210 * @return $matches Wether the supplied password hash matches
212 public function ifPasswordHashMatches (Requestable $requestInstance) {
213 // By default nothing matches... ;)
216 // Get a UserDatabaseWrapper instance
217 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
219 // Create a search criteria
220 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
222 // Add the username as a criteria and set limit to one entry
223 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
224 $criteriaInstance->setLimit(1);
226 // Get a search resultInstance
227 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
230 if ($this->getResultInstance()->next()) {
231 // Get the current entry (can only be one!)
232 $entry = $this->getResultInstance()->current();
234 // So does the hashes match?
235 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
236 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
244 * "Getter" for user's password hash
246 * @return $passHash User's password hash from database result
248 public final function getPasswordHash () {
249 // Default is missing password hash
252 // Get a database entry
253 $entry = $this->getDatabaseEntry();
255 // Is the password hash there?
256 if (isset($entry['pass_hash'])) {
258 $passHash = $entry['pass_hash'];
261 // And return the hash
266 * Getter for field name
268 * @param $fieldName Field name which we shall get
269 * @return $fieldValue Field value from the user
270 * @throws NullPointerException If the result instance is null
272 public final function getField ($fieldName) {
273 // Default field value
276 // Get result instance
277 $resultInstance = $this->getResultInstance();
279 // Is this instance null?
280 if (is_null($resultInstance)) {
281 // Then the user instance is no longer valid (expired cookies?)
282 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
286 $fieldArray = $resultInstance->current();
288 // Does the field exist?
289 if (isset($fieldArray[$fieldName])) {
291 $fieldValue = $fieldArray[$fieldName];
299 * Getter for primary key value
301 * @return $primaryValue Value of the primary key based on database type
303 public final function getPrimaryKey () {
304 // Get a user database wrapper
305 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
307 // Get the primary key back from the wrapper
308 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
311 $primaryValue = $this->getField($primaryKey);
314 return $primaryValue;