3 * A filter for checking user permissions
5 * @author Roland Haeder <webmaster@ship-simu.org>
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
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.
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.
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/>.
24 class UserAuthFilter extends BaseFilter implements Filterable {
25 // Exception constants
26 const EXCEPTION_AUTH_DATA_INVALID = 0x1b0;
29 * The login method we shall choose
31 private $authMethod = "";
34 * Protected constructor
38 protected function __construct () {
39 // Call parent constructor
40 parent::__construct(__CLASS__);
42 // Set part description
43 $this->setObjectDescription("A user authorization filter");
45 // Create unique ID number
46 $this->generateUniqueId();
50 * Creates an instance of this filter class
52 * @return $filterInstance An instance of this filter class
54 public final static function createUserAuthFilter () {
56 $filterInstance = new UserAuthFilter();
58 // Set default auth method
59 $filterInstance->setDefaultAuthMethod();
61 // Return the instance
62 return $filterInstance;
66 * Setter for default login method from config
70 protected function setDefaultAuthMethod () {
71 $this->authMethod = $this->getConfigInstance()->readConfig('auth_method_class');
75 * Executes the filter with given request and response objects
77 * @param $requestInstance An instance of a class with an Requestable interface
78 * @param $responseInstance An instance of a class with an Responseable interface
80 * @throws UserAuthorizationException If the auth login was not found or if it was invalid
81 * @throws UserPasswordMismatchException If the supplied password hash does not match
83 public function execute (Requestable $requestInstance, Responseable $responseInstance) {
84 // Then get an auth instance for checking and updating the auth cookies
85 $authInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
87 // Set request instance
88 $authInstance->setRequestInstance($requestInstance);
90 // Now, get the auth data for comparison
91 $authLogin = $authInstance->getUserAuth();
92 $authHash = $authInstance->getPasswordAuth();
94 // If one is empty stop here
95 if ((empty($authLogin)) || (empty($authHash))) {
96 // Destroy the auth data
97 $authInstance->destroyAuthData();
99 // Mark the request as invalid
100 $requestInstance->requestIsValid(false);
103 $responseInstance->addFatalMessage('auth_data_incomplete');
106 throw new UserAuthorizationException($this, self::EXCEPTION_AUTH_DATA_INVALID);
109 // Now, try to get a user or guest instance
110 if ($authLogin == $this->getConfigInstance()->readConfig('guest_login_user')) {
112 $userInstance = Guest::createGuestByUserName($authLogin);
114 // Regular user account
115 $userInstance = User::createUserByUserName($authLogin);
118 // Is the password correct?
119 if ($userInstance->getPasswordHash() !== $authHash) {
120 // Mismatching password
121 throw new UserPasswordMismatchException(array($this, $userInstance), User::EXCEPTION_USER_PASS_MISMATCH);
124 // Remember auth and user instances in registry
125 Registry::getRegistry()->addInstance('auth', $authInstance);
126 Registry::getRegistry()->addInstance('user', $userInstance);