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