3 namespace Org\Mxchange\CoreFramework\Filter\User\Auth;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
8 use Org\Mxchange\CoreFramework\Filter\Filterable;
9 use Org\Mxchange\CoreFramework\Loader\NoClassException;
10 use Org\Mxchange\CoreFramework\Registry\Registry;
11 use Org\Mxchange\CoreFramework\Request\Requestable;
12 use Org\Mxchange\CoreFramework\Response\Responseable;
13 use Org\Mxchange\CoreFramework\User\BaseUser;
16 * A filter for checking user permissions
18 * @author Roland Haeder <webmaster@shipsimu.org>
20 <<<<<<< HEAD:framework/main/classes/filter/auth/class_UserAuthFilter.php
21 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
23 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2016 Core Developer Team
24 >>>>>>> Some updates::inc/main/classes/filter/auth/class_UserAuthFilter.php
25 * @license GNU GPL 3.0 or any newer version
26 * @link http://www.shipsimu.org
28 * This program is free software: you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License as published by
30 * the Free Software Foundation, either version 3 of the License, or
31 * (at your option) any later version.
33 * This program is distributed in the hope that it will be useful,
34 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
36 * GNU General Public License for more details.
38 * You should have received a copy of the GNU General Public License
39 * along with this program. If not, see <http://www.gnu.org/licenses/>.
41 class UserAuthFilter extends BaseFilter implements Filterable {
42 // Exception constants
43 const EXCEPTION_AUTH_DATA_INVALID = 0x1b0;
46 * The login method we shall choose
48 private $authMethod = '';
51 * Protected constructor
55 protected function __construct () {
56 // Call parent constructor
57 parent::__construct(__CLASS__);
61 * Creates an instance of this filter class
63 * @return $filterInstance An instance of this filter class
65 public static final function createUserAuthFilter () {
67 $filterInstance = new UserAuthFilter();
69 // Set default auth method
70 $filterInstance->setDefaultAuthMethod();
72 // Return the instance
73 return $filterInstance;
77 * Setter for default login method from config
81 protected function setDefaultAuthMethod () {
82 $this->authMethod = $this->getConfigInstance()->getConfigEntry('auth_method_class');
86 * Executes the filter with given request and response objects
88 * @param $requestInstance An instance of a class with an Requestable interface
89 * @param $responseInstance An instance of a class with an Responseable interface
91 * @throws UserAuthorizationException If the auth login was not found or if it was invalid
92 * @throws UserPasswordMismatchException If the supplied password hash does not match
93 * @throws NoClassException If the user (guest/member) class was not found
95 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
96 // Then get an auth instance for checking and updating the auth cookies
97 $authInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
99 // Now, get the auth data for comparison
100 $authLogin = $authInstance->getUserAuth();
101 $authHash = $authInstance->getPasswordAuth();
103 // If one is empty stop here
104 if ((empty($authLogin)) || (empty($authHash))) {
105 // Destroy the auth data
106 $authInstance->destroyAuthData();
108 // Mark the request as invalid
109 $requestInstance->requestIsValid(false);
112 $responseInstance->addFatalMessage('auth_data_incomplete');
115 throw new UserAuthorizationException($this, self::EXCEPTION_AUTH_DATA_INVALID);
118 // Regular user account
119 $className = $this->getConfigInstance()->getConfigEntry('user_class');
120 $methodName = 'createMemberByUserName';
122 // Now, try to get a user or guest instance
123 if ($authLogin == $this->getConfigInstance()->getConfigEntry('guest_login_user')) {
125 $className = $this->getConfigInstance()->getConfigEntry('guest_class');
126 $methodName = 'createGuestByUserName';
129 // Does the guest class exist?
130 if (!class_exists($className)) {
132 throw new NoClassException (array($this, $className), self::EXCEPTION_CLASS_NOT_FOUND);
135 // Now try the dynamic login
136 $userInstance = call_user_func_array(array($className, $methodName), array($authLogin));
138 // Is the password correct?
139 if ($userInstance->getPasswordHash() !== $authHash) {
140 // Mismatching password
141 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
144 // Remember auth and user instances in registry
145 Registry::getRegistry()->addInstance('auth', $authInstance);
146 Registry::getRegistry()->addInstance('user', $userInstance);