Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ForceAdminPasswordChangeObserver.php
Go to the documentation of this file.
1 <?php
8 
11 
16 {
22  protected $observerConfig;
23 
29  protected $authorization;
30 
36  protected $url;
37 
43  protected $session;
44 
50  protected $authSession;
51 
57  protected $actionFlag;
58 
64  protected $messageManager;
65 
75  public function __construct(
76  \Magento\Framework\AuthorizationInterface $authorization,
77  \Magento\User\Model\Backend\Config\ObserverConfig $observerConfig,
78  \Magento\Backend\Model\UrlInterface $url,
79  \Magento\Backend\Model\Session $session,
80  \Magento\Backend\Model\Auth\Session $authSession,
81  \Magento\Framework\App\ActionFlag $actionFlag,
82  \Magento\Framework\Message\ManagerInterface $messageManager
83  ) {
84  $this->authorization = $authorization;
85  $this->observerConfig = $observerConfig;
86  $this->url = $url;
87  $this->session = $session;
88  $this->authSession = $authSession;
89  $this->actionFlag = $actionFlag;
90  $this->messageManager = $messageManager;
91  }
92 
99  public function execute(EventObserver $observer)
100  {
101  if (!$this->observerConfig->isPasswordChangeForced()) {
102  return;
103  }
104  if (!$this->authSession->isLoggedIn()) {
105  return;
106  }
107  $actionList = [
108  'adminhtml_system_account_index',
109  'adminhtml_system_account_save',
110  'adminhtml_auth_logout',
111  'mui_index_render'
112  ];
114  $controller = $observer->getEvent()->getControllerAction();
116  $request = $observer->getEvent()->getRequest();
117 
118  if ($this->authSession->getPciAdminUserIsPasswordExpired()) {
119  if (!in_array($request->getFullActionName(), $actionList)) {
120  if ($this->authorization->isAllowed('Magento_Backend::myaccount')) {
121  $controller->getResponse()->setRedirect($this->url->getUrl('adminhtml/system_account/'));
122  $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_DISPATCH, true);
123  $this->actionFlag->set('', \Magento\Framework\App\Action\Action::FLAG_NO_POST_DISPATCH, true);
124  } else {
125  /*
126  * if admin password is expired and access to 'My Account' page is denied
127  * than we need to do force logout with error message
128  */
129  $this->authSession->clearStorage();
130  $this->session->clearStorage();
131  $this->messageManager->addErrorMessage(
132  __('Your password has expired; please contact your administrator.')
133  );
134  $controller->getRequest()->setDispatched(false);
135  }
136  }
137  }
138  }
139 }
__()
Definition: __.php:13
__construct(\Magento\Framework\AuthorizationInterface $authorization, \Magento\User\Model\Backend\Config\ObserverConfig $observerConfig, \Magento\Backend\Model\UrlInterface $url, \Magento\Backend\Model\Session $session, \Magento\Backend\Model\Auth\Session $authSession, \Magento\Framework\App\ActionFlag $actionFlag, \Magento\Framework\Message\ManagerInterface $messageManager)
$controller
Definition: info.phtml:14