3 namespace CoreFramework\User;
5 // Import framework stuff
6 use CoreFramework\Object\BaseFrameworkSystem;
11 * @author Roland Haeder <webmaster@shipsimu.org>
13 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
14 * @license GNU GPL 3.0 or any newer version
15 * @link http://www.shipsimu.org
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 class BaseUser extends BaseFrameworkSystem implements Updateable {
31 // Exception constances
32 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
33 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
34 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
35 const EXCEPTION_USER_IS_GUEST = 0x153;
38 * Username of current user
40 private $userName = '';
43 * User id of current user
48 * Email of current user
53 * Protected constructor
55 * @param $className Name of the class
58 protected function __construct ($className) {
59 // Call parent constructor
60 parent::__construct($className);
66 * @param $userName The username to set
69 public final function setUserName ($userName) {
70 $this->userName = (string) $userName;
76 * @return $userName The username to get
78 public final function getUserName () {
79 return $this->userName;
85 * @param $userId The user id to set
87 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
89 public final function setUserId ($userId) {
90 $this->userId = $userId;
96 * @return $userId The user id to get
98 public final function getUserId () {
105 * @param $email The email to set
108 protected final function setEmail ($email) {
109 $this->email = (string) $email;
115 * @return $email The email to get
117 public final function getEmail () {
122 * Determines whether the username exists or not
124 * @return $exists Whether the username exists
126 public function ifUsernameExists () {
127 // By default the username does not exist
130 // Is a previous result there?
131 if (!$this->getResultInstance() instanceof SearchableResult) {
132 // Get a UserDatabaseWrapper instance
133 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
135 // Create a search criteria
136 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
138 // Add the username as a criteria and set limit to one entry
139 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
140 $criteriaInstance->setLimit(1);
142 // Get a search result
143 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
145 // Set the index "solver"
146 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
148 // And finally set it
149 $this->setResultInstance($resultInstance);
153 $this->getResultInstance()->rewind();
156 if ($this->getResultInstance()->next()) {
166 * Determines whether the email exists or not
168 * @return $exists Whether the email exists
170 public function ifEmailAddressExists () {
171 // By default the email does not exist
174 // Is a previous result there?
175 if (!$this->getResultInstance() instanceof SearchableResult) {
176 // Get a UserDatabaseWrapper instance
177 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
179 // Create a search criteria
180 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
182 // Add the username as a criteria and set limit to one entry
183 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
184 $criteriaInstance->setLimit(1);
186 // Get a search result
187 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
189 // Set the index "solver"
190 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
192 // And finally set it
193 $this->setResultInstance($resultInstance);
197 $this->getResultInstance()->rewind();
200 if ($this->getResultInstance()->next()) {
204 // Is the username set?
205 if ($this->getUserName() == '') {
207 $currEntry = $this->getResultInstance()->current();
210 $this->setUserName($currEntry['username']);
219 * Checks if supplied password hash in request matches with the stored in
222 * @param $requestInstance A requestable class instance
223 * @return $matches Whether the supplied password hash matches
225 public function ifPasswordHashMatches (Requestable $requestInstance) {
226 // By default nothing matches... ;)
229 // Is a previous result there?
230 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
231 // Get a UserDatabaseWrapper instance
232 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
234 // Create a search criteria
235 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
237 // Add the username as a criteria and set limit to one entry
238 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
239 $criteriaInstance->setLimit(1);
241 // Get a search result
242 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
244 // Set the index "solver"
245 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
247 // And finally set it
248 $this->setResultInstance($resultInstance);
251 // Rewind it and advance to first entry
252 $this->getResultInstance()->rewind();
254 // This call set the result instance to a clean state
255 $this->getResultInstance()->next();
258 if ($this->getResultInstance()->find('pass_hash')) {
259 // So does the hashes match?
260 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
261 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
269 * "Getter" for user's password hash
271 * @return $passHash User's password hash from database result
273 public final function getPasswordHash () {
274 // Default is missing password hash
277 // Get a database entry
278 $entry = $this->getDatabaseEntry();
280 // Is the password hash there?
281 if (isset($entry['pass_hash'])) {
283 $passHash = $entry['pass_hash'];
286 // And return the hash
291 * Getter for primary key value
293 * @return $primaryValue Value of the primary key based on database type
295 public final function getPrimaryKey () {
296 // Get a user database wrapper
297 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
299 // Get the primary key back from the wrapper
300 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
303 $primaryValue = $this->getField($primaryKey);
306 return $primaryValue;
310 * Updates a given field with new value
312 * @param $fieldName Field to update
313 * @param $fieldValue New value to store
315 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
317 public function updateDatabaseField ($fieldName, $fieldValue) {
318 // Get a critieria instance
319 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
321 // Add search criteria
322 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
323 $searchInstance->setLimit(1);
325 // Now get another criteria
326 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
328 // Add criteria entry which we shall update
329 $updateInstance->addCriteria($fieldName, $fieldValue);
331 // Add the search criteria for searching for the right entry
332 $updateInstance->setSearchInstance($searchInstance);
334 // Set wrapper class name
335 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
337 // Remember the update in database result
338 $this->getResultInstance()->add2UpdateQueue($updateInstance);
342 * Checks whether the user status is 'confirmed'
344 * @return $isConfirmed Whether the user status is 'confirmed'
346 public function isConfirmed () {
348 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
355 * Checks whether the user status is 'guest'
357 * @return $isGuest Whether the user status is 'guest'
359 public function isGuest () {
361 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));