3 namespace CoreFramework\User;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
8 use CoreFramework\Request\Requestable;
9 use CoreFramework\Result\Search\SearchableResult;
12 * A general user class
14 * @author Roland Haeder <webmaster@shipsimu.org>
16 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
17 * @license GNU GPL 3.0 or any newer version
18 * @link http://www.shipsimu.org
20 * This program is free software: you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation, either version 3 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 class BaseUser extends BaseFrameworkSystem implements Updateable {
34 // Exception constances
35 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
36 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
37 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
38 const EXCEPTION_USER_IS_GUEST = 0x153;
41 * Username of current user
43 private $userName = '';
46 * User id of current user
51 * Email of current user
56 * Protected constructor
58 * @param $className Name of the class
61 protected function __construct ($className) {
62 // Call parent constructor
63 parent::__construct($className);
69 * @param $userName The username to set
72 public final function setUserName ($userName) {
73 $this->userName = (string) $userName;
79 * @return $userName The username to get
81 public final function getUserName () {
82 return $this->userName;
88 * @param $userId The user id to set
90 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
92 public final function setUserId ($userId) {
93 $this->userId = $userId;
99 * @return $userId The user id to get
101 public final function getUserId () {
102 return $this->userId;
108 * @param $email The email to set
111 protected final function setEmail ($email) {
112 $this->email = (string) $email;
118 * @return $email The email to get
120 public final function getEmail () {
125 * Determines whether the username exists or not
127 * @return $exists Whether the username exists
129 public function ifUsernameExists () {
130 // By default the username does not exist
133 // Is a previous result there?
134 if (!$this->getResultInstance() instanceof SearchableResult) {
135 // Get a UserDatabaseWrapper instance
136 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
138 // Create a search criteria
139 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
141 // Add the username as a criteria and set limit to one entry
142 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
143 $criteriaInstance->setLimit(1);
145 // Get a search result
146 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
148 // Set the index "solver"
149 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
151 // And finally set it
152 $this->setResultInstance($resultInstance);
156 $this->getResultInstance()->rewind();
159 if ($this->getResultInstance()->next()) {
169 * Determines whether the email exists or not
171 * @return $exists Whether the email exists
173 public function ifEmailAddressExists () {
174 // By default the email does not exist
177 // Is a previous result there?
178 if (!$this->getResultInstance() instanceof SearchableResult) {
179 // Get a UserDatabaseWrapper instance
180 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
182 // Create a search criteria
183 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
185 // Add the username as a criteria and set limit to one entry
186 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
187 $criteriaInstance->setLimit(1);
189 // Get a search result
190 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
192 // Set the index "solver"
193 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
195 // And finally set it
196 $this->setResultInstance($resultInstance);
200 $this->getResultInstance()->rewind();
203 if ($this->getResultInstance()->next()) {
207 // Is the username set?
208 if ($this->getUserName() == '') {
210 $currEntry = $this->getResultInstance()->current();
213 $this->setUserName($currEntry['username']);
222 * Checks if supplied password hash in request matches with the stored in
225 * @param $requestInstance A Requestable class instance
226 * @return $matches Whether the supplied password hash matches
228 public function ifPasswordHashMatches (Requestable $requestInstance) {
229 // By default nothing matches... ;)
232 // Is a previous result there?
233 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
234 // Get a UserDatabaseWrapper instance
235 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
237 // Create a search criteria
238 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
240 // Add the username as a criteria and set limit to one entry
241 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
242 $criteriaInstance->setLimit(1);
244 // Get a search result
245 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
247 // Set the index "solver"
248 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
250 // And finally set it
251 $this->setResultInstance($resultInstance);
254 // Rewind it and advance to first entry
255 $this->getResultInstance()->rewind();
257 // This call set the result instance to a clean state
258 $this->getResultInstance()->next();
261 if ($this->getResultInstance()->find('pass_hash')) {
262 // So does the hashes match?
263 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
264 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
272 * "Getter" for user's password hash
274 * @return $passHash User's password hash from database result
276 public final function getPasswordHash () {
277 // Default is missing password hash
280 // Get a database entry
281 $entry = $this->getDatabaseEntry();
283 // Is the password hash there?
284 if (isset($entry['pass_hash'])) {
286 $passHash = $entry['pass_hash'];
289 // And return the hash
294 * Getter for primary key value
296 * @return $primaryValue Value of the primary key based on database type
298 public final function getPrimaryKey () {
299 // Get a user database wrapper
300 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
302 // Get the primary key back from the wrapper
303 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
306 $primaryValue = $this->getField($primaryKey);
309 return $primaryValue;
313 * Updates a given field with new value
315 * @param $fieldName Field to update
316 * @param $fieldValue New value to store
318 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
320 public function updateDatabaseField ($fieldName, $fieldValue) {
321 // Get a critieria instance
322 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
324 // Add search criteria
325 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
326 $searchInstance->setLimit(1);
328 // Now get another criteria
329 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
331 // Add criteria entry which we shall update
332 $updateInstance->addCriteria($fieldName, $fieldValue);
334 // Add the search criteria for searching for the right entry
335 $updateInstance->setSearchInstance($searchInstance);
337 // Set wrapper class name
338 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
340 // Remember the update in database result
341 $this->getResultInstance()->add2UpdateQueue($updateInstance);
345 * Checks whether the user status is 'confirmed'
347 * @return $isConfirmed Whether the user status is 'confirmed'
349 public function isConfirmed () {
351 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
358 * Checks whether the user status is 'guest'
360 * @return $isGuest Whether the user status is 'guest'
362 public function isGuest () {
364 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));