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