3 namespace CoreFramework\Filter\User\Auth;
5 // Import framework stuff
6 use CoreFramework\Factory\ObjectFactory;
7 use CoreFramework\Loader\NoClassException;
8 use CoreFramework\Registry\Registry;
9 use CoreFramework\Request\Requestable;
10 use CoreFramework\Response\Responseable;
13 * A filter for checking user permissions
15 * @author Roland Haeder <webmaster@shipsimu.org>
17 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2017 Core Developer Team
18 * @license GNU GPL 3.0 or any newer version
19 * @link http://www.shipsimu.org
21 * This program is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34 class UserAuthFilter extends BaseFilter implements Filterable {
35 // Exception constants
36 const EXCEPTION_AUTH_DATA_INVALID = 0x1b0;
39 * The login method we shall choose
41 private $authMethod = '';
44 * Protected constructor
48 protected function __construct () {
49 // Call parent constructor
50 parent::__construct(__CLASS__);
54 * Creates an instance of this filter class
56 * @return $filterInstance An instance of this filter class
58 public static final function createUserAuthFilter () {
60 $filterInstance = new UserAuthFilter();
62 // Set default auth method
63 $filterInstance->setDefaultAuthMethod();
65 // Return the instance
66 return $filterInstance;
70 * Setter for default login method from config
74 protected function setDefaultAuthMethod () {
75 $this->authMethod = $this->getConfigInstance()->getConfigEntry('auth_method_class');
79 * Executes the filter with given request and response objects
81 * @param $requestInstance An instance of a class with an Requestable interface
82 * @param $responseInstance An instance of a class with an Responseable interface
84 * @throws UserAuthorizationException If the auth login was not found or if it was invalid
85 * @throws UserPasswordMismatchException If the supplied password hash does not match
86 * @throws NoClassException If the user (guest/member) class was not found
88 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
89 // Then get an auth instance for checking and updating the auth cookies
90 $authInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
92 // Set request instance
93 $authInstance->setRequestInstance($requestInstance);
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 Registry::getRegistry()->addInstance('auth', $authInstance);
142 Registry::getRegistry()->addInstance('user', $userInstance);