Form.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Backend\Block\System\Account\Edit;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Locale\OptionInterface;
  9. /**
  10. * Adminhtml edit admin user account form
  11. *
  12. * @author Magento Core Team <core@magentocommerce.com>
  13. */
  14. class Form extends \Magento\Backend\Block\Widget\Form\Generic
  15. {
  16. const IDENTITY_VERIFICATION_PASSWORD_FIELD = 'current_password';
  17. /**
  18. * @var \Magento\Backend\Model\Auth\Session
  19. */
  20. protected $_authSession;
  21. /**
  22. * @var \Magento\User\Model\UserFactory
  23. */
  24. protected $_userFactory;
  25. /**
  26. * @var \Magento\Framework\Locale\ListsInterface
  27. */
  28. protected $_localeLists;
  29. /**
  30. * Operates with deployed locales.
  31. *
  32. * @var OptionInterface
  33. */
  34. private $deployedLocales;
  35. /**
  36. * @param \Magento\Backend\Block\Template\Context $context
  37. * @param \Magento\Framework\Registry $registry
  38. * @param \Magento\Framework\Data\FormFactory $formFactory
  39. * @param \Magento\User\Model\UserFactory $userFactory
  40. * @param \Magento\Backend\Model\Auth\Session $authSession
  41. * @param \Magento\Framework\Locale\ListsInterface $localeLists
  42. * @param array $data
  43. * @param OptionInterface $deployedLocales Operates with deployed locales
  44. */
  45. public function __construct(
  46. \Magento\Backend\Block\Template\Context $context,
  47. \Magento\Framework\Registry $registry,
  48. \Magento\Framework\Data\FormFactory $formFactory,
  49. \Magento\User\Model\UserFactory $userFactory,
  50. \Magento\Backend\Model\Auth\Session $authSession,
  51. \Magento\Framework\Locale\ListsInterface $localeLists,
  52. array $data = [],
  53. OptionInterface $deployedLocales = null
  54. ) {
  55. $this->_userFactory = $userFactory;
  56. $this->_authSession = $authSession;
  57. $this->_localeLists = $localeLists;
  58. $this->deployedLocales = $deployedLocales
  59. ?: ObjectManager::getInstance()->get(OptionInterface::class);
  60. parent::__construct($context, $registry, $formFactory, $data);
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. protected function _prepareForm()
  66. {
  67. $userId = $this->_authSession->getUser()->getId();
  68. $user = $this->_userFactory->create()->load($userId);
  69. $user->unsetData('password');
  70. /** @var \Magento\Framework\Data\Form $form */
  71. $form = $this->_formFactory->create();
  72. $fieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information')]);
  73. $fieldset->addField(
  74. 'username',
  75. 'text',
  76. ['name' => 'username', 'label' => __('User Name'), 'title' => __('User Name'), 'required' => true]
  77. );
  78. $fieldset->addField(
  79. 'firstname',
  80. 'text',
  81. ['name' => 'firstname', 'label' => __('First Name'), 'title' => __('First Name'), 'required' => true]
  82. );
  83. $fieldset->addField(
  84. 'lastname',
  85. 'text',
  86. ['name' => 'lastname', 'label' => __('Last Name'), 'title' => __('Last Name'), 'required' => true]
  87. );
  88. $fieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
  89. $fieldset->addField(
  90. 'email',
  91. 'text',
  92. ['name' => 'email', 'label' => __('Email'), 'title' => __('User Email'), 'required' => true]
  93. );
  94. $fieldset->addField(
  95. 'password',
  96. 'password',
  97. [
  98. 'name' => 'password',
  99. 'label' => __('New Password'),
  100. 'title' => __('New Password'),
  101. 'class' => 'validate-admin-password admin__control-text'
  102. ]
  103. );
  104. $fieldset->addField(
  105. 'confirmation',
  106. 'password',
  107. [
  108. 'name' => 'password_confirmation',
  109. 'label' => __('Password Confirmation'),
  110. 'class' => 'validate-cpassword admin__control-text'
  111. ]
  112. );
  113. $fieldset->addField(
  114. 'interface_locale',
  115. 'select',
  116. [
  117. 'name' => 'interface_locale',
  118. 'label' => __('Interface Locale'),
  119. 'title' => __('Interface Locale'),
  120. 'values' => $this->deployedLocales->getTranslatedOptionLocales(),
  121. 'class' => 'select'
  122. ]
  123. );
  124. $verificationFieldset = $form->addFieldset(
  125. 'current_user_verification_fieldset',
  126. ['legend' => __('Current User Identity Verification')]
  127. );
  128. $verificationFieldset->addField(
  129. self::IDENTITY_VERIFICATION_PASSWORD_FIELD,
  130. 'password',
  131. [
  132. 'name' => self::IDENTITY_VERIFICATION_PASSWORD_FIELD,
  133. 'label' => __('Your Password'),
  134. 'id' => self::IDENTITY_VERIFICATION_PASSWORD_FIELD,
  135. 'title' => __('Your Password'),
  136. 'class' => 'validate-current-password required-entry admin__control-text',
  137. 'required' => true
  138. ]
  139. );
  140. $data = $user->getData();
  141. unset($data[self::IDENTITY_VERIFICATION_PASSWORD_FIELD]);
  142. $form->setValues($data);
  143. $form->setAction($this->getUrl('adminhtml/system_account/save'));
  144. $form->setMethod('post');
  145. $form->setUseContainer(true);
  146. $form->setId('edit_form');
  147. $this->setForm($form);
  148. return parent::_prepareForm();
  149. }
  150. }