From: Roland Haeder Date: Tue, 7 Apr 2015 22:52:07 +0000 (+0200) Subject: Added isGuest() isConfirmed() to BaseUser for wrapping this type of check into X-Git-Url: https://git.mxchange.org/?p=core.git;a=commitdiff_plain;h=cbd2f71aee1c3daca3d11acc346c79757852316f;ds=sidebyside Added isGuest() isConfirmed() to BaseUser for wrapping this type of check into 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 --- diff --git a/inc/classes/exceptions/user/class_AccountPasswordMismatchException.php b/inc/classes/exceptions/user/class_AccountPasswordMismatchException.php index da241f1e..647ab230 100644 --- a/inc/classes/exceptions/user/class_AccountPasswordMismatchException.php +++ b/inc/classes/exceptions/user/class_AccountPasswordMismatchException.php @@ -1,6 +1,6 @@ * @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 index 00000000..9100f1dd --- /dev/null +++ b/inc/classes/exceptions/user/class_UnexpectedGuestAccountException.php @@ -0,0 +1,46 @@ + + * @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 . + */ +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] +?> diff --git a/inc/classes/exceptions/user/class_UserEmailMissingException.php b/inc/classes/exceptions/user/class_UserEmailMissingException.php index ee08ff2d..37a01393 100644 --- a/inc/classes/exceptions/user/class_UserEmailMissingException.php +++ b/inc/classes/exceptions/user/class_UserEmailMissingException.php @@ -1,6 +1,6 @@ * @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 index 00000000..515431a7 --- /dev/null +++ b/inc/classes/exceptions/user/class_UserNoGuestException.php @@ -0,0 +1,47 @@ + + * @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 . + */ +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] +?> diff --git a/inc/classes/exceptions/user/class_UserPasswordMismatchException.php b/inc/classes/exceptions/user/class_UserPasswordMismatchException.php index 6e0213a5..2a422390 100644 --- a/inc/classes/exceptions/user/class_UserPasswordMismatchException.php +++ b/inc/classes/exceptions/user/class_UserPasswordMismatchException.php @@ -1,6 +1,6 @@ * @version 0.0.0 diff --git a/inc/classes/exceptions/user/class_UsernameMissingException.php b/inc/classes/exceptions/user/class_UsernameMissingException.php index e1f89564..2ea69048 100644 --- a/inc/classes/exceptions/user/class_UsernameMissingException.php +++ b/inc/classes/exceptions/user/class_UsernameMissingException.php @@ -1,6 +1,6 @@ * @version 0.0.0 diff --git a/inc/classes/main/filter/verifier/class_UserStatusVerifierFilter.php b/inc/classes/main/filter/verifier/class_UserStatusVerifierFilter.php index 230d1de4..94b62a23 100644 --- a/inc/classes/main/filter/verifier/class_UserStatusVerifierFilter.php +++ b/inc/classes/main/filter/verifier/class_UserStatusVerifierFilter.php @@ -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); diff --git a/inc/classes/main/user/class_BaseUser.php b/inc/classes/main/user/class_BaseUser.php index f7369653..ce67c061 100644 --- a/inc/classes/main/user/class_BaseUser.php +++ b/inc/classes/main/user/class_BaseUser.php @@ -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] diff --git a/inc/classes/main/user/guest/class_Guest.php b/inc/classes/main/user/guest/class_Guest.php index ec9ef165..e2911cf3 100644 --- a/inc/classes/main/user/guest/class_Guest.php +++ b/inc/classes/main/user/guest/class_Guest.php @@ -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; diff --git a/inc/classes/main/user/member/class_Member.php b/inc/classes/main/user/member/class_Member.php index a0bec178..1b939c20 100644 --- a/inc/classes/main/user/member/class_Member.php +++ b/inc/classes/main/user/member/class_Member.php @@ -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);