]> git.mxchange.org Git - core.git/blob - framework/main/classes/filter/auth/class_UserAuthFilter.php
Continued:
[core.git] / framework / main / classes / filter / auth / class_UserAuthFilter.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Filter\User\Auth;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Filter\BaseFilter;
9 use Org\Mxchange\CoreFramework\Filter\Filterable;
10 use Org\Mxchange\CoreFramework\Generic\FrameworkInterface;
11 use Org\Mxchange\CoreFramework\Loader\NoClassException;
12 use Org\Mxchange\CoreFramework\Registry\Object\ObjectRegistry;
13 use Org\Mxchange\CoreFramework\Request\Requestable;
14 use Org\Mxchange\CoreFramework\Response\Responseable;
15 use Org\Mxchange\CoreFramework\User\BaseUser;
16
17 /**
18  * A filter for checking user permissions
19  *
20  * @author              Roland Haeder <webmaster@shipsimu.org>
21  * @version             0.0.0
22  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
23  * @license             GNU GPL 3.0 or any newer version
24  * @link                http://www.shipsimu.org
25  *
26  * This program is free software: you can redistribute it and/or modify
27  * it under the terms of the GNU General Public License as published by
28  * the Free Software Foundation, either version 3 of the License, or
29  * (at your option) any later version.
30  *
31  * This program is distributed in the hope that it will be useful,
32  * but WITHOUT ANY WARRANTY; without even the implied warranty of
33  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34  * GNU General Public License for more details.
35  *
36  * You should have received a copy of the GNU General Public License
37  * along with this program. If not, see <http://www.gnu.org/licenses/>.
38  */
39 class UserAuthFilter extends BaseFilter implements Filterable {
40         // Exception constants
41         const EXCEPTION_AUTH_DATA_INVALID = 0x1b0;
42
43         /**
44          * The login method we shall choose
45          */
46         private $authMethod = '';
47
48         /**
49          * Protected constructor
50          *
51          * @return      void
52          */
53         private function __construct () {
54                 // Call parent constructor
55                 parent::__construct(__CLASS__);
56         }
57
58         /**
59          * Creates an instance of this filter class
60          *
61          * @return      $filterInstance                 An instance of this filter class
62          */
63         public static final function createUserAuthFilter () {
64                 // Get a new instance
65                 $filterInstance = new UserAuthFilter();
66
67                 // Set default auth method
68                 $filterInstance->setDefaultAuthMethod();
69
70                 // Return the instance
71                 return $filterInstance;
72         }
73
74         /**
75          * Setter for default login method from config
76          *
77          * @return      void
78          */
79         protected function setDefaultAuthMethod () {
80                 $this->authMethod = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('auth_method_class');
81         }
82
83         /**
84          * Executes the filter with given request and response objects
85          *
86          * @param       $requestInstance        An instance of a class with an Requestable interface
87          * @param       $responseInstance       An instance of a class with an Responseable interface
88          * @return      void
89          * @throws      UserAuthorizationException      If the auth login was not found or if it was invalid
90          * @throws      UserPasswordMismatchException   If the supplied password hash does not match
91          * @throws      NoClassException        If the user (guest/member) class was not found
92          */
93         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
94                 // Then get an auth instance for checking and updating the auth cookies
95                 $authInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
96
97                 // Now, get the auth data for comparison
98                 $authLogin = $authInstance->getUserAuth();
99                 $authHash  = $authInstance->getPasswordAuth();
100
101                 // If one is empty stop here
102                 if ((empty($authLogin)) || (empty($authHash))) {
103                         // Destroy the auth data
104                         $authInstance->destroyAuthData();
105
106                         // Mark the request as invalid
107                         $requestInstance->setIsRequestValid(FALSE);
108
109                         // Add fatal message
110                         $responseInstance->addFatalMessage('auth_data_incomplete');
111
112                         // Stop here
113                         throw new UserAuthorizationException($this, self::EXCEPTION_AUTH_DATA_INVALID);
114                 }
115
116                 // Regular user account
117                 $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('user_class');
118                 $methodName = 'createMemberByUserName';
119
120                 // Now, try to get a user or guest instance
121                 if ($authLogin == FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('guest_login_user')) {
122                         // Set class
123                         $className = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('guest_class');
124                         $methodName = 'createGuestByUserName';
125                 }
126
127                 // Does the guest class exist?
128                 if (!class_exists($className)) {
129                         // Then abort here
130                         throw new NoClassException ([$this, $className], FrameworkInterface::EXCEPTION_CLASS_NOT_FOUND);
131                 }
132
133                 // Now try the dynamic login
134                 $userInstance = call_user_func_array([$className, $methodName], [$authLogin]);
135
136                 // Is the password correct?
137                 if ($userInstance->getPasswordHash() !== $authHash) {
138                         // Mismatching password
139                         throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
140                 }
141
142                 // Remember auth and user instances in registry
143                 ObjectRegistry::getRegistry()->addInstance('auth', $authInstance);
144                 ObjectRegistry::getRegistry()->addInstance('user', $userInstance);
145         }
146
147 }