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