_eventManager = $eventManager; $this->_backendData = $backendData; $this->_authStorage = $authStorage; $this->_credentialStorage = $credentialStorage; $this->_coreConfig = $coreConfig; $this->_modelFactory = $modelFactory; } /** * Set auth storage if it is instance of \Magento\Backend\Model\Auth\StorageInterface * * @param \Magento\Backend\Model\Auth\StorageInterface $storage * @return $this * @throws \Magento\Framework\Exception\AuthenticationException */ public function setAuthStorage($storage) { if (!$storage instanceof \Magento\Backend\Model\Auth\StorageInterface) { self::throwException(__('Authentication storage is incorrect.')); } $this->_authStorage = $storage; return $this; } /** * Return auth storage. * If auth storage was not defined outside - returns default object of auth storage * * @return \Magento\Backend\Model\Auth\StorageInterface * @codeCoverageIgnore */ public function getAuthStorage() { return $this->_authStorage; } /** * Return current (successfully authenticated) user, * an instance of \Magento\Backend\Model\Auth\Credential\StorageInterface * * @return \Magento\Backend\Model\Auth\Credential\StorageInterface */ public function getUser() { return $this->getAuthStorage()->getUser(); } /** * Initialize credential storage from configuration * * @return void */ protected function _initCredentialStorage() { $this->_credentialStorage = $this->_modelFactory->create( \Magento\Backend\Model\Auth\Credential\StorageInterface::class ); } /** * Return credential storage object * * @return null|\Magento\Backend\Model\Auth\Credential\StorageInterface * @codeCoverageIgnore */ public function getCredentialStorage() { return $this->_credentialStorage; } /** * Perform login process * * @param string $username * @param string $password * @return void * @throws \Magento\Framework\Exception\AuthenticationException */ public function login($username, $password) { if (empty($username) || empty($password)) { self::throwException( __( 'The account sign-in was incorrect or your account is disabled temporarily. ' . 'Please wait and try again later.' ) ); } try { $this->_initCredentialStorage(); $this->getCredentialStorage()->login($username, $password); if ($this->getCredentialStorage()->getId()) { $this->getAuthStorage()->setUser($this->getCredentialStorage()); $this->getAuthStorage()->processLogin(); $this->_eventManager->dispatch( 'backend_auth_user_login_success', ['user' => $this->getCredentialStorage()] ); } if (!$this->getAuthStorage()->getUser()) { self::throwException( __( 'The account sign-in was incorrect or your account is disabled temporarily. ' . 'Please wait and try again later.' ) ); } } catch (PluginAuthenticationException $e) { $this->_eventManager->dispatch( 'backend_auth_user_login_failed', ['user_name' => $username, 'exception' => $e] ); throw $e; } catch (\Magento\Framework\Exception\LocalizedException $e) { $this->_eventManager->dispatch( 'backend_auth_user_login_failed', ['user_name' => $username, 'exception' => $e] ); self::throwException( __( $e->getMessage()? : 'The account sign-in was incorrect or your account is disabled temporarily. ' . 'Please wait and try again later.' ) ); } } /** * Perform logout process * * @return void */ public function logout() { $this->getAuthStorage()->processLogout(); } /** * Check if current user is logged in * * @return bool */ public function isLoggedIn() { return $this->getAuthStorage()->isLoggedIn(); } /** * Throws specific Backend Authentication \Exception * * @param \Magento\Framework\Phrase $msg * @return void * @throws \Magento\Framework\Exception\AuthenticationException * @static */ public static function throwException(Phrase $msg = null) { if ($msg === null) { $msg = __('An authentication error occurred. Verify and try again.'); } throw new AuthenticationException($msg); } }