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