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 * Instance of the database result
28 private $resultInstance = null;
31 * Username of current user
33 private $userName = "";
36 * Email of current user
41 * Protected constructor
43 * @param $className Name of the class
46 protected function __construct ($className) {
47 // Call parent constructor
48 parent::__construct($className);
51 $this->removeNumberFormaters();
52 $this->removeSystemArray();
56 * "Getter" for databse entry
58 * @return $entry An array with database entries
59 * @throws NullPointerException If the database result is not found
60 * @throws InvalidDatabaseResultException If the database result is invalid
62 private function getDatabaseEntry () {
63 // Is there an instance?
64 if (is_null($this->getResultInstance())) {
65 // Throw new exception
66 throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
70 $this->getResultInstance()->rewind();
72 // Do we have an entry?
73 if (!$this->getResultInstance()->valid()) {
74 throw new InvalidDatabaseResultException(array($this, $this->resultInstance), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
78 $this->getResultInstance()->next();
81 $entry = $this->getResultInstance()->current();
90 * @param $userName The username to set
93 public final function setUserName ($userName) {
94 $this->userName = $userName;
100 * @param $email The email to set
103 protected final function setEmail ($email) {
104 $this->email = $email;
108 * Getter for username
110 * @return $userName The username to get
112 public final function getUsername () {
113 return $this->userName;
119 * @return $email The email to get
121 public final function getEmail () {
126 * Getter for database result instance
128 * @return $resultInstance An instance of a database result class
130 protected final function getResultInstance () {
131 return $this->resultInstance;
135 * Determines wether the username exists or not
137 * @return $exists Wether the username exists
139 public function ifUsernameExists () {
140 // By default the username does not exist
143 // Is a previous result there?
144 if (is_null($this->getResultInstance())) {
145 // Get a UserDatabaseWrapper instance
146 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
148 // Create a search criteria
149 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
151 // Add the username as a criteria and set limit to one entry
152 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
153 $criteriaInstance->setLimit(1);
155 // Get a search result
156 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
159 $this->getResultInstance()->rewind();
163 if ($this->getResultInstance()->next()) {
173 * Determines wether the email exists or not
175 * @return $exists Wether the email exists
177 public function ifEmailAddressExists () {
178 // By default the email does not exist
181 // Is a previous result there?
182 if (is_null($this->getResultInstance())) {
183 // Get a UserDatabaseWrapper instance
184 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
186 // Create a search criteria
187 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
189 // Add the username as a criteria and set limit to one entry
190 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
191 $criteriaInstance->setLimit(1);
193 // Get a search resultInstance
194 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
197 $this->getResultInstance()->rewind();
201 if ($this->getResultInstance()->next()) {
211 * Checks if the supplied password hash in request matches with the stored
214 * @param $requestInstance A requestable class instance
215 * @return $matches Wether the supplied password hash matches
217 public function ifPasswordHashMatches (Requestable $requestInstance) {
218 // By default nothing matches... ;)
221 // Get a UserDatabaseWrapper instance
222 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
224 // Create a search criteria
225 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
227 // Add the username as a criteria and set limit to one entry
228 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
229 $criteriaInstance->setLimit(1);
231 // Get a search resultInstance
232 $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
235 if ($this->getResultInstance()->next()) {
236 // Get the current entry (can only be one!)
237 $entry = $this->getResultInstance()->current();
239 // So does the hashes match?
240 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
241 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
249 * "Getter" for user's password hash
251 * @return $passHash User's password hash from database result
253 public function getPasswordHash () {
254 // Default is missing password hash
257 // Get a database entry
258 $entry = $this->getDatabaseEntry();
260 // Is the password hash there?
261 if (isset($entry['pass_hash'])) {
263 $passHash = $entry['pass_hash'];
266 // And return the hash
271 * Getter for field name
273 * @param $fieldName Field name which we shall get
274 * @return $fieldValue Field value from the user
275 * @todo Do we need to secure this here against missing results?
277 public function getField ($fieldName) {
278 // Default field value
282 $fieldArray = $this->getResultInstance()->current();
284 // Does the field exist?
285 if (isset($fieldArray[$fieldName])) {
287 $fieldValue = $fieldArray[$fieldName];