3 namespace Org\Mxchange\City\Login\Guest;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Auth\LoginableUser;
7 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Factory\User\UserFactory;
10 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
11 use Org\Mxchange\CoreFramework\Registry\GenericRegistry;
12 use Org\Mxchange\CoreFramework\Registry\Registerable;
13 use Org\Mxchange\CoreFramework\Request\Requestable;
14 use Org\Mxchange\CoreFramework\Response\Responseable;
17 * A special guest login class for City
19 * @author Roland Haeder <webmaster@shipsimu.org>
21 * @copyright Copyright (c) 2015 - 2023 City Developer Team
22 * @license GNU GPL 3.0 or any newer version
23 * @link http://www.shipsimu.org
25 * This program is free software: you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation, either version 3 of the License, or
28 * (at your option) any later version.
30 * This program is distributed in the hope that it will be useful,
31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
33 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public License
36 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 class CityGuestLogin extends BaseFrameworkSystem implements LoginableUser, Registerable {
42 private $hashedPassword = '';
45 * Protected constructor
49 protected function __construct () {
50 // Call parent constructor
51 parent::__construct(__CLASS__);
55 * Creates an instance of this login class
57 * @return $loginInstance An instance of this login class
59 public static final function createCityGuestLogin () {
61 $loginInstance = new CityGuestLogin();
63 // Return the instance
64 return $loginInstance;
68 * Logins the user with the given request containing the credential. The
69 * result of the login can be thrown by exception or, if prefered stored
70 * in a boolean attribute which is then readable by a matching getter.
72 * @param $requestInstance An instance of a Requestable class
73 * @param $responseInstance An instance of a Responseable class
75 * @throws UserPasswordMismatchException If the supplied password did not
76 * match with the stored password
78 public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
79 // Get a user instance
80 $userInstance = UserFactory::createUserByRequest($requestInstance);
82 // Remember this new instance in registry
83 GenericRegistry::getRegistry()->addInstance('user', $userInstance);
85 // Is the password correct?
86 if ($userInstance->ifPasswordHashMatches($requestInstance) === FALSE) {
87 // Mismatching password
88 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
91 // Now do the real login. This can be cookie- or session-based login
92 // which depends on the admins setting then on the user's taste.
93 // 1) Get a login helper instance
94 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
96 // 2) Execute the login. This will now login...
97 $helperInstance->executeLogin($responseInstance);
101 * Check if the implementation is correct. Only the request instance is
102 * needed as no redirect is done here.
104 * @param $requestInstance An instance of a Requestable class
107 public function testLogin (Requestable $requestInstance) {
108 // Create dummy instance
109 $dummyInstance = Guest::createGuestByRequest($requestInstance);
113 * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
115 * @return $loginDone Wether the login was fine or not
117 public function ifLoginWasSuccessfull () {
118 // Is the registry key there?
119 $loginDone = (GenericRegistry::getRegistry()->getInstance('login') instanceof Registerable);
126 * Encrypt given request key or throw an exception if key was not found in
129 * @param $requestKey Key in request class
132 public function encryptPassword (string $requestKey) {
133 // Check if password is found in request
134 if (FrameworkBootstrap::getRequestInstance()->isRequestElementSet($requestKey)) {
135 // So encrypt the password and store it for later usage in
138 // Get the plain password
139 $plainPassword = FrameworkBootstrap::getRequestInstance()->getRequestElement($requestKey);
142 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
144 // Get a crypto helper and hash the password
145 $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
147 // Store the hash back in request
148 FrameworkBootstrap::getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);