3 namespace Org\Mxchange\City\Login\User;
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
8 use Org\Mxchange\CoreFramework\Object\BaseFrameworkSystem;
9 use Org\Mxchange\CoreFramework\Registry\Registerable;
10 use Org\Mxchange\CoreFramework\Registry\Registry;
11 use Org\Mxchange\CoreFramework\Request\Requestable;
12 use Org\Mxchange\CoreFramework\Response\Responseable;
15 * A special login class for City
17 * @author Roland Haeder <webmaster@shipsimu.org>
19 * @copyright Copyright (c) 2015, 2016 City Developer Team
20 * @license GNU GPL 3.0 or any newer version
21 * @link http://www.shipsimu.org
23 * This program is free software: you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation, either version 3 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program. If not, see <http://www.gnu.org/licenses/>.
36 class CityUserLogin extends BaseFrameworkSystem implements LoginableUser, Registerable {
40 private $hashedPassword = '';
43 * Protected constructor
47 protected function __construct () {
48 // Call parent constructor
49 parent::__construct(__CLASS__);
53 * Creates an instance of this login class
55 * @return $loginInstance An instance of this login class
57 public static final function createCityUserLogin () {
59 $loginInstance = new CityUserLogin();
61 // Return the instance
62 return $loginInstance;
66 * Logins the user with the given request containing the credential. The
67 * result of the login can be thrown by exception or, if prefered stored
68 * in a boolean attribute which is then readable by a matching getter.
70 * @param $requestInstance An instance of a Requestable class
71 * @param $responseInstance An instance of a Responseable class
73 * @throws UserPasswordMismatchException If the supplied password did not
74 * match with the stored password
75 * @todo We need to add something here which will make more than one
76 * @todo guest logins, users who are online but based on the same
79 public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
80 // Get a user instance from factory
81 $userInstance = UserFactory::createUserByRequest($requestInstance);
83 // Remember this new instance in registry
84 Registry::getRegistry()->addInstance('user', $userInstance);
86 // Is the password correct?
87 if ($userInstance->ifPasswordHashMatches($requestInstance) === FALSE) {
88 // Mismatching password
89 throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
95 * Now do the real login. This can be cookie- or session-based login
96 * which depends on the admins setting then on the user's taste.
99 // 1) Get a login helper instance
100 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
102 // 2) Execute the login. This will now login...
103 $helperInstance->executeLogin($responseInstance);
107 * Check if the implementation is correct. Only the request instance is
108 * needed as no redirect is done here.
110 * @param $requestInstance An instance of a Requestable class
113 public function testLogin (Requestable $requestInstance) {
114 // Create a dummy instance
115 $dummyInstance = Member::createMemberByRequest($requestInstance);
119 * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
121 * @return $loginDone Wether the login was fine or not
123 public function ifLoginWasSuccessfull () {
124 // Is the registry key there?
125 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
132 * Encrypt given request key or throw an exception if key was not found in
135 * @param $requestKey Key in request class
138 public function encryptPassword ($requestKey) {
139 // Check if password is found in request
140 if (FrameworkBootstrap::getRequestInstance()->isRequestElementSet($requestKey)) {
141 // So encrypt the password and store it for later usage in
144 // Get the plain password
145 $plainPassword = FrameworkBootstrap::getRequestInstance()->getRequestElement($requestKey);
148 $userInstance = Registry::getRegistry()->getInstance('user');
150 // Get a crypto helper and hash the password
151 $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
153 // Store the hash back in request
154 FrameworkBootstrap::getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);