Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Auth.php
Go to the documentation of this file.
1 <?php
7 namespace Magento\Backend\Model;
8 
10 use Magento\Framework\Exception\Plugin\AuthenticationException as PluginAuthenticationException;
12 
19 class Auth
20 {
24  protected $_authStorage;
25 
30 
36  protected $_backendData;
37 
43  protected $_eventManager;
44 
48  protected $_coreConfig;
49 
53  protected $_modelFactory;
54 
63  public function __construct(
64  \Magento\Framework\Event\ManagerInterface $eventManager,
65  \Magento\Backend\Helper\Data $backendData,
66  \Magento\Backend\Model\Auth\StorageInterface $authStorage,
67  \Magento\Backend\Model\Auth\Credential\StorageInterface $credentialStorage,
68  \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
69  \Magento\Framework\Data\Collection\ModelFactory $modelFactory
70  ) {
71  $this->_eventManager = $eventManager;
72  $this->_backendData = $backendData;
73  $this->_authStorage = $authStorage;
74  $this->_credentialStorage = $credentialStorage;
75  $this->_coreConfig = $coreConfig;
76  $this->_modelFactory = $modelFactory;
77  }
78 
86  public function setAuthStorage($storage)
87  {
88  if (!$storage instanceof \Magento\Backend\Model\Auth\StorageInterface) {
89  self::throwException(__('Authentication storage is incorrect.'));
90  }
91  $this->_authStorage = $storage;
92  return $this;
93  }
94 
102  public function getAuthStorage()
103  {
104  return $this->_authStorage;
105  }
106 
113  public function getUser()
114  {
115  return $this->getAuthStorage()->getUser();
116  }
117 
123  protected function _initCredentialStorage()
124  {
125  $this->_credentialStorage = $this->_modelFactory->create(
126  \Magento\Backend\Model\Auth\Credential\StorageInterface::class
127  );
128  }
129 
136  public function getCredentialStorage()
137  {
139  }
140 
149  public function login($username, $password)
150  {
151  if (empty($username) || empty($password)) {
153  __(
154  'The account sign-in was incorrect or your account is disabled temporarily. '
155  . 'Please wait and try again later.'
156  )
157  );
158  }
159 
160  try {
161  $this->_initCredentialStorage();
162  $this->getCredentialStorage()->login($username, $password);
163  if ($this->getCredentialStorage()->getId()) {
164  $this->getAuthStorage()->setUser($this->getCredentialStorage());
165  $this->getAuthStorage()->processLogin();
166 
167  $this->_eventManager->dispatch(
168  'backend_auth_user_login_success',
169  ['user' => $this->getCredentialStorage()]
170  );
171  }
172 
173  if (!$this->getAuthStorage()->getUser()) {
175  __(
176  'The account sign-in was incorrect or your account is disabled temporarily. '
177  . 'Please wait and try again later.'
178  )
179  );
180  }
181  } catch (PluginAuthenticationException $e) {
182  $this->_eventManager->dispatch(
183  'backend_auth_user_login_failed',
184  ['user_name' => $username, 'exception' => $e]
185  );
186  throw $e;
187  } catch (\Magento\Framework\Exception\LocalizedException $e) {
188  $this->_eventManager->dispatch(
189  'backend_auth_user_login_failed',
190  ['user_name' => $username, 'exception' => $e]
191  );
193  __(
194  $e->getMessage()? : 'The account sign-in was incorrect or your account is disabled temporarily. '
195  . 'Please wait and try again later.'
196  )
197  );
198  }
199  }
200 
206  public function logout()
207  {
208  $this->getAuthStorage()->processLogout();
209  }
210 
216  public function isLoggedIn()
217  {
218  return $this->getAuthStorage()->isLoggedIn();
219  }
220 
229  public static function throwException(Phrase $msg = null)
230  {
231  if ($msg === null) {
232  $msg = __('An authentication error occurred. Verify and try again.');
233  }
234  throw new AuthenticationException($msg);
235  }
236 }
__()
Definition: __.php:13
static throwException(Phrase $msg=null)
Definition: Auth.php:229
__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)
Definition: Auth.php:63
login($username, $password)
Definition: Auth.php:149
setAuthStorage($storage)
Definition: Auth.php:86