3 * A special guest login class for Ship-Simu
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 ShipSimuGuestLogin extends BaseFrameworkSystem implements LoginableUser {
28 private $hashedPassword = "";
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
40 $this->removeNumberFormaters();
41 $this->removeSystemArray();
45 * Creates an instance of this login class
47 * @return $loginInstance An instance of this login class
49 public final static function createShipSimuGuestLogin () {
51 $loginInstance = new ShipSimuGuestLogin();
53 // Return the instance
54 return $loginInstance;
58 * Logins the user with the given request containing the credential. The
59 * result of the login can be thrown by exception or, if prefered stored
60 * in a boolean attribute which is then readable by a matching getter.
62 * @param $requestInstance An instance of a Requestable class
63 * @param $responseInstance An instance of a Responseable class
65 * @throws UserAuthMethodException If wether username nor email login
67 * @throws MissingMethodException If a method was not found in the
69 * @throws UserPasswordMismatchException If the supplied password did not
70 * match with the stored password
72 public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
73 // By default no method is selected
77 // Detect login method (username or email) and try to get a userinstance
78 if (!is_null($requestInstance->getRequestElement('user'))) {
80 $method = 'createGuestByUsername';
81 $data = $requestInstance->getRequestElement('user');
84 // Is a method detected?
85 if (is_null($method)) {
87 throw new UserAuthMethodException($this, self::EXCEPTION_MISSING_METHOD);
88 } elseif (!method_exists($this->getConfigInstance()->readConfig('guest_class'), $method)) {
89 // The method is invalid!
90 throw new MissingMethodException(array($this, $method), self::EXCEPTION_MISSING_METHOD);
93 // Get a instance of the registry
94 $userInstance = Registry::getRegistry()->getInstance('user');
96 // Is there an instance?
97 if (is_null($userInstance)) {
98 // Get a user instance
99 $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('guest_class'), $method), array($data));
101 // Remember this new instance in registry
102 Registry::getRegistry()->addInstance($userInstance);
105 // Is the password correct?
106 if ($userInstance->ifPasswordHashMatches($requestInstance) === false) {
107 // Mismatching password
108 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
111 // Now do the real login. This can be cookie- or session-based login
112 // which depends on the admins setting then on the user's taste.
113 // 1) Get a login helper instance
114 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
116 // 2) Execute the login. This will now login...
117 $helperInstance->executeLogin($responseInstance);
121 * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
123 * @return $loginDone Wether the login was fine or not
125 public function ifLoginWasSuccessfull () {
126 // Is the registry key there?
127 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
134 * Encrypt given request key or throw an exception if key was not found in
137 * @param $requestKey Key in request class
140 public function encryptPassword ($requestKey) {
141 // Check if password is found in request
142 if ($this->getRequestInstance()->isRequestElementSet($requestKey)) {
143 // So encrypt the password and store it for later usage in
146 // Get the plain password
147 $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey);
150 $userInstance = Registry::getRegistry()->getInstance('user');
152 // Get a crypto helper and hash the password
153 $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
155 // Store the hash back in request
156 $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);