ForceAdminPasswordChangeObserver.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Observer\Backend;
  7. use Magento\Framework\Event\Observer as EventObserver;
  8. use Magento\Framework\Event\ObserverInterface;
  9. /**
  10. * User backend observer model for passwords
  11. */
  12. class ForceAdminPasswordChangeObserver implements ObserverInterface
  13. {
  14. /**
  15. * Backend configuration interface
  16. *
  17. * @var \Magento\User\Model\Backend\Config\ObserverConfig
  18. */
  19. protected $observerConfig;
  20. /**
  21. * Authorization interface
  22. *
  23. * @var \Magento\Framework\AuthorizationInterface
  24. */
  25. protected $authorization;
  26. /**
  27. * Backend url interface
  28. *
  29. * @var \Magento\Backend\Model\UrlInterface
  30. */
  31. protected $url;
  32. /**
  33. * Backend session
  34. *
  35. * @var \Magento\Backend\Model\Session
  36. */
  37. protected $session;
  38. /**
  39. * Backend authorization session
  40. *
  41. * @var \Magento\Backend\Model\Auth\Session
  42. */
  43. protected $authSession;
  44. /**
  45. * Action flag
  46. *
  47. * @var \Magento\Framework\App\ActionFlag
  48. */
  49. protected $actionFlag;
  50. /**
  51. * Message manager interface
  52. *
  53. * @var \Magento\Framework\Message\ManagerInterface
  54. */
  55. protected $messageManager;
  56. /**
  57. * @param \Magento\Framework\AuthorizationInterface $authorization
  58. * @param \Magento\User\Model\Backend\Config\ObserverConfig $observerConfig
  59. * @param \Magento\Backend\Model\UrlInterface $url
  60. * @param \Magento\Backend\Model\Session $session
  61. * @param \Magento\Backend\Model\Auth\Session $authSession
  62. * @param \Magento\Framework\App\ActionFlag $actionFlag
  63. * @param \Magento\Framework\Message\ManagerInterface $messageManager
  64. */
  65. public function __construct(
  66. \Magento\Framework\AuthorizationInterface $authorization,
  67. \Magento\User\Model\Backend\Config\ObserverConfig $observerConfig,
  68. \Magento\Backend\Model\UrlInterface $url,
  69. \Magento\Backend\Model\Session $session,
  70. \Magento\Backend\Model\Auth\Session $authSession,
  71. \Magento\Framework\App\ActionFlag $actionFlag,
  72. \Magento\Framework\Message\ManagerInterface $messageManager
  73. ) {
  74. $this->authorization = $authorization;
  75. $this->observerConfig = $observerConfig;
  76. $this->url = $url;
  77. $this->session = $session;
  78. $this->authSession = $authSession;
  79. $this->actionFlag = $actionFlag;
  80. $this->messageManager = $messageManager;
  81. }
  82. /**
  83. * Force admin to change password
  84. *
  85. * @param EventObserver $observer
  86. * @return void
  87. */
  88. public function execute(EventObserver $observer)
  89. {
  90. if (!$this->observerConfig->isPasswordChangeForced()) {
  91. return;
  92. }
  93. if (!$this->authSession->isLoggedIn()) {
  94. return;
  95. }
  96. $actionList = [
  97. 'adminhtml_system_account_index',
  98. 'adminhtml_system_account_save',
  99. 'adminhtml_auth_logout',
  100. 'mui_index_render'
  101. ];
  102. /** @var \Magento\Framework\App\Action\Action $controller */
  103. $controller = $observer->getEvent()->getControllerAction();
  104. /** @var \Magento\Framework\App\RequestInterface $request */
  105. $request = $observer->getEvent()->getRequest();
  106. if ($this->authSession->getPciAdminUserIsPasswordExpired()) {
  107. if (!in_array($request->getFullActionName(), $actionList)) {
  108. if ($this->authorization->isAllowed('Magento_Backend::myaccount')) {
  109. $controller->getResponse()->setRedirect($this->url->getUrl('adminhtml/system_account/'));
  110. $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
  111. $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_POST_DISPATCH, true);
  112. } else {
  113. /*
  114. * if admin password is expired and access to 'My Account' page is denied
  115. * than we need to do force logout with error message
  116. */
  117. $this->authSession->clearStorage();
  118. $this->session->clearStorage();
  119. $this->messageManager->addErrorMessage(
  120. __('Your password has expired; please contact your administrator.')
  121. );
  122. $controller->getRequest()->setDispatched(false);
  123. }
  124. }
  125. }
  126. }
  127. }