3 namespace Org\Mxchange\City\Helper\Login;
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;
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
18 * The user shall be able to choose "Default login method" or similar to use his
21 * @author Roland Haeder <webmaster@shipsimu.org>
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
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.
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.
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/>.
40 class CityLoginHelper extends BaseLoginHelper implements HelpableLogin {
42 * The login method we shall choose
44 private $authMethod = '';
46 // Exception constants
47 const EXCEPTION_INVALID_USER_INSTANCE = 0x190;
50 * Protected constructor
54 protected function __construct () {
55 // Call parent constructor
56 parent::__construct(__CLASS__);
60 * Creates an instance of this class by given request instance
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
67 public static final function createCityLoginHelper (Requestable $requestInstance) {
68 // Get a new instance first
69 $helperInstance = new CityLoginHelper();
71 // Get a user instance from registry
72 $userInstance = Registry::getRegistry()->getInstance('user');
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);
80 // Set default login method from config
81 $helperInstance->setDefaultAuthMethod();
83 // Set request instance
84 $helperInstance->setRequestInstance($requestInstance);
86 // Return the prepared instance
87 return $helperInstance;
91 * Setter for default login method from config
95 protected function setDefaultAuthMethod () {
96 $this->authMethod = $this->getConfigInstance()->getConfigEntry('auth_method_class');
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.
103 * @param $responseInstance An instance of a Responseable class
106 public function executeLogin (Responseable $responseInstance) {
107 // Get an instance from the login method
108 $loginInstance = ObjectFactory::createObjectByName($this->authMethod, array($responseInstance));
111 $loginInstance->setUserAuth($this->getRequestInstance()->getRequestElement('username'));
113 // Set password cookie
114 $loginInstance->setPasswordAuth($this->getRequestInstance()->getRequestElement('pass_hash'));
116 // Remember this login instance for later usage
117 Registry::getRegistry()->addInstance('login', $loginInstance);