3 namespace Org\Mxchange\City\Login\User;
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 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 CityUserLogin 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 createCityUserLogin () {
61 $loginInstance = new CityUserLogin();
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
77 * @todo We need to add something here which will make more than one
78 * @todo guest logins, users who are online but based on the same
81 public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
82 // Get a user instance from factory
83 $userInstance = UserFactory::createUserByRequest($requestInstance);
85 // Remember this new instance in registry
86 GenericRegistry::getRegistry()->addInstance('user', $userInstance);
88 // Is the password correct?
89 if ($userInstance->ifPasswordHashMatches($requestInstance) === FALSE) {
90 // Mismatching password
91 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
97 * Now do the real login. This can be cookie- or session-based login
98 * which depends on the admins setting then on the user's taste.
101 // 1) Get a login helper instance
102 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
104 // 2) Execute the login. This will now login...
105 $helperInstance->executeLogin($responseInstance);
109 * Check if the implementation is correct. Only the request instance is
110 * needed as no redirect is done here.
112 * @param $requestInstance An instance of a Requestable class
115 public function testLogin (Requestable $requestInstance) {
116 // Create a dummy instance
117 $dummyInstance = Member::createMemberByRequest($requestInstance);
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 = (GenericRegistry::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 (FrameworkBootstrap::getRequestInstance()->isRequestElementSet($requestKey)) {
143 // So encrypt the password and store it for later usage in
146 // Get the plain password
147 $plainPassword = FrameworkBootstrap::getRequestInstance()->getRequestElement($requestKey);
150 $userInstance = GenericRegistry::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 FrameworkBootstrap::getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);