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