]> git.mxchange.org Git - city.git/blob - application/city/classes/login/class_CityUserLogin.php
Next wave:
[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\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;
11
12 /**
13  * A special login class for City
14  *
15  * @author              Roland Haeder <webmaster@shipsimu.org>
16  * @version             0.0.0
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
20  *
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.
25  *
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.
30  *
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/>.
33  */
34 class CityUserLogin extends BaseFrameworkSystem implements LoginableUser, Registerable {
35         /**
36          * The hashed password
37          */
38         private $hashedPassword = '';
39
40         /**
41          * Protected constructor
42          *
43          * @return      void
44          */
45         protected function __construct () {
46                 // Call parent constructor
47                 parent::__construct(__CLASS__);
48         }
49
50         /**
51          * Creates an instance of this login class
52          *
53          * @return      $loginInstance  An instance of this login class
54          */
55         public static final function createCityUserLogin () {
56                 // Get a new instance
57                 $loginInstance = new CityUserLogin();
58
59                 // Return the instance
60                 return $loginInstance;
61         }
62
63         /**
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.
67          *
68          * @param       $requestInstance        An instance of a Requestable class
69          * @param       $responseInstance       An instance of a Responseable class
70          * @return      void
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
75          * @todo        user account.
76          */
77         public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
78                 // Get a user instance from factory
79                 $userInstance = UserFactory::createUserByRequest($requestInstance);
80
81                 // Remember this new instance in registry
82                 Registry::getRegistry()->addInstance('user', $userInstance);
83
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);
88                 } // END - if
89
90                 // @TODO What else?
91
92                 /*
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.
95                  */
96
97                 // 1) Get a login helper instance
98                 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
99
100                 // 2) Execute the login. This will now login...
101                 $helperInstance->executeLogin($responseInstance);
102         }
103
104         /**
105          * Check if the implementation is correct. Only the request instance is
106          * needed as no redirect is done here.
107          *
108          * @param       $requestInstance        An instance of a Requestable class
109          * @return
110          */
111         public function testLogin (Requestable $requestInstance) {
112                 // Create a dummy instance
113                 $dummyInstance = Member::createMemberByRequest($requestInstance);
114         }
115
116         /**
117          * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
118          *
119          * @return      $loginDone      Wether the login was fine or not
120          */
121         public function ifLoginWasSuccessfull () {
122                 // Is the registry key there?
123                 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
124
125                 // Return the result
126                 return $loginDone;
127         }
128
129         /**
130          * Encrypt given request key or throw an exception if key was not found in
131          * request
132          *
133          * @param       $requestKey             Key in request class
134          * @return      void
135          */
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
140                         // the request:
141
142                         // Get the plain password
143                         $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey);
144
145                         // Get user instance
146                         $userInstance = Registry::getRegistry()->getInstance('user');
147
148                         // Get a crypto helper and hash the password
149                         $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
150
151                         // Store the hash back in request
152                         $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);
153                 } // END - if
154         }
155 }