Renamed classes/main/ to main/classes/ + added FuseFeature, an upcoming feature
[core.git] / inc / main / classes / user / class_BaseUser.php
diff --git a/inc/main/classes/user/class_BaseUser.php b/inc/main/classes/user/class_BaseUser.php
new file mode 100644 (file)
index 0000000..bc7988a
--- /dev/null
@@ -0,0 +1,363 @@
+<?php
+/**
+ * A general user class 
+ *
+ * @author             Roland Haeder <webmaster@shipsimu.org>
+ * @version            0.0.0
+ * @copyright  Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2015 Core Developer Team
+ * @license            GNU GPL 3.0 or any newer version
+ * @link               http://www.shipsimu.org
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+class BaseUser extends BaseFrameworkSystem implements Updateable {
+       // Exception constances
+       const EXCEPTION_USERNAME_NOT_FOUND   = 0x150;
+       const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
+       const EXCEPTION_USER_PASS_MISMATCH   = 0x152;
+       const EXCEPTION_USER_IS_GUEST        = 0x153;
+
+       /**
+        * Username of current user
+        */
+       private $userName = '';
+
+       /**
+        * User id of current user
+        */
+       private $userId = 0;
+
+       /**
+        * Email of current user
+        */
+       private $email = '';
+
+       /**
+        * Protected constructor
+        *
+        * @param       $className      Name of the class
+        * @return      void
+        */
+       protected function __construct ($className) {
+               // Call parent constructor
+               parent::__construct($className);
+       }
+
+       /**
+        * Setter for username
+        *
+        * @param       $userName       The username to set
+        * @return      void
+        */
+       public final function setUserName ($userName) {
+               $this->userName = (string) $userName;
+       }
+
+       /**
+        * Getter for username
+        *
+        * @return      $userName       The username to get
+        */
+       public final function getUserName () {
+               return $this->userName;
+       }
+
+       /**
+        * Setter for user id
+        *
+        * @param       $userId         The user id to set
+        * @return      void
+        * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
+        */
+       public final function setUserId ($userId) {
+               $this->userId = $userId;
+       }
+
+       /**
+        * Getter for user id
+        *
+        * @return      $userId The user id to get
+        */
+       public final function getUserId () {
+               return $this->userId;
+       }
+
+       /**
+        * Setter for email
+        *
+        * @param       $email  The email to set
+        * @return      void
+        */
+       protected final function setEmail ($email) {
+               $this->email = (string) $email;
+       }
+
+       /**
+        * Getter for email
+        *
+        * @return      $email  The email to get
+        */
+       public final function getEmail () {
+               return $this->email;
+       }
+
+       /**
+        * Determines whether the username exists or not
+        *
+        * @return      $exists         Whether the username exists
+        */
+       public function ifUsernameExists () {
+               // By default the username does not exist
+               $exists = FALSE;
+
+               // Is a previous result there?
+               if (!$this->getResultInstance() instanceof SearchableResult) {
+                       // Get a UserDatabaseWrapper instance
+                       $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
+
+                       // Create a search criteria
+                       $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+                       // Add the username as a criteria and set limit to one entry
+                       $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
+                       $criteriaInstance->setLimit(1);
+
+                       // Get a search result
+                       $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+                       // Set the index "solver"
+                       $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
+
+                       // And finally set it
+                       $this->setResultInstance($resultInstance);
+               } // END - if
+
+               // Rewind it
+               $this->getResultInstance()->rewind();
+
+               // Search for it
+               if ($this->getResultInstance()->next()) {
+                       // Entry found
+                       $exists = TRUE;
+               } // END - if
+
+               // Return the status
+               return $exists;
+       }
+
+       /**
+        * Determines whether the email exists or not
+        *
+        * @return      $exists         Whether the email exists
+        */
+       public function ifEmailAddressExists () {
+               // By default the email does not exist
+               $exists = FALSE;
+
+               // Is a previous result there?
+               if (!$this->getResultInstance() instanceof SearchableResult) {
+                       // Get a UserDatabaseWrapper instance
+                       $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
+
+                       // Create a search criteria
+                       $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+                       // Add the username as a criteria and set limit to one entry
+                       $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
+                       $criteriaInstance->setLimit(1);
+
+                       // Get a search result
+                       $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+                       // Set the index "solver"
+                       $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
+
+                       // And finally set it
+                       $this->setResultInstance($resultInstance);
+               } // END - if
+
+               // Rewind it
+               $this->getResultInstance()->rewind();
+
+               // Search for it
+               if ($this->getResultInstance()->next()) {
+                       // Entry found
+                       $exists = TRUE;
+
+                       // Is the username set?
+                       if ($this->getUserName() == '') {
+                               // Get current entry
+                               $currEntry = $this->getResultInstance()->current();
+
+                               // Set the username
+                               $this->setUserName($currEntry['username']);
+                       } // END - if
+               } // END - if
+
+               // Return the status
+               return $exists;
+       }
+
+       /**
+        * Checks if supplied password hash in request matches with the stored in
+        * database.
+        *
+        * @param       $requestInstance        A requestable class instance
+        * @return      $matches                        Whether the supplied password hash matches
+        */
+       public function ifPasswordHashMatches (Requestable $requestInstance) {
+               // By default nothing matches... ;)
+               $matches = FALSE;
+
+               // Is a previous result there?
+               if ((!$this->getResultInstance() instanceof SearchableResult) || ($this->getResultInstance()->count() == 0)) {
+                       // Get a UserDatabaseWrapper instance
+                       $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
+
+                       // Create a search criteria
+                       $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+                       // Add the username as a criteria and set limit to one entry
+                       $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
+                       $criteriaInstance->setLimit(1);
+
+                       // Get a search result
+                       $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+                       // Set the index "solver"
+                       $resultInstance->solveResultIndex(UserDatabaseWrapper::DB_COLUMN_USERID, $wrapperInstance, array($this, 'setUserId'));
+
+                       // And finally set it
+                       $this->setResultInstance($resultInstance);
+               } // END - if
+
+               // Rewind it and advance to first entry
+               $this->getResultInstance()->rewind();
+
+               // This call set the result instance to a clean state
+               $this->getResultInstance()->next();
+
+               // Search for it
+               if ($this->getResultInstance()->find('pass_hash')) {
+                       // So does the hashes match?
+                       //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash') . '<br />' . $this->getResultInstance()->getFoundValue() . '<br />';
+                       $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
+               } // END - if
+
+               // Return the status
+               return $matches;
+       }
+
+       /**
+        * "Getter" for user's password hash
+        *
+        * @return      $passHash       User's password hash from database result
+        */
+       public final function getPasswordHash () {
+               // Default is missing password hash
+               $passHash = NULL;
+
+               // Get a database entry
+               $entry = $this->getDatabaseEntry();
+
+               // Is the password hash there?
+               if (isset($entry['pass_hash'])) {
+                       // Get it
+                       $passHash = $entry['pass_hash'];
+               } // END - if
+
+               // And return the hash
+               return $passHash;
+       }
+
+       /**
+        * Getter for primary key value
+        *
+        * @return      $primaryValue   Value of the primary key based on database type
+        */
+       public final function getPrimaryKey () {
+               // Get a user database wrapper
+               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
+
+               // Get the primary key back from the wrapper
+               $primaryKey = $wrapperInstance->getPrimaryKeyValue();
+
+               // Get that field
+               $primaryValue = $this->getField($primaryKey);
+
+               // Return the value
+               return $primaryValue;
+       }
+
+       /**
+        * Updates a given field with new value
+        *
+        * @param       $fieldName              Field to update
+        * @param       $fieldValue             New value to store
+        * @return      void
+        * @todo        Try to make this method more generic so we can move it in BaseFrameworkSystem
+        */
+       public function updateDatabaseField ($fieldName, $fieldValue) {
+               // Get a critieria instance
+               $searchInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_class');
+
+               // Add search criteria
+               $searchInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
+               $searchInstance->setLimit(1);
+
+               // Now get another criteria
+               $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
+
+               // Add criteria entry which we shall update
+               $updateInstance->addCriteria($fieldName, $fieldValue);
+
+               // Add the search criteria for searching for the right entry
+               $updateInstance->setSearchInstance($searchInstance);
+
+               // Set wrapper class name
+               $updateInstance->setWrapperConfigEntry('user_db_wrapper_class');
+
+               // Remember the update in database result
+               $this->getResultInstance()->add2UpdateQueue($updateInstance);
+       }
+
+       /**
+        * Checks whether the user status is 'confirmed'
+        *
+        * @return      $isConfirmed    Whether the user status is 'confirmed'
+        */
+       public function isConfirmed () {
+               // Determine it
+               $isConfirmed = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_confirmed'));
+
+               // Return it
+               return $isConfirmed;
+       }
+
+       /**
+        * Checks whether the user status is 'guest'
+        *
+        * @return      $isGuest        Whether the user status is 'guest'
+        */
+       public function isGuest () {
+               // Determine it
+               $isGuest = ($this->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) == $this->getConfigInstance()->getConfigEntry('user_status_guest'));
+
+               // Return it
+               return $isGuest;
+       }
+}
+
+// [EOF]
+?>