Added isGuest() isConfirmed() to BaseUser for wrapping this type of check into
authorRoland Haeder <roland@mxchange.org>
Tue, 7 Apr 2015 22:52:07 +0000 (00:52 +0200)
committerRoland Haeder <roland@mxchange.org>
Tue, 7 Apr 2015 22:52:42 +0000 (00:52 +0200)
small methods. Also these methods are used to check the user instance before it
is being returned to other classes (see their factory methods).

Also 2 new exceptions has been added for above pre-checks.

Signed-off-by: Roland H├Ąder <roland@mxchange.org>
inc/classes/exceptions/user/class_AccountPasswordMismatchException.php
inc/classes/exceptions/user/class_UnexpectedGuestAccountException.php [new file with mode: 0644]
inc/classes/exceptions/user/class_UserEmailMissingException.php
inc/classes/exceptions/user/class_UserNoGuestException.php [new file with mode: 0644]
inc/classes/exceptions/user/class_UserPasswordMismatchException.php
inc/classes/exceptions/user/class_UsernameMissingException.php
inc/classes/main/filter/verifier/class_UserStatusVerifierFilter.php
inc/classes/main/user/class_BaseUser.php
inc/classes/main/user/guest/class_Guest.php
inc/classes/main/user/member/class_Member.php

index da241f1..647ab23 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * A class for mismatching passwords
+ * An exception for mismatching passwords
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
diff --git a/inc/classes/exceptions/user/class_UnexpectedGuestAccountException.php b/inc/classes/exceptions/user/class_UnexpectedGuestAccountException.php
new file mode 100644 (file)
index 0000000..9100f1d
--- /dev/null
@@ -0,0 +1,46 @@
+<?php
+/**
+ * An exception for unexpected guest accounts
+ *
+ * @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 UnexpectedGuestAccountException extends FrameworkException {
+       /**
+        * The super constructor for all exceptions
+        *
+        * @param       $msgArray       The non-optional message for the exception
+        * @param       $code           An optional code for better debugging
+        * @return      void
+        */
+       public function __construct (array $msgArray, $code = 0) {
+               // Create the message
+               $message = sprintf('[%s:%d] User %s is a guest account.',
+                       $msgArray[0]->__toString(),
+                       $this->getLine(),
+                       $msgArray[1]
+               );
+
+               // Make sure everything is assigned properly
+               parent::__construct($message, $code);
+       }
+}
+
+// [EOF]
+?>
index ee08ff2..37a0139 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * A class for non-existing user emails
+ * An exception for non-existing user emails
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
diff --git a/inc/classes/exceptions/user/class_UserNoGuestException.php b/inc/classes/exceptions/user/class_UserNoGuestException.php
new file mode 100644 (file)
index 0000000..515431a
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+/**
+ * An exception for non-guest accounts (but guest was expected)
+ *
+ * @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 UserNoGuestException extends FrameworkException {
+       /**
+        * The super constructor for all exceptions
+        *
+        * @param       $msgArray       The non-optional message for the exception
+        * @param       $code           An optional code for better debugging
+        * @return      void
+        */
+       public function __construct (array $msgArray, $code = 0) {
+               // Create the message
+               $message = sprintf('[%s:%d] User %s is not a guest account: %s',
+                       $msgArray[0]->__toString(),
+                       $this->getLine(),
+                       $msgArray[1],
+                       $msgArray[0]->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS)
+               );
+
+               // Make sure everything is assigned properly
+               parent::__construct($message, $code);
+       }
+}
+
+// [EOF]
+?>
index 6e0213a..2a42239 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * A class for mismatching passwords
+ * An exception for mismatching passwords
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
index e1f8956..2ea6904 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * A class for non-existing usernames
+ * An exception for non-existing usernames
  *
  * @author             Roland Haeder <webmaster@shipsimu.org>
  * @version            0.0.0
index 230d1de..94b62a2 100644 (file)
@@ -57,7 +57,7 @@ class UserStatusVerifierFilter extends BaseFilter implements Filterable {
                $userInstance = Registry::getRegistry()->getInstance('user');
 
                // Is the user account confirmed?
-               if (($userInstance->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) != $this->getConfigInstance()->getConfigEntry('user_status_confirmed')) && ($userInstance->getField(UserDatabaseWrapper::DB_COLUMN_USER_STATUS) != $this->getConfigInstance()->getConfigEntry('user_status_guest')) && ($requestInstance->getRequestElement('action') != $this->getConfigInstance()->getConfigEntry('action_status_problem'))) {
+               if ((!$userInstance->isConfirmed()) && (!$userInstance->isGuest()) && ($requestInstance->getRequestElement('action') != $this->getConfigInstance()->getConfigEntry('action_status_problem'))) {
                        // Request is invalid!
                        $requestInstance->requestIsValid(FALSE);
 
index f736965..ce67c06 100644 (file)
@@ -330,6 +330,32 @@ class BaseUser extends BaseFrameworkSystem implements Updateable {
                // 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]
index ec9ef16..e2911cf 100644 (file)
@@ -26,6 +26,7 @@ class Guest extends BaseUser implements ManageableGuest, Registerable {
        const EXCEPTION_USERNAME_NOT_FOUND   = 0x170;
        const EXCEPTION_USER_EMAIL_NOT_FOUND = 0x171;
        const EXCEPTION_USER_PASS_MISMATCH   = 0x172;
+       const EXCEPTION_USER_NOT_GUEST       = 0x173;
 
        /**
         * Protected constructor
@@ -45,6 +46,7 @@ class Guest extends BaseUser implements ManageableGuest, Registerable {
         * @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
+        * @throws      UserNoGuestException            If the user is no guest account
         */
        public static final function createGuestByUsername ($userName) {
                // Get a new instance
@@ -57,7 +59,10 @@ class Guest extends BaseUser implements ManageableGuest, Registerable {
                if ($userInstance->ifUsernameExists() === FALSE) {
                        // Throw an exception here
                        throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
-               } // END - if
+               } elseif ($userInstance->isGuest() === FALSE) {
+                       // Sanity check on 'guest' status failed
+                       throw new UserNoGuestException(array($userInstance, $userName), self::EXCEPTION_USER_NOT_GUEST_STATUS);
+               }
 
                // Return the instance
                return $userInstance;
index a0bec17..1b939c2 100644 (file)
@@ -40,6 +40,7 @@ class Member extends BaseUser implements ManageableMember, Registerable {
         * @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
+        * @throws      UnexpectedGuestAccountException         If the user status is 'guest'
         */
        public static final function createMemberByUsername ($userName) {
                // Get a new instance
@@ -52,7 +53,10 @@ class Member extends BaseUser implements ManageableMember, Registerable {
                if ($userInstance->ifUsernameExists() === FALSE) {
                        // Throw an exception here
                        throw new UsernameMissingException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
-               } // END - if
+               } elseif ($userInstance->isGuest()) === TRUE) {
+                       // User should not be a guest here
+                       throw new UnexpectedGuestAccountException(array($userInstance, $userName), self::EXCEPTION_USERNAME_NOT_FOUND);
+               }
 
                // Return the instance
                return $userInstance;
@@ -130,8 +134,8 @@ class Member extends BaseUser implements ManageableMember, Registerable {
                $updateInstance = ObjectFactory::createObjectByConfiguredName('update_criteria_class');
 
                // And add our both entries
-               $updateInstance->addCriteria("last_activity", date("Y-m-d H:i:s", time()));
-               $updateInstance->addCriteria("last_action", $lastAction);
+               $updateInstance->addCriteria('last_activity', date('Y-m-d H:i:s', time()));
+               $updateInstance->addCriteria('last_action', $lastAction);
 
                // Add the search criteria for searching for the right entry
                $updateInstance->setSearchInstance($searchInstance);