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