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