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