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__);
39 // Set part description
40 $this->setObjectDescription("Guest login for Ship-Simu");
42 // Create unique ID number
43 $this->generateUniqueId();
46 $this->removeNumberFormaters();
47 $this->removeSystemArray();
51 * Creates an instance of this login class
53 * @return $loginInstance An instance of this login class
55 public final static function createShipSimuGuestLogin () {
57 $loginInstance = new ShipSimuGuestLogin();
59 // Return the instance
60 return $loginInstance;
64 * Logins the user with the given request containing the credential. The
65 * result of the login can be thrown by exception or, if prefered stored
66 * in a boolean attribute which is then readable by a matching getter.
68 * @param $requestInstance An instance of a Requestable class
69 * @param $responseInstance An instance of a Responseable class
71 * @throws UserAuthMethodException If wether username nor email login
73 * @throws MissingMethodException If a method was not found in the
75 * @throws UserPasswordMismatchException If the supplied password did not
76 * match with the stored password
78 public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
79 // By default no method is selected
83 // Detect login method (username or email) and try to get a userinstance
84 if (!is_null($requestInstance->getRequestElement('username'))) {
86 $method = "createGuestByUsername";
87 $data = $requestInstance->getRequestElement('username');
88 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
90 $method = "createGuestByEmail";
91 $data = $requestInstance->getRequestElement('email');
94 // Is a method detected?
95 if (is_null($method)) {
97 throw new UserAuthMethodException($this, self::EXCEPTION_MISSING_METHOD);
98 } elseif (!method_exists($this->getConfigInstance()->readConfig('guest_class'), $method)) {
99 // The method is invalid!
100 throw new MissingMethodException(array($this, $method), self::EXCEPTION_MISSING_METHOD);
103 // Get a instance of the registry
104 $userInstance = Registry::getRegistry()->getInstance('user');
106 // Is there an instance?
107 if (is_null($userInstance)) {
108 // Get a user instance
109 $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('guest_class'), $method), array($data));
111 // Remember this new instance in registry
112 Registry::getRegistry()->addInstance($userInstance);
115 // Is the password correct?
116 if (!$userInstance->ifPasswordHashMatches($requestInstance)) {
117 // Mismatching password
118 throw new UserPasswordMismatchException(array($this, $userInstance), User::EXCEPTION_USER_PASS_MISMATCH);
121 // Now do the real login. This can be cookie- or session-based login
122 // which depends on the admins setting then on the user's taste.
123 // 1) Get a login helper instance
124 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
126 // 2) Execute the login. This will now login...
127 $helperInstance->executeLogin($responseInstance);
131 * Determines wether the login was fine. This is done by checking if the 'login' instance is in registry
133 * @return $loginDone Wether the login was fine or not
135 public function ifLoginWasSuccessfull () {
136 // Is the registry key there?
137 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
144 * Encrypt the given request key or throw an exception if the key was not
145 * found in the request
147 * @param $requestKey Key in request class
150 public function encryptPassword ($requestKey) {
151 // Check if the password is found in the request
152 if ($this->getRequestInstance()->isRequestElementSet($requestKey)) {
153 // So encrypt the password and store it for later usage in
156 // Get the plain password
157 $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey);
160 $userInstance = Registry::getRegistry()->getInstance('user');
162 // Get a crypto helper and hash the password
163 $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashPassword($plainPassword, $userInstance->getPasswordHash());
165 // Store the hash back in the request
166 $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);