3 * A special login class for Ship-Simu
5 * @author Roland Haeder <webmaster@shipsimu.org>
7 * @copyright Copyright (c) 2007, 2008 Roland Haeder, 2009 Ship-Simu Developer Team
8 * @license GNU GPL 3.0 or any newer version
9 * @link http://www.shipsimu.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 ShipSimuUserLogin extends BaseFrameworkSystem implements LoginableUser, Registerable {
28 private $hashedPassword = '';
31 * Protected constructor
35 protected function __construct () {
36 // Call parent constructor
37 parent::__construct(__CLASS__);
41 * Creates an instance of this login class
43 * @return $loginInstance An instance of this login class
45 public static final function createShipSimuUserLogin () {
47 $loginInstance = new ShipSimuUserLogin();
49 // Return the instance
50 return $loginInstance;
54 * Logins the user with the given request containing the credential. The
55 * result of the login can be thrown by exception or, if prefered stored
56 * in a boolean attribute which is then readable by a matching getter.
58 * @param $requestInstance An instance of a Requestable class
59 * @param $responseInstance An instance of a Responseable class
61 * @throws UserPasswordMismatchException If the supplied password did not
62 * match with the stored password
63 * @todo We need to add something here which will make more than one
64 * @todo guest logins, users who are online but based on the same
67 public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
68 // Get a user instance
69 $userInstance = LoginFactory::createLoginObjectByRequest($requestInstance);
71 // Remember this new instance in registry
72 Registry::getRegistry()->addInstance('user', $userInstance);
74 // Is the password correct?
75 if ($userInstance->ifPasswordHashMatches($requestInstance) === false) {
76 // Mismatching password
77 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
82 // Now do the real login. This can be cookie- or session-based login
83 // which depends on the admins setting then on the user's taste.
84 // 1) Get a login helper instance
85 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
87 // 2) Execute the login. This will now login...
88 $helperInstance->executeLogin($responseInstance);
92 * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
94 * @return $loginDone Wether the login was fine or not
96 public function ifLoginWasSuccessfull () {
97 // Is the registry key there?
98 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
105 * Encrypt given request key or throw an exception if key was not found in
108 * @param $requestKey Key in request class
111 public function encryptPassword ($requestKey) {
112 // Check if password is found in request
113 if ($this->getRequestInstance()->isRequestElementSet($requestKey)) {
114 // So encrypt the password and store it for later usage in
117 // Get the plain password
118 $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey);
121 $userInstance = Registry::getRegistry()->getInstance('user');
123 // Get a crypto helper and hash the password
124 $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
126 // Store the hash back in request
127 $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);