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