3 namespace Org\Mxchange\City\Login\Guest;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Request\Requestable;
7 use Org\Mxchange\CoreFramework\Response\Responseable;
10 * A special guest login class for City
12 * @author Roland Haeder <webmaster@shipsimu.org>
14 * @copyright Copyright (c) 2015, 2016 City Developer Team
15 * @license GNU GPL 3.0 or any newer version
16 * @link http://www.shipsimu.org
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
31 class CityGuestLogin extends BaseFrameworkSystem implements LoginableUser, Registerable {
35 private $hashedPassword = '';
38 * Protected constructor
42 protected function __construct () {
43 // Call parent constructor
44 parent::__construct(__CLASS__);
48 * Creates an instance of this login class
50 * @return $loginInstance An instance of this login class
52 public static final function createCityGuestLogin () {
54 $loginInstance = new CityGuestLogin();
56 // Return the instance
57 return $loginInstance;
61 * Logins the user with the given request containing the credential. The
62 * result of the login can be thrown by exception or, if prefered stored
63 * in a boolean attribute which is then readable by a matching getter.
65 * @param $requestInstance An instance of a Requestable class
66 * @param $responseInstance An instance of a Responseable class
68 * @throws UserPasswordMismatchException If the supplied password did not
69 * match with the stored password
71 public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
72 // Get a user instance
73 $userInstance = UserFactory::createUserByRequest($requestInstance);
75 // Remember this new instance in registry
76 Registry::getRegistry()->addInstance('user', $userInstance);
78 // Is the password correct?
79 if ($userInstance->ifPasswordHashMatches($requestInstance) === FALSE) {
80 // Mismatching password
81 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
84 // Now do the real login. This can be cookie- or session-based login
85 // which depends on the admins setting then on the user's taste.
86 // 1) Get a login helper instance
87 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
89 // 2) Execute the login. This will now login...
90 $helperInstance->executeLogin($responseInstance);
94 * Check if the implementation is correct. Only the request instance is
95 * needed as no redirect is done here.
97 * @param $requestInstance An instance of a Requestable class
100 public function testLogin (Requestable $requestInstance) {
101 // Create dummy instance
102 $dummyInstance = Guest::createGuestByRequest($requestInstance);
106 * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
108 * @return $loginDone Wether the login was fine or not
110 public function ifLoginWasSuccessfull () {
111 // Is the registry key there?
112 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
119 * Encrypt given request key or throw an exception if key was not found in
122 * @param $requestKey Key in request class
125 public function encryptPassword ($requestKey) {
126 // Check if password is found in request
127 if ($this->getRequestInstance()->isRequestElementSet($requestKey)) {
128 // So encrypt the password and store it for later usage in
131 // Get the plain password
132 $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey);
135 $userInstance = Registry::getRegistry()->getInstance('user');
137 // Get a crypto helper and hash the password
138 $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
140 // Store the hash back in request
141 $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);