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