Skip to content

Commit 9ca3638

Browse files
committed
Fix admin user save
1 parent de550d9 commit 9ca3638

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

Model/Admin/User/Save.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* Copyright © OpenGento, All rights reserved.
4+
* See LICENSE bundled with this library for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Opengento\Hoodoor\Model\Admin\User;
9+
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\Exception\State\UserLockedException;
12+
use Magento\Security\Model\SecurityCookie;
13+
use Magento\User\Controller\Adminhtml\User\Save as CoreSave;
14+
use Magento\User\Model\Spi\NotificationExceptionInterface;
15+
use Magento\User\Model\User;
16+
17+
class Save extends CoreSave
18+
{
19+
private const XML_PATH_ENABLE_ADMIN = 'hoodoor/general/enable_admin';
20+
21+
private $securityCookie;
22+
23+
public function __construct(
24+
private readonly ScopeConfigInterface $scopeConfig,
25+
\Magento\Backend\App\Action\Context $context,
26+
\Magento\Framework\Registry $coreRegistry,
27+
\Magento\User\Model\UserFactory $userFactory
28+
)
29+
{
30+
parent::__construct($context, $coreRegistry, $userFactory);
31+
}
32+
33+
public function execute(): void
34+
{
35+
if(!$this->isEnabled()) {
36+
parent::execute();
37+
return;
38+
}
39+
40+
$userId = (int)$this->getRequest()->getParam('user_id');
41+
$data = $this->getRequest()->getPostValue();
42+
if (array_key_exists('form_key', $data)) {
43+
unset($data['form_key']);
44+
}
45+
if (!$data) {
46+
$this->_redirect('adminhtml/*/');
47+
return;
48+
}
49+
50+
$model = $this->_userFactory->create()->load($userId);
51+
if ($userId && $model->isObjectNew()) {
52+
$this->messageManager->addError(__('This user no longer exists.'));
53+
$this->_redirect('adminhtml/*/');
54+
return;
55+
}
56+
$model->setData($this->_getAdminUserData($data));
57+
$userRoles = $this->getRequest()->getParam('roles', []);
58+
if (count($userRoles)) {
59+
$model->setRoleId($userRoles[0]);
60+
}
61+
62+
/** @var $currentUser User */
63+
$currentUser = $this->_objectManager->get(\Magento\Backend\Model\Auth\Session::class)->getUser();
64+
if ($userId == $currentUser->getId()
65+
&& $this->_objectManager->get(\Magento\Framework\Validator\Locale::class)
66+
->isValid($data['interface_locale'])
67+
) {
68+
$this->_objectManager->get(
69+
\Magento\Backend\Model\Locale\Manager::class
70+
)->switchBackendInterfaceLocale(
71+
$data['interface_locale']
72+
);
73+
}
74+
75+
try {
76+
$model->save();
77+
$this->messageManager->addSuccess(__('You saved the user.'));
78+
$this->_getSession()->setUserData(false);
79+
$this->_redirect('adminhtml/*/');
80+
81+
$model->sendNotificationEmailsIfRequired();
82+
} catch (UserLockedException $e) {
83+
$this->_auth->logout();
84+
$this->getSecurityCookie()->setLogoutReasonCookie(
85+
\Magento\Security\Model\AdminSessionsManager::LOGOUT_REASON_USER_LOCKED
86+
);
87+
$this->_redirect('*');
88+
} catch (NotificationExceptionInterface $exception) {
89+
$this->messageManager->addErrorMessage($exception->getMessage());
90+
} catch (\Magento\Framework\Validator\Exception $e) {
91+
$messages = $e->getMessages();
92+
$this->messageManager->addMessages($messages);
93+
$this->redirectToEdit($model, $data);
94+
} catch (\Magento\Framework\Exception\LocalizedException $e) {
95+
if ($e->getMessage()) {
96+
$this->messageManager->addError($e->getMessage());
97+
}
98+
$this->redirectToEdit($model, $data);
99+
}
100+
}
101+
102+
private function getSecurityCookie()
103+
{
104+
if (!($this->securityCookie instanceof SecurityCookie)) {
105+
return \Magento\Framework\App\ObjectManager::getInstance()->get(SecurityCookie::class);
106+
} else {
107+
return $this->securityCookie;
108+
}
109+
}
110+
111+
private function isEnabled(): bool
112+
{
113+
return $this->scopeConfig->isSetFlag(self::XML_PATH_ENABLE_ADMIN);
114+
}
115+
}

etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
type="Opengento\Hoodoor\Controller\Account\Edit" />
2222
<preference for="Magento\AdminAdobeIms\Plugin\PerformIdentityCheckMessagePlugin"
2323
type="Opengento\Hoodoor\Plugin\AdobeImsReAuth\PerformIdentityCheckMessagePlugin"/>
24+
<preference for="Magento\User\Controller\Adminhtml\User\Save" type="Opengento\Hoodoor\Model\Admin\User\Save"/>
2425
<type name="Magento\Webapi\Controller\Rest">
2526
<plugin name="hoodoor_disable_customer_webapi" type="Opengento\Hoodoor\Plugin\Webapi\Controller\Rest\DisableApi" />
2627
</type>

etc/module.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
<module name="Opengento_Hoodoor">
1111
<sequence>
1212
<module name="Magento_Customer"/>
13+
<module name="Magento_Backend"/>
14+
<module name="Magento_User"/>
1315
</sequence>
1416
</module>
1517
</config>

0 commit comments

Comments
 (0)