3 namespace Org\Mxchange\City\Helper\Login;
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;
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
24 * The user shall be able to choose "Default login method" or similar to use his
27 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
46 class CityLoginHelper extends BaseLoginHelper implements HelpableLogin {
48 * The login method we shall choose
50 private $authMethod = '';
52 // Exception constants
53 const EXCEPTION_INVALID_USER_INSTANCE = 0x190;
56 * Protected constructor
60 protected function __construct () {
61 // Call parent constructor
62 parent::__construct(__CLASS__);
66 * Creates an instance of this class by given request instance
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
72 public static final function createCityLoginHelper (Requestable $requestInstance) {
73 // Get a new instance first
74 $helperInstance = new CityLoginHelper();
76 // Get a user instance from registry
77 $userInstance = GenericRegistry::getRegistry()->getInstance('user');
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);
85 // Set default login method from config
86 $helperInstance->setDefaultAuthMethod();
88 // Return the prepared instance
89 return $helperInstance;
93 * Setter for default login method from config
97 protected function setDefaultAuthMethod () {
98 $this->authMethod = FrameworkBootstrap::getConfigurationInstance()->getConfigEntry('auth_method_class');
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.
105 * @param $responseInstance An instance of a Responseable class
108 public function executeLogin (Responseable $responseInstance) {
109 // Get an instance from the login method
110 $loginInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
113 $loginInstance->setUserAuth(FrameworkBootstrap::getRequestInstance()->getRequestElement('username'));
115 // Set password cookie
116 $loginInstance->setPasswordAuth(FrameworkBootstrap::getRequestInstance()->getRequestElement('pass_hash'));
118 // Remember this login instance for later usage
119 GenericRegistry::getRegistry()->addInstance('login', $loginInstance);