3 namespace CoreFramework\User;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Object\BaseFrameworkSystem;
10 * A general user class
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class BaseUser extends BaseFrameworkSystem implements Updateable {
32 // Exception constances
33 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
34 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
35 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
36 const EXCEPTION_USER_IS_GUEST = 0x153;
39 * Username of current user
41 private $userName = '';
44 * User id of current user
49 * Email of current user
54 * Protected constructor
56 * @param $className Name of the class
59 protected function __construct ($className) {
60 // Call parent constructor
61 parent::__construct($className);
67 * @param $userName The username to set
70 public final function setUserName ($userName) {
71 $this->userName = (string) $userName;
77 * @return $userName The username to get
79 public final function getUserName () {
80 return $this->userName;
86 * @param $userId The user id to set
88 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
90 public final function setUserId ($userId) {
91 $this->userId = $userId;
97 * @return $userId The user id to get
99 public final function getUserId () {
100 return $this->userId;
106 * @param $email The email to set
109 protected final function setEmail ($email) {
110 $this->email = (string) $email;
116 * @return $email The email to get
118 public final function getEmail () {
123 * Determines whether the username exists or not
125 * @return $exists Whether the username exists
127 public function ifUsernameExists () {
128 // By default the username does not exist
131 // Is a previous result there?
132 if (!$this->getResultInstance() instanceof SearchableResult) {
133 // Get a UserDatabaseWrapper instance
134 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
136 // Create a search criteria
137 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
139 // Add the username as a criteria and set limit to one entry
140 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
141 $criteriaInstance->setLimit(1);
143 // Get a search result
144 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
146 // Set the index "solver"
147 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
149 // And finally set it
150 $this->setResultInstance($resultInstance);
154 $this->getResultInstance()->rewind();
157 if ($this->getResultInstance()->next()) {
167 * Determines whether the email exists or not
169 * @return $exists Whether the email exists
171 public function ifEmailAddressExists () {
172 // By default the email does not exist
175 // Is a previous result there?
176 if (!$this->getResultInstance() instanceof SearchableResult) {
177 // Get a UserDatabaseWrapper instance
178 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
180 // Create a search criteria
181 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
183 // Add the username as a criteria and set limit to one entry
184 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
185 $criteriaInstance->setLimit(1);
187 // Get a search result
188 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
190 // Set the index "solver"
191 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
193 // And finally set it
194 $this->setResultInstance($resultInstance);
198 $this->getResultInstance()->rewind();
201 if ($this->getResultInstance()->next()) {
205 // Is the username set?
206 if ($this->getUserName() == '') {
208 $currEntry = $this->getResultInstance()->current();
211 $this->setUserName($currEntry['username']);
220 * Checks if supplied password hash in request matches with the stored in
223 * @param $requestInstance A requestable class instance
224 * @return $matches Whether the supplied password hash matches
226 public function ifPasswordHashMatches (Requestable $requestInstance) {
227 // By default nothing matches... ;)
230 // Is a previous result there?
231 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
232 // Get a UserDatabaseWrapper instance
233 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
235 // Create a search criteria
236 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
238 // Add the username as a criteria and set limit to one entry
239 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
240 $criteriaInstance->setLimit(1);
242 // Get a search result
243 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
245 // Set the index "solver"
246 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
248 // And finally set it
249 $this->setResultInstance($resultInstance);
252 // Rewind it and advance to first entry
253 $this->getResultInstance()->rewind();
255 // This call set the result instance to a clean state
256 $this->getResultInstance()->next();
259 if ($this->getResultInstance()->find('pass_hash')) {
260 // So does the hashes match?
261 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
262 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
270 * "Getter" for user's password hash
272 * @return $passHash User's password hash from database result
274 public final function getPasswordHash () {
275 // Default is missing password hash
278 // Get a database entry
279 $entry = $this->getDatabaseEntry();
281 // Is the password hash there?
282 if (isset($entry['pass_hash'])) {
284 $passHash = $entry['pass_hash'];
287 // And return the hash
292 * Getter for primary key value
294 * @return $primaryValue Value of the primary key based on database type
296 public final function getPrimaryKey () {
297 // Get a user database wrapper
298 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
300 // Get the primary key back from the wrapper
301 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
304 $primaryValue = $this->getField($primaryKey);
307 return $primaryValue;
311 * Updates a given field with new value
313 * @param $fieldName Field to update
314 * @param $fieldValue New value to store
316 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
318 public function updateDatabaseField ($fieldName, $fieldValue) {
319 // Get a critieria instance
320 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
322 // Add search criteria
323 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
324 $searchInstance->setLimit(1);
326 // Now get another criteria
327 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
329 // Add criteria entry which we shall update
330 $updateInstance->addCriteria($fieldName, $fieldValue);
332 // Add the search criteria for searching for the right entry
333 $updateInstance->setSearchInstance($searchInstance);
335 // Set wrapper class name
336 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
338 // Remember the update in database result
339 $this->getResultInstance()->add2UpdateQueue($updateInstance);
343 * Checks whether the user status is 'confirmed'
345 * @return $isConfirmed Whether the user status is 'confirmed'
347 public function isConfirmed () {
349 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
356 * Checks whether the user status is 'guest'
358 * @return $isGuest Whether the user status is 'guest'
360 public function isGuest () {
362 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));