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