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;
10 use CoreFramework\Wrapper\Database\User\UserDatabaseWrapper;
13 * A general user class
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class BaseUser extends BaseFrameworkSystem implements Updateable {
35 // Exception constances
36 const EXCEPTION_USERNAME_NOT_FOUND = 0x150;
37 const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
38 const EXCEPTION_USER_PASS_MISMATCH = 0x152;
39 const EXCEPTION_USER_IS_GUEST = 0x153;
42 * Username of current user
44 private $userName = '';
47 * User id of current user
52 * Email of current user
57 * Protected constructor
59 * @param $className Name of the class
62 protected function __construct ($className) {
63 // Call parent constructor
64 parent::__construct($className);
70 * @param $userName The username to set
73 public final function setUserName ($userName) {
74 $this->userName = (string) $userName;
80 * @return $userName The username to get
82 public final function getUserName () {
83 return $this->userName;
89 * @param $userId The user id to set
91 * @todo Find a way of casting here. "(int)" might destroy the user id > 32766
93 public final function setUserId ($userId) {
94 $this->userId = $userId;
100 * @return $userId The user id to get
102 public final function getUserId () {
103 return $this->userId;
109 * @param $email The email to set
112 protected final function setEmail ($email) {
113 $this->email = (string) $email;
119 * @return $email The email to get
121 public final function getEmail () {
126 * Determines whether the username exists or not
128 * @return $exists Whether the username exists
130 public function ifUsernameExists () {
131 // By default the username does not exist
134 // Is a previous result there?
135 if (!$this->getResultInstance() instanceof SearchableResult) {
136 // Get a UserDatabaseWrapper instance
137 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
139 // Create a search criteria
140 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
142 // Add the username as a criteria and set limit to one entry
143 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
144 $criteriaInstance->setLimit(1);
146 // Get a search result
147 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
149 // Set the index "solver"
150 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
152 // And finally set it
153 $this->setResultInstance($resultInstance);
157 $this->getResultInstance()->rewind();
160 if ($this->getResultInstance()->next()) {
170 * Determines whether the email exists or not
172 * @return $exists Whether the email exists
174 public function ifEmailAddressExists () {
175 // By default the email does not exist
178 // Is a previous result there?
179 if (!$this->getResultInstance() instanceof SearchableResult) {
180 // Get a UserDatabaseWrapper instance
181 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
183 // Create a search criteria
184 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
186 // Add the username as a criteria and set limit to one entry
187 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
188 $criteriaInstance->setLimit(1);
190 // Get a search result
191 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
193 // Set the index "solver"
194 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
196 // And finally set it
197 $this->setResultInstance($resultInstance);
201 $this->getResultInstance()->rewind();
204 if ($this->getResultInstance()->next()) {
208 // Is the username set?
209 if ($this->getUserName() == '') {
211 $currEntry = $this->getResultInstance()->current();
214 $this->setUserName($currEntry['username']);
223 * Checks if supplied password hash in request matches with the stored in
226 * @param $requestInstance A Requestable class instance
227 * @return $matches Whether the supplied password hash matches
229 public function ifPasswordHashMatches (Requestable $requestInstance) {
230 // By default nothing matches... ;)
233 // Is a previous result there?
234 if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
235 // Get a UserDatabaseWrapper instance
236 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
238 // Create a search criteria
239 $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
241 // Add the username as a criteria and set limit to one entry
242 $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
243 $criteriaInstance->setLimit(1);
245 // Get a search result
246 $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
248 // Set the index "solver"
249 $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
251 // And finally set it
252 $this->setResultInstance($resultInstance);
255 // Rewind it and advance to first entry
256 $this->getResultInstance()->rewind();
258 // This call set the result instance to a clean state
259 $this->getResultInstance()->next();
262 if ($this->getResultInstance()->find('pass_hash')) {
263 // So does the hashes match?
264 //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
265 $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
273 * "Getter" for user's password hash
275 * @return $passHash User's password hash from database result
277 public final function getPasswordHash () {
278 // Default is missing password hash
281 // Get a database entry
282 $entry = $this->getDatabaseEntry();
284 // Is the password hash there?
285 if (isset($entry['pass_hash'])) {
287 $passHash = $entry['pass_hash'];
290 // And return the hash
295 * Getter for primary key value
297 * @return $primaryValue Value of the primary key based on database type
299 public final function getPrimaryKey () {
300 // Get a user database wrapper
301 $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
303 // Get the primary key back from the wrapper
304 $primaryKey = $wrapperInstance->getPrimaryKeyValue();
307 $primaryValue = $this->getField($primaryKey);
310 return $primaryValue;
314 * Updates a given field with new value
316 * @param $fieldName Field to update
317 * @param $fieldValue New value to store
319 * @todo Try to make this method more generic so we can move it in BaseFrameworkSystem
321 public function updateDatabaseField ($fieldName, $fieldValue) {
322 // Get a critieria instance
323 $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
325 // Add search criteria
326 $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
327 $searchInstance->setLimit(1);
329 // Now get another criteria
330 $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
332 // Add criteria entry which we shall update
333 $updateInstance->addCriteria($fieldName, $fieldValue);
335 // Add the search criteria for searching for the right entry
336 $updateInstance->setSearchInstance($searchInstance);
338 // Set wrapper class name
339 $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
341 // Remember the update in database result
342 $this->getResultInstance()->add2UpdateQueue($updateInstance);
346 * Checks whether the user status is 'confirmed'
348 * @return $isConfirmed Whether the user status is 'confirmed'
350 public function isConfirmed () {
352 $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
359 * Checks whether the user status is 'guest'
361 * @return $isGuest Whether the user status is 'guest'
363 public function isGuest () {
365 $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));