]> git.mxchange.org Git - core.git/blob - inc/classes/main/filter/auth/class_UserAuthFilter.php
c2b0ac4287164c49e3ac59dae1f57565b6483f85
[core.git] / inc / classes / main / filter / auth / class_UserAuthFilter.php
1 <?php
2 /**
3  * A filter for checking user permissions
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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          * @param       $controllerInstance             An instance of a Controller class
47          * @return      $filterInstance                 An instance of this filter class
48          */
49         public final static function createUserAuthFilter (Controller $controllerInstance) {
50                 // Get a new instance
51                 $filterInstance = new UserAuthFilter();
52
53                 // Set the controller
54                 $filterInstance->setControllerInstance($controllerInstance);
55
56                 // Set default auth method
57                 $filterInstance->setDefaultAuthMethod();
58
59                 // Return the instance
60                 return $filterInstance;
61         }
62
63         /**
64          * Setter for default login method from config
65          *
66          * @return      void
67          */
68         protected function setDefaultAuthMethod () {
69                 $this->authMethod = $this->getConfigInstance()->readConfig('auth_method_class');
70         }
71
72         /**
73          * Executes the filter with given request and response objects
74          *
75          * @param       $requestInstance        An instance of a class with an Requestable interface
76          * @param       $responseInstance       An instance of a class with an Responseable interface
77          * @return      void
78          * @throws      UserAuthorizationException      If the auth login was not found or if it was invalid
79          * @throws      UserPasswordMismatchException   If the supplied password hash does not match
80          * @throws      ClassNotFoundException  If the user (guest/member) class was not found
81          */
82         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
83                 // Then get an auth instance for checking and updating the auth cookies
84                 $authInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
85
86                 // Set request instance
87                 $authInstance->setRequestInstance($requestInstance);
88
89                 // Now, get the auth data for comparison
90                 $authLogin = $authInstance->getUserAuth();
91                 $authHash  = $authInstance->getPasswordAuth();
92
93                 // If one is empty stop here
94                 if ((empty($authLogin)) || (empty($authHash))) {
95                         // Destroy the auth data
96                         $authInstance->destroyAuthData();
97
98                         // Mark the request as invalid
99                         $requestInstance->requestIsValid(false);
100
101                         // Add fatal message
102                         $responseInstance->addFatalMessage('auth_data_incomplete');
103
104                         // Stop here
105                         throw new UserAuthorizationException($this, self::EXCEPTION_AUTH_DATA_INVALID);
106                 } // END - if
107
108                 // Regular user account
109                 $className = $this->getConfigInstance()->readConfig('user_class');
110                 $methodName = 'createMemberByUserName';
111
112                 // Now, try to get a user or guest instance
113                 if ($authLogin == $this->getConfigInstance()->readConfig('guest_login_user')) {
114                         // Set class
115                         $className = $this->getConfigInstance()->readConfig('guest_class');
116                         $methodName = 'createGuestByUserName';
117                 } // END - if
118
119                 // Does the guest class exist?
120                 if (!class_exists($className)) {
121                         // Then abort here
122                         throw new ClassNotFoundException (array($this, $className), self::EXCEPTION_CLASS_NOT_FOUND);
123                 } // END - if
124
125                 // Now try the dynamic login
126                 $userInstance = call_user_func_array(array($className, $methodName), array($authLogin));
127
128                 // Is the password correct?
129                 if ($userInstance->getPasswordHash() !== $authHash) {
130                         // Mismatching password
131                         throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
132                 } // END - if
133
134                 // Remember auth and user instances in registry
135                 Registry::getRegistry()->addInstance('auth', $authInstance);
136                 Registry::getRegistry()->addInstance('user', $userInstance);
137         }
138 }
139
140 // [EOF]
141 ?>