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