]> git.mxchange.org Git - city.git/blob - application/city/classes/login/class_CityUserLogin.php
Continued:
[city.git] / application / city / classes / login / class_CityUserLogin.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Login\User;
4
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;
15
16 /**
17  * A special login class for City
18  *
19  * @author              Roland Haeder <webmaster@shipsimu.org>
20  * @version             0.0.0
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
24  *
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.
29  *
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.
34  *
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/>.
37  */
38 class CityUserLogin extends BaseFrameworkSystem implements LoginableUser, Registerable {
39         /**
40          * The hashed password
41          */
42         private $hashedPassword = '';
43
44         /**
45          * Protected constructor
46          *
47          * @return      void
48          */
49         protected function __construct () {
50                 // Call parent constructor
51                 parent::__construct(__CLASS__);
52         }
53
54         /**
55          * Creates an instance of this login class
56          *
57          * @return      $loginInstance  An instance of this login class
58          */
59         public static final function createCityUserLogin () {
60                 // Get a new instance
61                 $loginInstance = new CityUserLogin();
62
63                 // Return the instance
64                 return $loginInstance;
65         }
66
67         /**
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.
71          *
72          * @param       $requestInstance        An instance of a Requestable class
73          * @param       $responseInstance       An instance of a Responseable class
74          * @return      void
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
79          * @todo        user account.
80          */
81         public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
82                 // Get a user instance from factory
83                 $userInstance = UserFactory::createUserByRequest($requestInstance);
84
85                 // Remember this new instance in registry
86                 GenericRegistry::getRegistry()->addInstance('user', $userInstance);
87
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);
92                 }
93
94                 // @TODO What else?
95
96                 /*
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.
99                  */
100
101                 // 1) Get a login helper instance
102                 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
103
104                 // 2) Execute the login. This will now login...
105                 $helperInstance->executeLogin($responseInstance);
106         }
107
108         /**
109          * Check if the implementation is correct. Only the request instance is
110          * needed as no redirect is done here.
111          *
112          * @param       $requestInstance        An instance of a Requestable class
113          * @return
114          */
115         public function testLogin (Requestable $requestInstance) {
116                 // Create a dummy instance
117                 $dummyInstance = Member::createMemberByRequest($requestInstance);
118         }
119
120         /**
121          * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
122          *
123          * @return      $loginDone      Wether the login was fine or not
124          */
125         public function ifLoginWasSuccessfull () {
126                 // Is the registry key there?
127                 $loginDone = (GenericRegistry::getRegistry()->getInstance('login') instanceof Registerable);
128
129                 // Return the result
130                 return $loginDone;
131         }
132
133         /**
134          * Encrypt given request key or throw an exception if key was not found in
135          * request
136          *
137          * @param       $requestKey             Key in request class
138          * @return      void
139          */
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
144                         // the request:
145
146                         // Get the plain password
147                         $plainPassword = FrameworkBootstrap::getRequestInstance()->getRequestElement($requestKey);
148
149                         // Get user instance
150                         $userInstance = GenericRegistry::getRegistry()->getInstance('user');
151
152                         // Get a crypto helper and hash the password
153                         $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
154
155                         // Store the hash back in request
156                         FrameworkBootstrap::getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);
157                 }
158         }
159 }