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();
53 * @param $userName The username to set
56 public final function setUserName ($userName) {
57 $this->userName = $userName;
63 * @param $email The email to set
66 protected final function setEmail ($email) {
67 $this->email = $email;
73 * @return $userName The username to get
75 public final function getUsername () {
76 return $this->userName;
82 * @return $email The email to get
84 public final function getEmail () {
89 * Determines wether the username exists or not
91 * @return $exists Wether the username exists
93 public function ifUsernameExists () {
94 // By default the username does not exist
97 // Is a previous result there?
98 if (is_null($this->getResultInstance())) {
99 // Get a UserDatabaseWrapper instance
100 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
102 // Create a search criteria
103 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
105 // Add the username as a criteria and set limit to one entry
106 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUsername());
107 $criteriaInstance->setLimit(1);
109 // Get a search result
110 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
113 $this->getResultInstance()->rewind();
117 if ($this->getResultInstance()->next()) {
127 * Determines wether the email exists or not
129 * @return $exists Wether the email exists
131 public function ifEmailAddressExists () {
132 // By default the email does not exist
135 // Is a previous result there?
136 if (is_null($this->getResultInstance())) {
137 // Get a UserDatabaseWrapper instance
138 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
140 // Create a search criteria
141 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
143 // Add the username as a criteria and set limit to one entry
144 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
145 $criteriaInstance->setLimit(1);
147 // Get a search resultInstance
148 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
151 $this->getResultInstance()->rewind();
155 if ($this->getResultInstance()->next()) {
159 // Is the username set?
160 if ($this->getUserName() == "") {
162 $currEntry = $this->getResultInstance()->current();
165 $this->setUserName($currEntry['username']);
174 * Checks if the supplied password hash in request matches with the stored
177 * @param $requestInstance A requestable class instance
178 * @return $matches Wether the supplied password hash matches
180 public function ifPasswordHashMatches (Requestable $requestInstance) {
181 // By default nothing matches... ;)
184 // Get a UserDatabaseWrapper instance
185 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
187 // Create a search criteria
188 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
190 // Add the username as a criteria and set limit to one entry
191 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
192 $criteriaInstance->setLimit(1);
194 // Get a search resultInstance
195 $this->setResultInstance($wrapperInstance->doSelectByCriteria($criteriaInstance));
198 if ($this->getResultInstance()->next()) {
199 // Get the current entry (can only be one!)
200 $entry = $this->getResultInstance()->current();
202 // So does the hashes match?
203 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
204 $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
212 * "Getter" for user's password hash
214 * @return $passHash User's password hash from database result
216 public final function getPasswordHash () {
217 // Default is missing password hash
220 // Get a database entry
221 $entry = $this->getDatabaseEntry();
223 // Is the password hash there?
224 if (isset($entry['pass_hash'])) {
226 $passHash = $entry['pass_hash'];
229 // And return the hash
234 * Getter for primary key value
236 * @return $primaryValue Value of the primary key based on database type
238 public final function getPrimaryKey () {
239 // Get a user database wrapper
240 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
242 // Get the primary key back from the wrapper
243 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
246 $primaryValue = $this->getField($primaryKey);
249 return $primaryValue;