External core repository finally set
[shipsimu.git] / application / admin / main / login / class_AdminUserLogin.php
1 <?php
2 /**
3  * A special login class for administration area
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 AdminUserLogin 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                 // Clean up a little
40                 $this->removeNumberFormaters();
41                 $this->removeSystemArray();
42         }
43
44         /**
45          * Creates an instance of this login class
46          *
47          * @return      $loginInstance  An instance of this login class
48          */
49         public final static function createAdminUserLogin () {
50                 // Get a new instance
51                 $loginInstance = new AdminUserLogin();
52
53                 // Return the instance
54                 return $loginInstance;
55         }
56
57         /**
58          * Logins the user with the given request containing the credential. The
59          * result of the login can be thrown by exception or, if prefered stored
60          * in a boolean attribute which is then readable by a matching getter.
61          *
62          * @param       $requestInstance        An instance of a Requestable class
63          * @param       $responseInstance       An instance of a Responseable class
64          * @return      void
65          * @throws      UserPasswordMismatchException   If the supplied password did not
66          *                                                                              match with the stored password
67          * @todo        We need to add something here which will make more than one
68          * @todo        guest logins, users who are online but based on the same
69          * @todo        user account.
70          */
71         public function doLogin (Requestable $requestInstance, Responseable $responseInstance) {
72                 // By default no method is selected
73                 $method = null;
74                 $data = "";
75
76                 // Get a instance of the registry
77                 $userInstance = Registry::getRegistry()->getInstance('user');
78
79                 // Is there an instance?
80                 if (is_null($userInstance)) {
81                         // Get member class
82                         $userClass = $this->getConfigInstance()->readConfig('user_class');
83
84                         // Get a user instance
85                         $userInstance = call_user_func_array(array($userClass, 'createMemberByRequest'), array($requestInstance));
86
87                         // Remember this new instance in registry
88                         Registry::getRegistry()->addInstance($userInstance);
89                 } // END - if
90
91                 // Is the password correct?
92                 if ($userInstance->ifPasswordHashMatches($requestInstance) === false) {
93                         // Mismatching password
94                         throw new UserPasswordMismatchException(array($this, $userInstance), BaseUser::EXCEPTION_USER_PASS_MISMATCH);
95                 } // END - if
96
97                 // ToDo place
98
99                 // Now do the real login. This can be cookie- or session-based login
100                 // which depends on the admins setting then on the user's taste.
101                 // 1) Get a login helper instance
102                 $helperInstance = ObjectFactory::createObjectByConfiguredName('login_helper_class', array($requestInstance));
103
104                 // 2) Execute the login. This will now login...
105                 $helperInstance->executeLogin($responseInstance);
106         }
107
108         /**
109          * Determines wether the login was fine. This is done by checking if 'login' instance is in registry
110          *
111          * @return      $loginDone      Wether the login was fine or not
112          */
113         public function ifLoginWasSuccessfull () {
114                 // Is the registry key there?
115                 $loginDone = (Registry::getRegistry()->getInstance('login') instanceof Registerable);
116
117                 // Return the result
118                 return $loginDone;
119         }
120
121         /**
122          * Encrypt given request key or throw an exception if key was not found in
123          * request
124          *
125          * @param       $requestKey             Key in request class
126          * @return      void
127          */
128         public function encryptPassword ($requestKey) {
129                 // Check if password is found in request
130                 if ($this->getRequestInstance()->isRequestElementSet($requestKey)) {
131                         // So encrypt the password and store it for later usage in
132                         // the request:
133
134                         // Get the plain password
135                         $plainPassword = $this->getRequestInstance()->getRequestElement($requestKey);
136
137                         // Get user instance
138                         $userInstance = Registry::getRegistry()->getInstance('user');
139
140                         // Get a crypto helper and hash the password
141                         $this->hashedPassword = ObjectFactory::createObjectByConfiguredName('crypto_class')->hashString($plainPassword, $userInstance->getPasswordHash());
142
143                         // Store the hash back in request
144                         $this->getRequestInstance()->setRequestElement('pass_hash', $this->hashedPassword);
145                 } // END - if
146         }
147 }
148
149 // [EOF]
150 ?>