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