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