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