Image generator added, first CAPTCHA added with missing controller (partly work)
[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 $authMethod = "";
37
38         // Exception constants
39         const EXCEPTION_INVALID_USER_INSTANCE = 0x900;
40
41         /**
42          * Protected constructor
43          *
44          * @return      void
45          */
46         protected function __construct () {
47                 // Call parent constructor
48                 parent::__construct(__CLASS__);
49
50                 // Set part description
51                 $this->setObjectDescription("Login helper for Ship-Simu");
52
53                 // Create unique ID number
54                 $this->generateUniqueId();
55         }
56
57         /**
58          * Creates an instance of this class by given request instance
59          *
60          * @param       $requestInstance        An instance of a Requestable class
61          * @return      $helperInstance         An instance of this helper class
62          * @throws      UserInstanceMissingException    If the user instance in registry
63          *                                                                                      is missing or invalid
64          */
65         public final static function createShipSimuLoginHelper (Requestable $requestInstance) {
66                 // Get a new instance first
67                 $helperInstance = new ShipSimuLoginHelper();
68
69                 // Get a user instance from registry
70                 $userInstance = Registry::getRegistry()->getInstance('user');
71
72                 // Is this instance valid?
73                 if (!$userInstance instanceof ManageableAccount) {
74                         // Thrown an exception here
75                         throw new UserInstanceMissingException (array($helperInstance, 'user'), self::EXCEPTION_INVALID_USER_INSTANCE);
76                 } // END - if
77
78                 // Set default login method from config
79                 $helperInstance->setDefaultAuthMethod();
80
81                 // Set request instance
82                 $helperInstance->setRequestInstance($requestInstance);
83
84                 // Return the prepared instance
85                 return $helperInstance;
86         }
87
88         /**
89          * Setter for default login method from config
90          *
91          * @return      void
92          */
93         protected function setDefaultAuthMethod () {
94                 $this->authMethod = $this->getConfigInstance()->readConfig('auth_method_class');
95         }
96
97         /**
98          * Execute the login request by given response instance. This instance can
99          * be used for sending cookies or at least the session id out.
100          *
101          * @param       $responseInstance       An instance of a Responseable class
102          * @return      void
103          */
104         public function executeLogin (Responseable $responseInstance) {
105                 // Get an instance from the login method
106                 $loginInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
107
108                 // Set user cookie
109                 $loginInstance->setUserAuth($this->getRequestInstance()->getRequestElement('username'));
110
111                 // Set password cookie
112                 $loginInstance->setPasswordAuth($this->getRequestInstance()->getRequestElement('pass_hash'));
113
114                 // Remember this login instance for later usage
115                 Registry::getRegistry()->addInstance('login', $loginInstance);
116         }
117 }
118
119 //
120 ?>