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