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