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