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