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