Skip to content

Commit bcef590

Browse files
committed
replace changes in User module with plugin for admin user form (#22833: Short-term admin accounts)
1 parent 89b8512 commit bcef590

File tree

3 files changed

+74
-19
lines changed

3 files changed

+74
-19
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Security\Model\Plugin;
9+
10+
/**
11+
* Add the `expires_at` form field to the User main form.
12+
*
13+
* @package Magento\Security\Model\Plugin
14+
*/
15+
class AdminUserForm
16+
{
17+
18+
/**
19+
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
20+
*/
21+
private $localeDate;
22+
23+
/**
24+
* UserForm constructor.
25+
*
26+
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
27+
*/
28+
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
29+
{
30+
$this->localeDate = $localeDate;
31+
}
32+
33+
/**
34+
* Add the `expires_at` field to the admin user edit form.
35+
* @param \Magento\User\Block\User\Edit\Tab\Main $subject
36+
* @param \Closure $proceed
37+
* @return mixed
38+
*/
39+
public function aroundGetFormHtml(
40+
\Magento\User\Block\User\Edit\Tab\Main $subject,
41+
\Closure $proceed
42+
) {
43+
/** @var \Magento\Framework\Data\Form $form */
44+
$form = $subject->getForm();
45+
if (is_object($form)) {
46+
$dateFormat = $this->localeDate->getDateFormat(
47+
\IntlDateFormatter::MEDIUM
48+
);
49+
$timeFormat = $this->localeDate->getTimeFormat(
50+
\IntlDateFormatter::MEDIUM
51+
);
52+
$fieldset = $form->getElement('base_fieldset');
53+
$fieldset->addField(
54+
'expires_at',
55+
'date',
56+
[
57+
'name' => 'expires_at',
58+
'label' => __('Expiration Date'),
59+
'title' => __('Expiration Date'),
60+
'date_format' => $dateFormat,
61+
'time_format' => $timeFormat,
62+
'class' => 'validate-date',
63+
]
64+
);
65+
66+
$subject->setForm($form);
67+
}
68+
69+
return $proceed();
70+
}
71+
}

app/code/Magento/Security/etc/adminhtml/di.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,7 @@
3131
</argument>
3232
</arguments>
3333
</type>
34+
<type name="Magento\User\Block\User\Edit\Tab\Main">
35+
<plugin name="user_expiration_user_form_field" type="Magento\Security\Model\Plugin\AdminUserForm"/>
36+
</type>
3437
</config>

app/code/Magento/User/Block/User/Edit/Tab/Main.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -170,25 +170,6 @@ protected function _prepareForm()
170170
);
171171
}
172172

173-
$dateFormat = $this->_localeDate->getDateFormat(
174-
\IntlDateFormatter::MEDIUM
175-
);
176-
$timeFormat = $this->_localeDate->getTimeFormat(
177-
\IntlDateFormatter::MEDIUM
178-
);
179-
$baseFieldset->addField(
180-
'expires_at',
181-
'date',
182-
[
183-
'name' => 'expires_at',
184-
'label' => __('Expiration Date'),
185-
'title' => __('Expiration Date'),
186-
'date_format' => $dateFormat,
187-
'time_format' => $timeFormat,
188-
'class' => 'validate-date',
189-
]
190-
);
191-
192173
$baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);
193174

194175
$currentUserVerificationFieldset = $form->addFieldset(

0 commit comments

Comments
 (0)