Skip to content

Commit

Permalink
replace changes in User module with plugin for admin user form (#22833:…
Browse files Browse the repository at this point in the history
… Short-term admin accounts)
  • Loading branch information
lfolco committed Sep 30, 2019
1 parent 89b8512 commit bcef590
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 19 deletions.
71 changes: 71 additions & 0 deletions app/code/Magento/Security/Model/Plugin/AdminUserForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Security\Model\Plugin;

/**
* Add the `expires_at` form field to the User main form.
*
* @package Magento\Security\Model\Plugin
*/
class AdminUserForm
{

/**
* @var \Magento\Framework\Stdlib\DateTime\TimezoneInterface
*/
private $localeDate;

/**
* UserForm constructor.
*
* @param \Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate
*/
public function __construct(\Magento\Framework\Stdlib\DateTime\TimezoneInterface $localeDate)
{
$this->localeDate = $localeDate;
}

/**
* Add the `expires_at` field to the admin user edit form.
* @param \Magento\User\Block\User\Edit\Tab\Main $subject
* @param \Closure $proceed
* @return mixed
*/
public function aroundGetFormHtml(
\Magento\User\Block\User\Edit\Tab\Main $subject,
\Closure $proceed
) {
/** @var \Magento\Framework\Data\Form $form */
$form = $subject->getForm();
if (is_object($form)) {
$dateFormat = $this->localeDate->getDateFormat(
\IntlDateFormatter::MEDIUM
);
$timeFormat = $this->localeDate->getTimeFormat(
\IntlDateFormatter::MEDIUM
);
$fieldset = $form->getElement('base_fieldset');
$fieldset->addField(
'expires_at',
'date',
[
'name' => 'expires_at',
'label' => __('Expiration Date'),
'title' => __('Expiration Date'),
'date_format' => $dateFormat,
'time_format' => $timeFormat,
'class' => 'validate-date',
]
);

$subject->setForm($form);
}

return $proceed();
}
}
3 changes: 3 additions & 0 deletions app/code/Magento/Security/etc/adminhtml/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,7 @@
</argument>
</arguments>
</type>
<type name="Magento\User\Block\User\Edit\Tab\Main">
<plugin name="user_expiration_user_form_field" type="Magento\Security\Model\Plugin\AdminUserForm"/>
</type>
</config>
19 changes: 0 additions & 19 deletions app/code/Magento/User/Block/User/Edit/Tab/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,25 +170,6 @@ protected function _prepareForm()
);
}

$dateFormat = $this->_localeDate->getDateFormat(
\IntlDateFormatter::MEDIUM
);
$timeFormat = $this->_localeDate->getTimeFormat(
\IntlDateFormatter::MEDIUM
);
$baseFieldset->addField(
'expires_at',
'date',
[
'name' => 'expires_at',
'label' => __('Expiration Date'),
'title' => __('Expiration Date'),
'date_format' => $dateFormat,
'time_format' => $timeFormat,
'class' => 'validate-date',
]
);

$baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);

$currentUserVerificationFieldset = $form->addFieldset(
Expand Down

0 comments on commit bcef590

Please sign in to comment.