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