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 {
25 // Exception constances
26 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
27 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
28 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
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();
58 * @param $userName The username to set
61 public final function setUserName ($userName) {
62 $this->userName = $userName;
68 * @param $email The email to set
71 protected final function setEmail ($email) {
72 $this->email = $email;
78 * @return $userName The username to get
80 public final function getUsername () {
81 return $this->userName;
87 * @return $email The email to get
89 public final function getEmail () {
94 * Determines wether the username exists or not
96 * @return $exists Wether the username exists
98 public function ifUsernameExists () {
99 // By default the username does not exist
102 // Is a previous result there?
103 if (is_null($this->getResultInstance())) {
104 // Get a UserDatabaseWrapper instance
105 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
107 // Create a search criteria
108 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
110 // Add the username as a criteria and set limit to one entry
111 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
112 $criteriaInstance->setLimit(1);
114 // Get a search result
115 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
118 $this->getResultInstance()->rewind();
122 if ($this->getResultInstance()->next()) {
132 * Determines wether the email exists or not
134 * @return $exists Wether the email exists
136 public function ifEmailAddressExists () {
137 // By default the email does not exist
140 // Is a previous result there?
141 if (is_null($this->getResultInstance())) {
142 // Get a UserDatabaseWrapper instance
143 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
145 // Create a search criteria
146 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
148 // Add the username as a criteria and set limit to one entry
149 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
150 $criteriaInstance->setLimit(1);
152 // Get a search resultInstance
153 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
156 $this->getResultInstance()->rewind();
160 if ($this->getResultInstance()->next()) {
164 // Is the username set?
165 if ($this->getUserName() == "") {
167 $currEntry = $this->getResultInstance()->current();
170 $this->setUserName($currEntry['username']);
179 * Checks if the supplied password hash in request matches with the stored
182 * @param $requestInstance A requestable class instance
183 * @return $matches Wether the supplied password hash matches
185 public function ifPasswordHashMatches (Requestable $requestInstance) {
186 // By default nothing matches... ;)
189 // Get a UserDatabaseWrapper instance
190 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
192 // Create a search criteria
193 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
195 // Add the username as a criteria and set limit to one entry
196 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
197 $criteriaInstance->setLimit(1);
199 // Get a search resultInstance
200 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
203 if ($this->getResultInstance()->next()) {
204 // Get the current entry (can only be one!)
205 $entry = $this->getResultInstance()->current();
207 // So does the hashes match?
208 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
209 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
217 * "Getter" for user's password hash
219 * @return $passHash User's password hash from database result
221 public final function getPasswordHash () {
222 // Default is missing password hash
225 // Get a database entry
226 $entry = $this->getDatabaseEntry();
228 // Is the password hash there?
229 if (isset($entry['pass_hash'])) {
231 $passHash = $entry['pass_hash'];
234 // And return the hash
239 * Getter for primary key value
241 * @return $primaryValue Value of the primary key based on database type
243 public final function getPrimaryKey () {
244 // Get a user database wrapper
245 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
247 // Get the primary key back from the wrapper
248 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
251 $primaryValue = $this->getField($primaryKey);
254 return $primaryValue;