5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2013 Core Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 implements Updateable {
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 * User id of current user
41 * Email of current user
46 * Protected constructor
48 * @param $className Name of the class
51 protected function __construct ($className) {
52 // Call parent constructor
53 parent::__construct($className);
59 * @param $userName The username to set
62 public final function setUserName ($userName) {
63 $this->userName = (string) $userName;
69 * @return $userName The username to get
71 public final function getUserName () {
72 return $this->userName;
78 * @param $userId The user id to set
80 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
82 public final function setUserId ($userId) {
83 $this->userId = $userId;
89 * @return $userId The user id to get
91 public final function getUserId () {
98 * @param $email The email to set
101 protected final function setEmail ($email) {
102 $this->email = (string) $email;
108 * @return $email The email to get
110 public final function getEmail () {
115 * Determines whether the username exists or not
117 * @return $exists Whether the username exists
119 public function ifUsernameExists () {
120 // By default the username does not exist
123 // Is a previous result there?
124 if (is_null($this->getResultInstance())) {
125 // Get a UserDatabaseWrapper instance
126 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
128 // Create a search criteria
129 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
131 // Add the username as a criteria and set limit to one entry
132 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
133 $criteriaInstance->setLimit(1);
135 // Get a search result
136 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
138 // Set the index "solver"
139 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
141 // And finally set it
142 $this->setResultInstance($resultInstance);
146 $this->getResultInstance()->rewind();
149 if ($this->getResultInstance()->next()) {
159 * Determines whether the email exists or not
161 * @return $exists Whether 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 result
180 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
182 // Set the index "solver"
183 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
185 // And finally set it
186 $this->setResultInstance($resultInstance);
190 $this->getResultInstance()->rewind();
193 if ($this->getResultInstance()->next()) {
197 // Is the username set?
198 if ($this->getUserName() == '') {
200 $currEntry = $this->getResultInstance()->current();
203 $this->setUserName($currEntry['username']);
212 * Checks if supplied password hash in request matches with the stored in
215 * @param $requestInstance A requestable class instance
216 * @return $matches Whether the supplied password hash matches
218 public function ifPasswordHashMatches (Requestable $requestInstance) {
219 // By default nothing matches... ;)
222 // Is a previous result there?
223 if (is_null($this->getResultInstance())) {
224 // Get a UserDatabaseWrapper instance
225 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
227 // Create a search criteria
228 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
230 // Add the username as a criteria and set limit to one entry
231 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
232 $criteriaInstance->setLimit(1);
234 // Get a search result
235 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
237 // Set the index "solver"
238 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
240 // And finally set it
241 $this->setResultInstance($resultInstance);
244 // Rewind it and advance to first entry
245 $this->getResultInstance()->rewind();
247 // This call set the result instance to a clean state
248 $this->getResultInstance()->next();
251 if ($this->getResultInstance()->find('pass_hash')) {
252 // So does the hashes match?
253 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
254 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
262 * "Getter" for user's password hash
264 * @return $passHash User's password hash from database result
266 public final function getPasswordHash () {
267 // Default is missing password hash
270 // Get a database entry
271 $entry = $this->getDatabaseEntry();
273 // Is the password hash there?
274 if (isset($entry['pass_hash'])) {
276 $passHash = $entry['pass_hash'];
279 // And return the hash
284 * Getter for primary key value
286 * @return $primaryValue Value of the primary key based on database type
288 public final function getPrimaryKey () {
289 // Get a user database wrapper
290 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
292 // Get the primary key back from the wrapper
293 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
296 $primaryValue = $this->getField($primaryKey);
299 return $primaryValue;
303 * Updates a given field with new value
305 * @param $fieldName Field to update
306 * @param $fieldValue New value to store
308 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
310 public function updateDatabaseField ($fieldName, $fieldValue) {
311 // Get a critieria instance
312 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
314 // Add search criteria
315 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
316 $searchInstance->setLimit(1);
318 // Now get another criteria
319 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
321 // Add criteria entry which we shall update
322 $updateInstance->addCriteria($fieldName, $fieldValue);
324 // Add the search criteria for searching for the right entry
325 $updateInstance->setSearchInstance($searchInstance);
327 // Set wrapper class name
328 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
330 // Remember the update in database result
331 $this->getResultInstance()->add2UpdateQueue($updateInstance);