]> git.mxchange.org Git - shipsimu.git/blob - application/ship-simu/main/login/class_ShipSimuUserLogin.php
Class/method doc-tags fixed
[shipsimu.git] / application / ship-simu / main / login / class_ShipSimuUserLogin.php
1 <?php
2 /**
3  * A special login class for Ship-Simu
4  *
5  * @author              Roland Haeder <webmaster@ship-simu.org>
6  * @version             0.0.0
7  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
8  * @license             GNU GPL 3.0 or any newer version
9  * @link                http://www.ship-simu.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 ShipSimuUserLogin extends BaseFrameworkSystem implements LoginableUser {
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                 // Set part description
40                 $this->setObjectDescription("Login for Ship-Simu");
41
42                 // Create unique ID number
43                 $this->generateUniqueId();
44
45                 // Clean up a little
46                 $this->removeNumberFormaters();
47                 $this->removeSystemArray();
48         }
49
50         /**
51          * Creates an instance of this login class
52          *
53          * @return      $loginInstance  An instance of this login class
54          */
55         public final static function createShipSimuUserLogin () {
56                 // Get a new instance
57                 $loginInstance = new ShipSimuUserLogin();
58
59                 // Return the instance
60                 return $loginInstance;
61         }
62
63         /**
64          * Logins the user with the given request containing the credential. The
65          * result of the login can be thrown by exception or, if prefered stored
66          * in a boolean attribute which is then readable by a matching getter.
67          *
68          * @param       $requestInstance        An instance of a Requestable class
69          * @param       $responseInstance       An instance of a Responseable class
70          * @return      void
71          * @throws      UserAuthMethodException If wether username nor email login
72          *                                                                              was detected
73          * @throws      MissingMethodException          If a method was not found in the
74          *                                                                              User class
75          * @throws      UserPasswordMismatchException   If the supplied password did not
76          *                                                                              match with the stored password
77          * @todo        We need to add something here which will make more than one
78          * @todo        guest logins, users who are online but based on the same
79          * @todo        user account.
80          */
81         public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
82                 // By default no method is selected
83                 $method = null;
84                 $data = "";
85
86                 // Detect login method (username or email) and try to get a userinstance
87                 if (!is_null($requestInstance->getRequestElement('username'))) {
88                         // Username found!
89                         $method = "createUserByUsername";
90                         $data = $requestInstance->getRequestElement('username');
91                 } elseif (!is_null($requestInstance->getRequestElement('email'))) {
92                         // Email found!
93                         $method = "createUserByEmail";
94                         $data = $requestInstance->getRequestElement('email');
95                 }
96
97                 // Is a method detected?
98                 if (is_null($method)) {
99                         // Then abort here
100                         throw new UserAuthMethodException($this, self::EXCEPTION_MISSING_METHOD);
101                 } elseif (!method_exists($this->getConfigInstance()->readConfig('user_class'), $method)) {
102                         // The method is invalid!
103                         throw new MissingMethodException(array($this, $method), self::EXCEPTION_MISSING_METHOD);
104                 }
105
106                 // Get a instance of the registry
107                 $userInstance = Registry::getRegistry()->getInstance('user');
108
109                 // Is there an instance?
110                 if (is_null($userInstance)) {
111                         // Get a user instance
112                         $userInstance = call_user_func_array(array($this->getConfigInstance()->readConfig('user_class'), $method), array($data));
113
114                         // Remember this new instance in registry
115                         Registry::getRegistry()->addInstance($userInstance);
116                 } // END - if
117
118                 // Is the password correct?
119                 if (!$userInstance->ifPasswordHashMatches($requestInstance)) {
120                         // Mismatching password
121                         throw new UserPasswordMismatchException(array($this, $userInstance), User::EXCEPTION_USER_PASS_MISMATCH);
122                 } // END - if
123
124                 // ToDo place
125
126                 // Now do the real login. This can be cookie- or session-based login
127                 // which depends on the admins setting then on the user's taste.
128                 // 1) Get a login helper instance
129                 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
130
131                 // 2) Execute the login. This will now login...
132                 $helperInstance->executeLogin($responseInstance);
133         }
134
135         /**
136          * Determines wether the login was fine. This is done by checking if the 'login' instance is in registry
137          *
138          * @return      $loginDone      Wether the login was fine or not
139          */
140         public function ifLoginWasSuccessfull () {
141                 // Is the registry key there?
142                 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
143
144                 // Return the result
145                 return $loginDone;
146         }
147
148         /**
149          * Encrypt the given request key or throw an exception if the key was not
150          * found in the request
151          *
152          * @param       $requestKey             Key in request class
153          * @return      void
154          */
155         public function encryptPassword ($requestKey) {
156                 // Check if the password is found in the request
157                 if ($this->getRequestInstance()->isRequestElementSet($requestKey)) {
158                         // So encrypt the password and store it for later usage in
159                         // the request:
160
161                         // Get the plain password
162                         $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey);
163
164                         // Get user instance
165                         $userInstance = Registry::getRegistry()->getInstance('user');
166
167                         // Get a crypto helper and hash the password
168                         $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashPassword($plainPassword, $userInstance->getPasswordHash());
169
170                         // Store the hash back in the request
171                         $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);
172                 } // END - if
173         }
174 }
175
176 // [EOF]
177 ?>