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