]> git.mxchange.org Git - shipsimu.git/blobdiff - inc/classes/main/user/class_BaseUser.php
A lot rewrites and fixes for weak redirect methods
[shipsimu.git] / inc / classes / main / user / class_BaseUser.php
index e7e4541e70623d0dcaf99a6150777ece46fa41b7..b00a0730f4cfe6b1d4da5cc1057e25730e8f87bf 100644 (file)
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  */
 class BaseUser extends BaseFrameworkSystem {
-       /**
-        * Instance of the database result
-        */
-       private $resultInstance = null;
+       // Exception constances
+       const EXCEPTION_USERNAME_NOT_FOUND   = 0x150;
+       const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x151;
+       const EXCEPTION_USER_PASS_MISMATCH   = 0x152;
 
        /**
         * Username of current user
         */
        private $userName = "";
 
+       /**
+        * User id of current user
+        */
+       private $userId = 0;
+
        /**
         * Email of current user
         */
@@ -53,82 +58,61 @@ class BaseUser extends BaseFrameworkSystem {
        }
 
        /**
-        * "Getter" for databse entry
+        * Setter for username
         *
-        * @return      $entry  An array with database entries
-        * @throws      NullPointerException    If the database result is not found
-        * @throws      InvalidDatabaseResultException  If the database result is invalid
+        * @param       $userName       The username to set
+        * @return      void
         */
-       private function getDatabaseEntry () {
-               // Is there an instance?
-               if (is_null($this->getResultInstance())) {
-                       // Throw new exception
-                       throw new NullPointerException($this, self::EXCEPTION_IS_NULL_POINTER);
-               } // END - if
-
-               // Rewind it
-               $this->getResultInstance()->rewind();
-
-               // Do we have an entry?
-               if (!$this->getResultInstance()->valid()) {
-                       throw new InvalidDatabaseResultException(array($this, $this->resultInstance), DatabaseResult::EXCEPTION_INVALID_DATABASE_RESULT);
-               } // END - if
-
-               // Get next entry
-               $this->getResultInstance()->next();
-
-               // Fetch it
-               $entry = $this->getResultInstance()->current();
-
-               // And return it
-               return $entry;
+       public final function setUserName ($userName) {
+               $this->userName = (string) $userName;
        }
 
        /**
-        * Setter for username
+        * Getter for username
         *
-        * @param       $userName       The username to set
-        * @return      void
+        * @return      $userName       The username to get
         */
-       public final function setUserName ($userName) {
-               $this->userName = $userName;
+       public final function getUserName () {
+               return $this->userName;
        }
 
        /**
-        * Setter for email
+        * Setter for user id
         *
-        * @param       $email  The email to set
+        * @param       $userId         The user id to set
         * @return      void
+        * @todo        Find a way of casting here. "(int)" might destroy the user id > 32766
         */
-       protected final function setEmail ($email) {
-               $this->email = $email;
+       public final function setUserId ($userId) {
+               $this->userId = $userId;
        }
 
        /**
-        * Getter for username
+        * Getter for user id
         *
-        * @return      $userName       The username to get
+        * @return      $userId The user id to get
         */
-       public final function getUsername () {
-               return $this->userName;
+       public final function getUserId () {
+               return $this->userId;
        }
 
        /**
-        * Getter for email
+        * Setter for email
         *
-        * @return      $email  The email to get
+        * @param       $email  The email to set
+        * @return      void
         */
-       public final function getEmail () {
-               return $this->email;
+       protected final function setEmail ($email) {
+               $this->email = (string) $email;
        }
 
        /**
-        * Getter for database result instance
+        * Getter for email
         *
-        * @return      $resultInstance         An instance of a database result class
+        * @return      $email  The email to get
         */
-       protected final function getResultInstance () {
-               return $this->resultInstance;
+       public final function getEmail () {
+               return $this->email;
        }
 
        /**
@@ -149,15 +133,21 @@ class BaseUser extends BaseFrameworkSystem {
                        $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->addCriteria(UserDatabaseWrapper::DB_COLUMN_USERNAME, $this->getUserName());
                        $criteriaInstance->setLimit(1);
 
                        // Get a search result
-                       $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
-               } else {
-                       // Rewind it
-                       $this->getResultInstance()->rewind();
-               }
+                       $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()) {
@@ -190,17 +180,32 @@ class BaseUser extends BaseFrameworkSystem {
                        $criteriaInstance->addCriteria(UserDatabaseWrapper::DB_COLUMN_EMAIL, $this->getEmail());
                        $criteriaInstance->setLimit(1);
 
-                       // Get a search resultInstance
-                       $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
-               } else {
-                       // Rewind it
-                       $this->getResultInstance()->rewind();
-               }
+                       // 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
@@ -208,8 +213,8 @@ class BaseUser extends BaseFrameworkSystem {
        }
 
        /**
-        * Checks if the supplied password hash in request matches with the stored
-        * in database.
+        * Checks if supplied password hash in request matches with the stored in
+        * database.
         *
         * @param       $requestInstance        A requestable class instance
         * @return      $matches                        Wether the supplied password hash matches
@@ -218,27 +223,36 @@ class BaseUser extends BaseFrameworkSystem {
                // By default nothing matches... ;)
                $matches = false;
 
-               // Get a UserDatabaseWrapper instance
-               $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
+               // Is a previous result there?
+               if (is_null($this->getResultInstance())) {
+                       // Get a UserDatabaseWrapper instance
+                       $wrapperInstance = ObjectFactory::createObjectByConfiguredName('user_db_wrapper_class');
 
-               // Create a search criteria
-               $criteriaInstance = ObjectFactory::createObjectByConfiguredName('search_criteria_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);
+                       // 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 resultInstance
-               $this->resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+                       // Get a search result
+                       $resultInstance = $wrapperInstance->doSelectByCriteria($criteriaInstance);
 
-               // Search for it
-               if ($this->getResultInstance()->next()) {
-                       // Get the current entry (can only be one!)
-                       $entry = $this->getResultInstance()->current();
+                       // 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()->find('pass_hash')) {
                        // So does the hashes match?
                        //* DEBUG: */ echo $requestInstance->getRequestElement('pass_hash')."/".$entry['pass_hash'];
-                       $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
+                       $matches = ($requestInstance->getRequestElement('pass_hash') === $this->getResultInstance()->getFoundValue());
                } // END - if
 
                // Return the status
@@ -261,36 +275,12 @@ class BaseUser extends BaseFrameworkSystem {
                if (isset($entry['pass_hash'])) {
                        // Get it
                        $passHash = $entry['pass_hash'];
-               }
+               } // END - if
 
                // And return the hash
                return $passHash;
        }
 
-       /**
-        * Getter for field name
-        *
-        * @param       $fieldName              Field name which we shall get
-        * @return      $fieldValue             Field value from the user
-        * @todo        Do we need to secure this here against missing results?
-        */
-       public final function getField ($fieldName) {
-               // Default field value
-               $fieldValue = null;
-
-               // Get current array
-               $fieldArray = $this->getResultInstance()->current();
-
-               // Does the field exist?
-               if (isset($fieldArray[$fieldName])) {
-                       // Get it
-                       $fieldValue = $fieldArray[$fieldName];
-               } // END - if
-
-               // Return it
-               return $fieldValue;
-       }
-
        /**
         * Getter for primary key value
         *
@@ -309,6 +299,41 @@ class BaseUser extends BaseFrameworkSystem {
                // 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
+        * @throws      DatabaseUpdateSupportException  If this class does not support database updates
+        */
+       public function updateDatabaseField ($fieldName, $fieldValue) {
+               // Is updating database fields allowed by interface?
+               if (!$this instanceof Updateable) {
+                       // Update not supported!
+                       throw new DatabaseUpdateSupportException($this, self::EXCEPTION_DATABASE_UPDATED_NOT_ALLOWED);
+               } // END - if
+
+               // 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');
+
+               // And add our both entries
+               $updateInstance->addCriteria($fieldName, $fieldValue);
+
+               // Add the search criteria for searching for the right entry
+               $updateInstance->setSearchInstance($searchInstance);
+
+               // Remember the update in database result
+               $this->getResultInstance()->add2UpdateQueue($updateInstance);
+       }
 }
 
 // [EOF]