]> git.mxchange.org Git - shipsimu.git/blob - application/ship-simu/main/login/helper/class_ShipSimuLoginHelper.php
Cookie-based login initially done
[shipsimu.git] / application / ship-simu / main / login / helper / class_ShipSimuLoginHelper.php
1 <?php
2 /**
3  * A helper for Ship-Simu to login. This login helper first checks what setting
4  * (cookie or session) the admin has choosen then overwrites it with the setting
5  * from current user. The registry instance should hold an instance of this user
6  * class at key 'user' else an exception will be thrown. After this the setting
7  * from a login form will be taken as login method and be stored in database
8  * for later usage.
9  *
10  * The user shall be able to choose "Default login method" or similar to use his
11  * own login method.
12  *
13  * @author              Roland Haeder <webmaster@ship-simu.org>
14  * @version             0.0.0
15  * @copyright   Copyright(c) 2007, 2008 Roland Haeder, this is free software
16  * @license             GNU GPL 3.0 or any newer version
17  * @link                http://www.ship-simu.org
18  *
19  * This program is free software: you can redistribute it and/or modify
20  * it under the terms of the GNU General Public License as published by
21  * the Free Software Foundation, either version 3 of the License, or
22  * (at your option) any later version.
23  *
24  * This program is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with this program. If not, see <http://www.gnu.org/licenses/>.
31  */
32 class ShipSimuLoginHelper extends BaseLoginHelper implements HelpableLogin {
33         /**
34          * The login method we shall choose
35          */
36         private $loginMethod = "";
37
38         /**
39          * Instance for a request class
40          */
41         private $requestInstance = null;
42
43         // Exception constants
44         const EXCEPTION_INVALID_USER_INSTANCE = 0xf00;
45
46         /**
47          * Protected constructor
48          *
49          * @return      void
50          */
51         protected function __construct () {
52                 // Call parent constructor
53                 parent::__construct(__CLASS__);
54
55                 // Set part description
56                 $this->setObjectDescription("Login helper for Ship-Simu");
57
58                 // Create unique ID number
59                 $this->generateUniqueId();
60         }
61
62         /**
63          * Creates an instance of this class by given request instance
64          *
65          * @param       $requestInstance        An instance of a Requestable class
66          * @return      $helperInstance         An instance of this helper class
67          * @throws      UserInstanceMissingException    If the user instance in registry
68          *                                                                                      is missing or invalid
69          */
70         public final static function createShipSimuLoginHelper (Requestable $requestInstance) {
71                 // Get a new instance first
72                 $helperInstance = new ShipSimuLoginHelper();
73
74                 // Get a user instance from registry
75                 $userInstance = Registry::getRegistry()->getInstance('user');
76
77                 // Is this instance valid?
78                 if (!$userInstance instanceof ManageableUser) {
79                         // Thrown an exception here
80                         throw new UserInstanceMissingException (array($helperInstance, 'user'), self::EXCEPTION_INVALID_USER_INSTANCE);
81                 } // END - if
82
83                 // Get the login method from request
84                 $methodRequest = $requestInstance->getRequestElement('login_method');
85
86                 // Now, if that wents fine we can check if the request includes a login method entry
87                 if ((!is_null($methodRequest)) && ($methodRequest != "default") && ($methodRequest != $userInstance->getLoginMethod())) {
88                         // Okay, the login method has been choosen by user so remember it
89                         $helperInstance->setLoginMethod($methodRequest);
90
91                         // Remeber that we need to update the user account as well
92                         $userInstance->addUpdateData('login_method', $methodRequest);
93                 } elseif (($methodRequest == "default") && (is_string($userInstance->getLoginMethod()))) {
94                         // Choose default method from user
95                         $helperInstance->setLoginMethod($userInstance->getLoginMethod());
96                 } else {
97                         // Set default login method from config
98                         $helperInstance->setDefaultLoginMethod();
99                 }
100
101                 // Set request instance
102                 $helperInstance->setRequestInstance($requestInstance);
103
104                 // Return the prepared instance
105                 return $helperInstance;
106         }
107
108         /**
109          * Setter for default login method from config
110          *
111          * @return      void
112          */
113         protected function setDefaultLoginMethod () {
114                 $this->loginMethod = $this->getConfigInstance()->readConfig('login_method');
115         }
116
117         /**
118          * Setter for request instance
119          *
120          * @param       $requestInstance        A Requestable class instance
121          * @return      void
122          */
123         public final function setRequestInstance (Requestable $requestInstance) {
124                 $this->requestInstance = $requestInstance;
125         }
126
127         /**
128          * Getter for request instance
129          *
130          * @param       
131          * @return      $requestInstance        A Requestable class instance
132          */
133         public final function getRequestInstance () {
134                 return $this->requestInstance;
135         }
136
137         /**
138          * Execute the login request by given response instance. This instance can
139          * be used for sending cookies or at least the session id out.
140          *
141          * @param       $responseInstance       An instance of a Responseable class
142          * @return      void
143          */
144         public function executeLogin (Responseable $responseInstance) {
145                 // First create the requested login method name
146                 $loginMethodClass = ucfirst(strtolower($this->loginMethod)) . "Login";
147
148                 // Then try to get an instance from it
149                 $loginInstance = ObjectFactory::createObjectByName($loginMethodClass, array($responseInstance));
150
151                 // Set user cookie
152                 $loginInstance->setUserAuth($this->requestInstance->getRequestElement('username'));
153
154                 // Set password cookie
155                 $loginInstance->setPasswordAuth($this->requestInstance->getRequestElement('pass_hash'));
156
157                 // Remember this login instance for later usage
158                 Registry::getRegistry()->addInstance('login', $loginInstance);
159         }
160 }
161
162 //
163 ?>