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 da241f1efe10a07a068a82307583a51b1fd419f6..647ab230c4c0c549231ce63e06de16382ffe231e 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 ee08ff2d2fac526b4dd505533b4c376c3985ac35..37a013934b7bf300cf692ac83037431dccb5b405 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 6e0213a587992098f346d2177fb4205eb604646b..2a422390676ded57758e9778203179efabfad72f 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 e1f89564648c79606b7fc3e6b861395f88789ee7..2ea69048668560d0cf541d0b76a44753f2816324 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 230d1de4a3b45697cc4bf0483d43b46ce8be9e23..94b62a23cd400b6ccf8764e27a9ae3747fd5159e 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 f73696537d3b61be06957766b1516b4fbbf2dfb1..ce67c061f9128cbde18ffbbae13484eb093e6698 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 ec9ef165d5d13e7a8055eba2666acfb1e3a9c9d5..e2911cf3b81709c56e7c094eb4219085c1aeeae9 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 a0bec178bf34f2940dc361f080b1851ea314dfd0..1b939c2013035a4962f3e559d8ed6cba9bfea7b1 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);