]> git.mxchange.org Git - mailer.git/blobdiff - inc/classes/main/user/class_User.php
Code sync from ship-simu code (all class config entries must end with _class!)
[mailer.git] / inc / classes / main / user / class_User.php
index 53e8b46efec3e7910a5e9b1372b10835bac52a2c..cc9c44348058bd2e9645030a5d2950a22554955e 100644 (file)
  * 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 User extends BaseFrameworkSystem implements ManageableUser {
+class User extends BaseFrameworkSystem implements ManageableUser, Registerable {
        /**
-        * Username
+        * Username of current user
         */
-       private $username = "";
+       private $userName = "";
+
+       /**
+        * Email of current user
+        */
+       private $email = "";
 
        // Exceptions
-       const EXCEPTION_USERNAME_NOT_FOUND = 0xd00;
+       const EXCEPTION_USERNAME_NOT_FOUND   = 0x060;
+       const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x061;
+       const EXCEPTION_USER_PASS_MISMATCH   = 0x062;
 
        /**
-        * Private constructor
+        * Protected constructor
         *
         * @return      void
         */
@@ -46,7 +53,7 @@ class User extends BaseFrameworkSystem implements ManageableUser {
                $this->setObjectDescription("Generic user class");
 
                // Create unique ID number
-               $this->createUniqueID();
+               $this->generateUniqueId();
 
                // Clean up a little
                $this->removeNumberFormaters();
@@ -58,7 +65,7 @@ class User extends BaseFrameworkSystem implements ManageableUser {
         * factory method will check if the username is already taken and if not
         * so it will throw an exception.
         *
-        * @param       $username               Username we need a class instance for
+        * @param       $userName               Username we need a class instance for
         * @return      $userInstance   An instance of this user class
         * @throws      UsernameMissingException        If the username does not exist
         */
@@ -67,7 +74,7 @@ class User extends BaseFrameworkSystem implements ManageableUser {
                $userInstance = new User();
 
                // Set the username
-               $userInstance->setUsername($userName);
+               $userInstance->setUserName($userName);
 
                // Check if the username exists
                if (!$userInstance->ifUsernameExists()) {
@@ -79,23 +86,172 @@ class User extends BaseFrameworkSystem implements ManageableUser {
                return $userInstance;
        }
 
+       /**
+        * Creates an instance of this user class by a provided email address. This
+        * factory method will not check if the email address is there.
+        *
+        * @param       $email                  Email address of the user
+        * @return      $userInstance   An instance of this user class
+        */
+       public final static function createUserByEmail ($email) {
+               // Get a new instance
+               $userInstance = new User();
+
+               // Set the username
+               $userInstance->setEmail($email);
+
+               // Return the instance
+               return $userInstance;
+       }
+
        /**
         * Setter for username
         *
         * @param       $userName       The username to set
         * @return      void
         */
-       protected final function setUsername ($userName) {
-               $this->userNane = $userName;
+       public final function setUserName ($userName) {
+               $this->userName = $userName;
+       }
+
+       /**
+        * Setter for email
+        *
+        * @param       $email  The email to set
+        * @return      void
+        */
+       protected final function setEmail ($email) {
+               $this->email = $email;
        }
 
        /**
         * Getter for username
         *
-        * @return      $userName       The username to set
+        * @return      $userName       The username to get
         */
        public final function getUsername () {
-               return $this->userNane;
+               return $this->userName;
+       }
+
+       /**
+        * Getter for email
+        *
+        * @return      $email  The email to get
+        */
+       public final function getEmail () {
+               return $this->email;
+       }
+
+       /**
+        * Determines wether the username exists or not
+        *
+        * @return      $exists         Wether the username exists
+        */
+       public function ifUsernameExists () {
+               // By default the username does not exist
+               $exists = false;
+
+               // 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
+               $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+               // Search for it
+               if ($result->next()) {
+                       // Entry found
+                       $exists = true;
+               } // END - if
+
+               // Return the status
+               return $exists;
+       }
+
+       /**
+        * Determines wether the email exists or not
+        *
+        * @return      $exists         Wether the email exists
+        */
+       public function ifEmailAddressExists () {
+               // By default the email does not exist
+               $exists = false;
+
+               // 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
+               $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+               // Search for it
+               if ($result->next()) {
+                       // Entry found
+                       $exists = true;
+               } // END - if
+
+               // Return the status
+               return $exists;
+       }
+
+       /**
+        * Checks if the 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
+        */
+       public function ifPasswordHashMatches (Requestable $requestInstance) {
+               // By default nothing matches... ;)
+               $matches = false;
+
+               // 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
+               $result = $wrapperInstance->doSelectByCriteria($criteriaInstance);
+
+               // Search for it
+               if ($result->next()) {
+                       // Get the current entry (can only be one!)
+                       $entry = $result->current();
+
+                       // So does the hashes match?
+                       $matches = ($requestInstance->getRequestElement('pass_hash') === $entry['pass_hash']);
+               } // END - if
+
+               // Return the status
+               return $matches;
+       }
+
+       /**
+        * Adds data for later complete update
+        *
+        * @param       $column         Column we want to update
+        * @param       $value          New value to store in database
+        * @return      void
+        */
+       public function addUpdateData ($column, $value) {
+               $this->partialStub("Column={$column}, value={$value}");
        }
 }