123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Backend\Model;
- use Magento\Framework\Exception\AuthenticationException;
- use Magento\Framework\Exception\Plugin\AuthenticationException as PluginAuthenticationException;
- use Magento\Framework\Phrase;
- /**
- * Backend Auth model
- *
- * @api
- * @since 100.0.2
- */
- class Auth
- {
- /**
- * @var \Magento\Backend\Model\Auth\StorageInterface
- */
- protected $_authStorage;
- /**
- * @var \Magento\Backend\Model\Auth\Credential\StorageInterface
- */
- protected $_credentialStorage;
- /**
- * Backend data
- *
- * @var \Magento\Backend\Helper\Data
- */
- protected $_backendData;
- /**
- * Core event manager proxy
- *
- * @var \Magento\Framework\Event\ManagerInterface
- */
- protected $_eventManager;
- /**
- * @var \Magento\Framework\App\Config\ScopeConfigInterface
- */
- protected $_coreConfig;
- /**
- * @var \Magento\Framework\Data\Collection\ModelFactory
- */
- protected $_modelFactory;
- /**
- * @param \Magento\Framework\Event\ManagerInterface $eventManager
- * @param \Magento\Backend\Helper\Data $backendData
- * @param \Magento\Backend\Model\Auth\StorageInterface $authStorage
- * @param \Magento\Backend\Model\Auth\Credential\StorageInterface $credentialStorage
- * @param \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig
- * @param \Magento\Framework\Data\Collection\ModelFactory $modelFactory
- */
- public function __construct(
- \Magento\Framework\Event\ManagerInterface $eventManager,
- \Magento\Backend\Helper\Data $backendData,
- \Magento\Backend\Model\Auth\StorageInterface $authStorage,
- \Magento\Backend\Model\Auth\Credential\StorageInterface $credentialStorage,
- \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
- \Magento\Framework\Data\Collection\ModelFactory $modelFactory
- ) {
- $this->_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);
- }
- }
|