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