3 namespace CoreFramework\Filter\User\Auth;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Registry\Generic\Registry;
8 use CoreFramework\Request\Requestable;
11 * A filter for checking user permissions
13 * @author Roland Haeder <webmaster@shipsimu.org>
15 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
16 * @license GNU GPL 3.0 or any newer version
17 * @link http://www.shipsimu.org
19 * This program is free software: you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation, either version 3 of the License, or
22 * (at your option) any later version.
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 class UserAuthFilter extends BaseFilter implements Filterable {
33 // Exception constants
34 const EXCEPTION_AUTH_DATA_INVALID = 0x1b0;
37 * The login method we shall choose
39 private $authMethod = '';
42 * Protected constructor
46 protected function __construct () {
47 // Call parent constructor
48 parent::__construct(__CLASS__);
52 * Creates an instance of this filter class
54 * @return $filterInstance An instance of this filter class
56 public static final function createUserAuthFilter () {
58 $filterInstance = new UserAuthFilter();
60 // Set default auth method
61 $filterInstance->setDefaultAuthMethod();
63 // Return the instance
64 return $filterInstance;
68 * Setter for default login method from config
72 protected function setDefaultAuthMethod () {
73 $this->authMethod = $this->getConfigInstance()->getConfigEntry('auth_method_class');
77 * Executes the filter with given request and response objects
79 * @param $requestInstance An instance of a class with an Requestable interface
80 * @param $responseInstance An instance of a class with an Responseable interface
82 * @throws UserAuthorizationException If the auth login was not found or if it was invalid
83 * @throws UserPasswordMismatchException If the supplied password hash does not match
84 * @throws NoClassException If the user (guest/member) class was not found
86 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
87 // Then get an auth instance for checking and updating the auth cookies
88 $authInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
90 // Set request instance
91 $authInstance->setRequestInstance($requestInstance);
93 // Now, get the auth data for comparison
94 $authLogin = $authInstance->getUserAuth();
95 $authHash = $authInstance->getPasswordAuth();
97 // If one is empty stop here
98 if ((empty($authLogin)) || (empty($authHash))) {
99 // Destroy the auth data
100 $authInstance->destroyAuthData();
102 // Mark the request as invalid
103 $requestInstance->requestIsValid(FALSE);
106 $responseInstance->addFatalMessage('auth_data_incomplete');
109 throw new UserAuthorizationException($this, self::EXCEPTION_AUTH_DATA_INVALID);
112 // Regular user account
113 $className = $this->getConfigInstance()->getConfigEntry('user_class');
114 $methodName = 'createMemberByUserName';
116 // Now, try to get a user or guest instance
117 if ($authLogin == $this->getConfigInstance()->getConfigEntry('guest_login_user')) {
119 $className = $this->getConfigInstance()->getConfigEntry('guest_class');
120 $methodName = 'createGuestByUserName';
123 // Does the guest class exist?
124 if (!class_exists($className)) {
126 throw new NoClassException (array($this, $className), self::EXCEPTION_CLASS_NOT_FOUND);
129 // Now try the dynamic login
130 $userInstance = call_user_func_array(array($className, $methodName), array($authLogin));
132 // Is the password correct?
133 if ($userInstance->getPasswordHash() !== $authHash) {
134 // Mismatching password
135 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
138 // Remember auth and user instances in registry
139 Registry::getRegistry()->addInstance('auth', $authInstance);
140 Registry::getRegistry()->addInstance('user', $userInstance);