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\GenericRegistry;
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 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
21 * @license GNU GPL 3.0 or any newer version
22 * @link http://www.shipsimu.org
24 * This program is free software: you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation, either version 3 of the License, or
27 * (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program. If not, see <http://www.gnu.org/licenses/>.
37 class UserAuthFilter extends BaseFilter implements Filterable {
38 // Exception constants
39 const EXCEPTION_AUTH_DATA_INVALID = 0x1b0;
42 * The login method we shall choose
44 private $authMethod = '';
47 * Protected constructor
51 protected function __construct () {
52 // Call parent constructor
53 parent::__construct(__CLASS__);
57 * Creates an instance of this filter class
59 * @return $filterInstance An instance of this filter class
61 public static final function createUserAuthFilter () {
63 $filterInstance = new UserAuthFilter();
65 // Set default auth method
66 $filterInstance->setDefaultAuthMethod();
68 // Return the instance
69 return $filterInstance;
73 * Setter for default login method from config
77 protected function setDefaultAuthMethod () {
78 $this->authMethod = $this->getConfigInstance()->getConfigEntry('auth_method_class');
82 * Executes the filter with given request and response objects
84 * @param $requestInstance An instance of a class with an Requestable interface
85 * @param $responseInstance An instance of a class with an Responseable interface
87 * @throws UserAuthorizationException If the auth login was not found or if it was invalid
88 * @throws UserPasswordMismatchException If the supplied password hash does not match
89 * @throws NoClassException If the user (guest/member) class was not found
91 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
92 // Then get an auth instance for checking and updating the auth cookies
93 $authInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
95 // Now, get the auth data for comparison
96 $authLogin = $authInstance->getUserAuth();
97 $authHash = $authInstance->getPasswordAuth();
99 // If one is empty stop here
100 if ((empty($authLogin)) || (empty($authHash))) {
101 // Destroy the auth data
102 $authInstance->destroyAuthData();
104 // Mark the request as invalid
105 $requestInstance->requestIsValid(false);
108 $responseInstance->addFatalMessage('auth_data_incomplete');
111 throw new UserAuthorizationException($this, self::EXCEPTION_AUTH_DATA_INVALID);
114 // Regular user account
115 $className = $this->getConfigInstance()->getConfigEntry('user_class');
116 $methodName = 'createMemberByUserName';
118 // Now, try to get a user or guest instance
119 if ($authLogin == $this->getConfigInstance()->getConfigEntry('guest_login_user')) {
121 $className = $this->getConfigInstance()->getConfigEntry('guest_class');
122 $methodName = 'createGuestByUserName';
125 // Does the guest class exist?
126 if (!class_exists($className)) {
128 throw new NoClassException (array($this, $className), self::EXCEPTION_CLASS_NOT_FOUND);
131 // Now try the dynamic login
132 $userInstance = call_user_func_array(array($className, $methodName), array($authLogin));
134 // Is the password correct?
135 if ($userInstance->getPasswordHash() !== $authHash) {
136 // Mismatching password
137 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
140 // Remember auth and user instances in registry
141 GenericRegistry::getRegistry()->addInstance('auth', $authInstance);
142 GenericRegistry::getRegistry()->addInstance('user', $userInstance);