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