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