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