]> git.mxchange.org Git - city.git/blob - application/city/classes/login/helper/class_CityLoginHelper.php
Next wave:
[city.git] / application / city / classes / login / helper / class_CityLoginHelper.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\City\Helper\Login;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Factory\ObjectFactory;
7 use Org\Mxchange\CoreFramework\Registry\Registry;
8 use Org\Mxchange\CoreFramework\Request\Requestable;
9
10 /**
11  * A helper for City to login. This login helper first checks what setting
12  * (cookie or session) the admin has choosen then overwrites it with the setting
13  * from current user. The registry instance should hold an instance of this user
14  * class at key 'user' else an exception will be thrown. After this the setting
15  * from a login form will be taken as login method and be stored in database
16  * for later usage.
17  *
18  * The user shall be able to choose "Default login method" or similar to use his
19  * own login method.
20  *
21  * @author              Roland Haeder <webmaster@shipsimu.org>
22  * @version             0.0.0
23  * @copyright   Copyright (c) 2015, 2016 City Developer Team
24  * @license             GNU GPL 3.0 or any newer version
25  * @link                http://www.shipsimu.org
26  *
27  * This program is free software: you can redistribute it and/or modify
28  * it under the terms of the GNU General Public License as published by
29  * the Free Software Foundation, either version 3 of the License, or
30  * (at your option) any later version.
31  *
32  * This program is distributed in the hope that it will be useful,
33  * but WITHOUT ANY WARRANTY; without even the implied warranty of
34  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35  * GNU General Public License for more details.
36  *
37  * You should have received a copy of the GNU General Public License
38  * along with this program. If not, see <http://www.gnu.org/licenses/>.
39  */
40 class CityLoginHelper extends BaseLoginHelper implements HelpableLogin {
41         /**
42          * The login method we shall choose
43          */
44         private $authMethod = '';
45
46         // Exception constants
47         const EXCEPTION_INVALID_USER_INSTANCE = 0x190;
48
49         /**
50          * Protected constructor
51          *
52          * @return      void
53          */
54         protected function __construct () {
55                 // Call parent constructor
56                 parent::__construct(__CLASS__);
57         }
58
59         /**
60          * Creates an instance of this class by given request instance
61          *
62          * @param       $requestInstance        An instance of a Requestable class
63          * @return      $helperInstance         An instance of this helper class
64          * @throws      UserInstanceMissingException    If the user instance in registry
65          *                                                                                      is missing or invalid
66          */
67         public static final function createCityLoginHelper (Requestable $requestInstance) {
68                 // Get a new instance first
69                 $helperInstance = new CityLoginHelper();
70
71                 // Get a user instance from registry
72                 $userInstance = Registry::getRegistry()->getInstance('user');
73
74                 // Is this instance valid?
75                 if (!$userInstance instanceof ManageableAccount) {
76                         // Thrown an exception here
77                         throw new UserInstanceMissingException (array($helperInstance, 'user'), self::EXCEPTION_INVALID_USER_INSTANCE);
78                 } // END - if
79
80                 // Set default login method from config
81                 $helperInstance->setDefaultAuthMethod();
82
83                 // Set request instance
84                 $helperInstance->setRequestInstance($requestInstance);
85
86                 // Return the prepared instance
87                 return $helperInstance;
88         }
89
90         /**
91          * Setter for default login method from config
92          *
93          * @return      void
94          */
95         protected function setDefaultAuthMethod () {
96                 $this->authMethod = $this->getConfigInstance()->getConfigEntry('auth_method_class');
97         }
98
99         /**
100          * Execute the login request by given response instance. This instance can
101          * be used for sending cookies or at least the session id out.
102          *
103          * @param       $responseInstance       An instance of a Responseable class
104          * @return      void
105          */
106         public function executeLogin (Responseable $responseInstance) {
107                 // Get an instance from the login method
108                 $loginInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
109
110                 // Set user cookie
111                 $loginInstance->setUserAuth($this->getRequestInstance()->getRequestElement('username'));
112
113                 // Set password cookie
114                 $loginInstance->setPasswordAuth($this->getRequestInstance()->getRequestElement('pass_hash'));
115
116                 // Remember this login instance for later usage
117                 Registry::getRegistry()->addInstance('login', $loginInstance);
118         }
119 }