Results are now searchable and iterateable, insertDataSet renamed to queryInsertDataS...
[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  * @todo                Find an interface name for login helper
19  *
20  * This program is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28  * GNU General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program. If not, see <http://www.gnu.org/licenses/>.
32  */
33 class ShipSimuLoginHelper extends BaseLoginHelper {
34         // Exception constants
35         const EXCEPTION_INVALID_USER_INSTANCE = 0xf00;
36
37         /**
38          * Protected constructor
39          *
40          * @return      void
41          */
42         protected function __construct () {
43                 // Call parent constructor
44                 parent::__construct(__CLASS__);
45
46                 // Set part description
47                 $this->setObjectDescription("Login helper for Ship-Simu");
48
49                 // Create unique ID number
50                 $this->generateUniqueId();
51         }
52
53         /**
54          * Creates an instance of this class by given request instance
55          *
56          * @param       $requestInstance        An instance of a Requestable class
57          * @return      $helperInstance         An instance of this helper class
58          * @throws      UserInstanceMissingException    If the user instance in registry
59          *                                                                                      is missing or invalid
60          */
61         public final static function createShipSimuLoginHelper (Requestable $requestInstance) {
62                 // Get a new instance first
63                 $helperInstance = new ShipSimuLoginHelper();
64
65                 // Get a user instance from registry
66                 $userInstance = Registry::getRegistry()->getInstance('user');
67
68                 // Is this instance valid?
69                 if (!$userInstance instanceof ManageableUser) {
70                         // Thrown an exception here
71                         throw new UserInstanceMissingException (array($helperInstance, 'user'), self::EXCEPTION_INVALID_USER_INSTANCE);
72                 } // END - if
73
74                 // Get the login method from request
75                 $methodRequest = $requestInstance->getRequestElement('login_method');
76
77                 // Now, if that wents fine we can check if the request includes a login method entry
78                 if ((!is_null($methodRequest)) && ($methodRequest != "default") && ($methodRequest != $userInstance->getLoginMethod())) {
79                         // Okay, the login method has been choosen by user so remember it
80                         $helperInstance->setLoginMethod($methodRequest);
81
82                         // Remeber that we need to update the user account as well
83                         $userInstance->addUpdateData('login_method', $methodRequest);
84                 } elseif (($methodRequest == "default") && (is_string($userInstance->getLoginMethod()))) {
85                         // Choose default method from user
86                         $helperInstance->setLoginMethod($userInstance->getLoginMethod());
87                 } else {
88                         // Set default login method from config
89                         $helperInstance->setDefaultLoginMethod();
90                 }
91                 
92                 // Return the prepared instance
93                 return $helperInstance;
94         }
95 }
96
97 // [EOF]
98 ?>