Session.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Model\Auth;
  7. use Magento\Framework\Stdlib\Cookie\CookieMetadataFactory;
  8. use Magento\Framework\Stdlib\CookieManagerInterface;
  9. /**
  10. * Backend Auth session model
  11. *
  12. * @api
  13. * @method \Magento\User\Model\User|null getUser()
  14. * @method \Magento\Backend\Model\Auth\Session setUser(\Magento\User\Model\User $value)
  15. * @method \Magento\Framework\Acl|null getAcl()
  16. * @method \Magento\Backend\Model\Auth\Session setAcl(\Magento\Framework\Acl $value)
  17. * @method int getUpdatedAt()
  18. * @method \Magento\Backend\Model\Auth\Session setUpdatedAt(int $value)
  19. *
  20. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  21. * @todo implement solution that keeps is_first_visit flag in session during redirects
  22. * @api
  23. * @since 100.0.2
  24. */
  25. class Session extends \Magento\Framework\Session\SessionManager implements \Magento\Backend\Model\Auth\StorageInterface
  26. {
  27. /**
  28. * Admin session lifetime config path
  29. */
  30. const XML_PATH_SESSION_LIFETIME = 'admin/security/session_lifetime';
  31. /**
  32. * Whether it is the first page after successful login
  33. *
  34. * @var boolean
  35. */
  36. protected $_isFirstAfterLogin;
  37. /**
  38. * Access Control List builder
  39. *
  40. * @var \Magento\Framework\Acl\Builder
  41. */
  42. protected $_aclBuilder;
  43. /**
  44. * @var \Magento\Backend\Model\UrlInterface
  45. */
  46. protected $_backendUrl;
  47. /**
  48. * @var \Magento\Backend\App\ConfigInterface
  49. */
  50. protected $_config;
  51. /**
  52. * @param \Magento\Framework\App\Request\Http $request
  53. * @param \Magento\Framework\Session\SidResolverInterface $sidResolver
  54. * @param \Magento\Framework\Session\Config\ConfigInterface $sessionConfig
  55. * @param \Magento\Framework\Session\SaveHandlerInterface $saveHandler
  56. * @param \Magento\Framework\Session\ValidatorInterface $validator
  57. * @param \Magento\Framework\Session\StorageInterface $storage
  58. * @param CookieManagerInterface $cookieManager
  59. * @param CookieMetadataFactory $cookieMetadataFactory
  60. * @param \Magento\Framework\App\State $appState
  61. * @param \Magento\Framework\Acl\Builder $aclBuilder
  62. * @param \Magento\Backend\Model\UrlInterface $backendUrl
  63. * @param \Magento\Backend\App\ConfigInterface $config
  64. * @throws \Magento\Framework\Exception\SessionException
  65. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  66. */
  67. public function __construct(
  68. \Magento\Framework\App\Request\Http $request,
  69. \Magento\Framework\Session\SidResolverInterface $sidResolver,
  70. \Magento\Framework\Session\Config\ConfigInterface $sessionConfig,
  71. \Magento\Framework\Session\SaveHandlerInterface $saveHandler,
  72. \Magento\Framework\Session\ValidatorInterface $validator,
  73. \Magento\Framework\Session\StorageInterface $storage,
  74. CookieManagerInterface $cookieManager,
  75. CookieMetadataFactory $cookieMetadataFactory,
  76. \Magento\Framework\App\State $appState,
  77. \Magento\Framework\Acl\Builder $aclBuilder,
  78. \Magento\Backend\Model\UrlInterface $backendUrl,
  79. \Magento\Backend\App\ConfigInterface $config
  80. ) {
  81. $this->_config = $config;
  82. $this->_aclBuilder = $aclBuilder;
  83. $this->_backendUrl = $backendUrl;
  84. parent::__construct(
  85. $request,
  86. $sidResolver,
  87. $sessionConfig,
  88. $saveHandler,
  89. $validator,
  90. $storage,
  91. $cookieManager,
  92. $cookieMetadataFactory,
  93. $appState
  94. );
  95. }
  96. /**
  97. * Refresh ACL resources stored in session
  98. *
  99. * @param \Magento\User\Model\User $user
  100. * @return \Magento\Backend\Model\Auth\Session
  101. */
  102. public function refreshAcl($user = null)
  103. {
  104. if ($user === null) {
  105. $user = $this->getUser();
  106. }
  107. if (!$user) {
  108. return $this;
  109. }
  110. if (!$this->getAcl() || $user->getReloadAclFlag()) {
  111. $this->setAcl($this->_aclBuilder->getAcl());
  112. }
  113. if ($user->getReloadAclFlag()) {
  114. $user->unsetData('password');
  115. $user->setReloadAclFlag('0')->save();
  116. }
  117. return $this;
  118. }
  119. /**
  120. * Check current user permission on resource and privilege
  121. *
  122. * @param string $resource
  123. * @param string $privilege
  124. * @return boolean
  125. */
  126. public function isAllowed($resource, $privilege = null)
  127. {
  128. $user = $this->getUser();
  129. $acl = $this->getAcl();
  130. if ($user && $acl) {
  131. try {
  132. return $acl->isAllowed($user->getAclRole(), $resource, $privilege);
  133. } catch (\Exception $e) {
  134. try {
  135. if (!$acl->has($resource)) {
  136. return $acl->isAllowed($user->getAclRole(), null, $privilege);
  137. }
  138. } catch (\Exception $e) {
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144. /**
  145. * Check if user is logged in
  146. *
  147. * @return boolean
  148. */
  149. public function isLoggedIn()
  150. {
  151. return $this->getUser() && $this->getUser()->getId();
  152. }
  153. /**
  154. * Set session UpdatedAt to current time
  155. *
  156. * @return void
  157. */
  158. public function prolong()
  159. {
  160. $lifetime = $this->_config->getValue(self::XML_PATH_SESSION_LIFETIME);
  161. $cookieValue = $this->cookieManager->getCookie($this->getName());
  162. if ($cookieValue) {
  163. $this->setUpdatedAt(time());
  164. $cookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata()
  165. ->setDuration($lifetime)
  166. ->setPath($this->sessionConfig->getCookiePath())
  167. ->setDomain($this->sessionConfig->getCookieDomain())
  168. ->setSecure($this->sessionConfig->getCookieSecure())
  169. ->setHttpOnly($this->sessionConfig->getCookieHttpOnly());
  170. $this->cookieManager->setPublicCookie($this->getName(), $cookieValue, $cookieMetadata);
  171. }
  172. }
  173. /**
  174. * Check if it is the first page after successful login
  175. *
  176. * @return bool
  177. */
  178. public function isFirstPageAfterLogin()
  179. {
  180. if ($this->_isFirstAfterLogin === null) {
  181. $this->_isFirstAfterLogin = $this->getData('is_first_visit', true);
  182. }
  183. return $this->_isFirstAfterLogin;
  184. }
  185. /**
  186. * Setter whether the current/next page should be treated as first page after login
  187. *
  188. * @param bool $value
  189. * @return \Magento\Backend\Model\Auth\Session
  190. */
  191. public function setIsFirstPageAfterLogin($value)
  192. {
  193. $this->_isFirstAfterLogin = (bool)$value;
  194. return $this->setIsFirstVisit($this->_isFirstAfterLogin);
  195. }
  196. /**
  197. * Process of configuring of current auth storage when login was performed
  198. *
  199. * @return \Magento\Backend\Model\Auth\Session
  200. */
  201. public function processLogin()
  202. {
  203. if ($this->getUser()) {
  204. $this->regenerateId();
  205. if ($this->_backendUrl->useSecretKey()) {
  206. $this->_backendUrl->renewSecretUrls();
  207. }
  208. $this->setIsFirstPageAfterLogin(true);
  209. $this->setAcl($this->_aclBuilder->getAcl());
  210. $this->setUpdatedAt(time());
  211. }
  212. return $this;
  213. }
  214. /**
  215. * Process of configuring of current auth storage when logout was performed
  216. *
  217. * @return \Magento\Backend\Model\Auth\Session
  218. */
  219. public function processLogout()
  220. {
  221. $this->destroy();
  222. return $this;
  223. }
  224. /**
  225. * Skip path validation in backend area
  226. *
  227. * @param string $path
  228. * @return bool
  229. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  230. * @codeCoverageIgnore
  231. */
  232. public function isValidForPath($path)
  233. {
  234. return true;
  235. }
  236. }