Auth.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model;
  7. use Magento\Framework\Exception\AuthenticationException;
  8. use Magento\Framework\Exception\Plugin\AuthenticationException as PluginAuthenticationException;
  9. use Magento\Framework\Phrase;
  10. /**
  11. * Backend Auth model
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Auth
  17. {
  18. /**
  19. * @var \Magento\Backend\Model\Auth\StorageInterface
  20. */
  21. protected $_authStorage;
  22. /**
  23. * @var \Magento\Backend\Model\Auth\Credential\StorageInterface
  24. */
  25. protected $_credentialStorage;
  26. /**
  27. * Backend data
  28. *
  29. * @var \Magento\Backend\Helper\Data
  30. */
  31. protected $_backendData;
  32. /**
  33. * Core event manager proxy
  34. *
  35. * @var \Magento\Framework\Event\ManagerInterface
  36. */
  37. protected $_eventManager;
  38. /**
  39. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  40. */
  41. protected $_coreConfig;
  42. /**
  43. * @var \Magento\Framework\Data\Collection\ModelFactory
  44. */
  45. protected $_modelFactory;
  46. /**
  47. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  48. * @param \Magento\Backend\Helper\Data $backendData
  49. * @param \Magento\Backend\Model\Auth\StorageInterface $authStorage
  50. * @param \Magento\Backend\Model\Auth\Credential\StorageInterface $credentialStorage
  51. * @param \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig
  52. * @param \Magento\Framework\Data\Collection\ModelFactory $modelFactory
  53. */
  54. public function __construct(
  55. \Magento\Framework\Event\ManagerInterface $eventManager,
  56. \Magento\Backend\Helper\Data $backendData,
  57. \Magento\Backend\Model\Auth\StorageInterface $authStorage,
  58. \Magento\Backend\Model\Auth\Credential\StorageInterface $credentialStorage,
  59. \Magento\Framework\App\Config\ScopeConfigInterface $coreConfig,
  60. \Magento\Framework\Data\Collection\ModelFactory $modelFactory
  61. ) {
  62. $this->_eventManager = $eventManager;
  63. $this->_backendData = $backendData;
  64. $this->_authStorage = $authStorage;
  65. $this->_credentialStorage = $credentialStorage;
  66. $this->_coreConfig = $coreConfig;
  67. $this->_modelFactory = $modelFactory;
  68. }
  69. /**
  70. * Set auth storage if it is instance of \Magento\Backend\Model\Auth\StorageInterface
  71. *
  72. * @param \Magento\Backend\Model\Auth\StorageInterface $storage
  73. * @return $this
  74. * @throws \Magento\Framework\Exception\AuthenticationException
  75. */
  76. public function setAuthStorage($storage)
  77. {
  78. if (!$storage instanceof \Magento\Backend\Model\Auth\StorageInterface) {
  79. self::throwException(__('Authentication storage is incorrect.'));
  80. }
  81. $this->_authStorage = $storage;
  82. return $this;
  83. }
  84. /**
  85. * Return auth storage.
  86. * If auth storage was not defined outside - returns default object of auth storage
  87. *
  88. * @return \Magento\Backend\Model\Auth\StorageInterface
  89. * @codeCoverageIgnore
  90. */
  91. public function getAuthStorage()
  92. {
  93. return $this->_authStorage;
  94. }
  95. /**
  96. * Return current (successfully authenticated) user,
  97. * an instance of \Magento\Backend\Model\Auth\Credential\StorageInterface
  98. *
  99. * @return \Magento\Backend\Model\Auth\Credential\StorageInterface
  100. */
  101. public function getUser()
  102. {
  103. return $this->getAuthStorage()->getUser();
  104. }
  105. /**
  106. * Initialize credential storage from configuration
  107. *
  108. * @return void
  109. */
  110. protected function _initCredentialStorage()
  111. {
  112. $this->_credentialStorage = $this->_modelFactory->create(
  113. \Magento\Backend\Model\Auth\Credential\StorageInterface::class
  114. );
  115. }
  116. /**
  117. * Return credential storage object
  118. *
  119. * @return null|\Magento\Backend\Model\Auth\Credential\StorageInterface
  120. * @codeCoverageIgnore
  121. */
  122. public function getCredentialStorage()
  123. {
  124. return $this->_credentialStorage;
  125. }
  126. /**
  127. * Perform login process
  128. *
  129. * @param string $username
  130. * @param string $password
  131. * @return void
  132. * @throws \Magento\Framework\Exception\AuthenticationException
  133. */
  134. public function login($username, $password)
  135. {
  136. if (empty($username) || empty($password)) {
  137. self::throwException(
  138. __(
  139. 'The account sign-in was incorrect or your account is disabled temporarily. '
  140. . 'Please wait and try again later.'
  141. )
  142. );
  143. }
  144. try {
  145. $this->_initCredentialStorage();
  146. $this->getCredentialStorage()->login($username, $password);
  147. if ($this->getCredentialStorage()->getId()) {
  148. $this->getAuthStorage()->setUser($this->getCredentialStorage());
  149. $this->getAuthStorage()->processLogin();
  150. $this->_eventManager->dispatch(
  151. 'backend_auth_user_login_success',
  152. ['user' => $this->getCredentialStorage()]
  153. );
  154. }
  155. if (!$this->getAuthStorage()->getUser()) {
  156. self::throwException(
  157. __(
  158. 'The account sign-in was incorrect or your account is disabled temporarily. '
  159. . 'Please wait and try again later.'
  160. )
  161. );
  162. }
  163. } catch (PluginAuthenticationException $e) {
  164. $this->_eventManager->dispatch(
  165. 'backend_auth_user_login_failed',
  166. ['user_name' => $username, 'exception' => $e]
  167. );
  168. throw $e;
  169. } catch (\Magento\Framework\Exception\LocalizedException $e) {
  170. $this->_eventManager->dispatch(
  171. 'backend_auth_user_login_failed',
  172. ['user_name' => $username, 'exception' => $e]
  173. );
  174. self::throwException(
  175. __(
  176. $e->getMessage()? : 'The account sign-in was incorrect or your account is disabled temporarily. '
  177. . 'Please wait and try again later.'
  178. )
  179. );
  180. }
  181. }
  182. /**
  183. * Perform logout process
  184. *
  185. * @return void
  186. */
  187. public function logout()
  188. {
  189. $this->getAuthStorage()->processLogout();
  190. }
  191. /**
  192. * Check if current user is logged in
  193. *
  194. * @return bool
  195. */
  196. public function isLoggedIn()
  197. {
  198. return $this->getAuthStorage()->isLoggedIn();
  199. }
  200. /**
  201. * Throws specific Backend Authentication \Exception
  202. *
  203. * @param \Magento\Framework\Phrase $msg
  204. * @return void
  205. * @throws \Magento\Framework\Exception\AuthenticationException
  206. * @static
  207. */
  208. public static function throwException(Phrase $msg = null)
  209. {
  210. if ($msg === null) {
  211. $msg = __('An authentication error occurred. Verify and try again.');
  212. }
  213. throw new AuthenticationException($msg);
  214. }
  215. }