Main.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\User\Block\User\Edit\Tab;
  7. use Magento\Framework\App\ObjectManager;
  8. use Magento\Framework\Locale\OptionInterface;
  9. /**
  10. * Cms page edit form main tab
  11. *
  12. * @SuppressWarnings(PHPMD.DepthOfInheritance)
  13. */
  14. class Main extends \Magento\Backend\Block\Widget\Form\Generic
  15. {
  16. const CURRENT_USER_PASSWORD_FIELD = 'current_password';
  17. /**
  18. * @var \Magento\Backend\Model\Auth\Session
  19. */
  20. protected $_authSession;
  21. /**
  22. * @var \Magento\Framework\Locale\ListsInterface
  23. */
  24. protected $_LocaleLists;
  25. /**
  26. * Operates with deployed locales.
  27. *
  28. * @var OptionInterface
  29. */
  30. private $deployedLocales;
  31. /**
  32. * @param \Magento\Backend\Block\Template\Context $context
  33. * @param \Magento\Framework\Registry $registry
  34. * @param \Magento\Framework\Data\FormFactory $formFactory
  35. * @param \Magento\Backend\Model\Auth\Session $authSession
  36. * @param \Magento\Framework\Locale\ListsInterface $localeLists
  37. * @param array $data
  38. * @param OptionInterface $deployedLocales Operates with deployed locales.
  39. */
  40. public function __construct(
  41. \Magento\Backend\Block\Template\Context $context,
  42. \Magento\Framework\Registry $registry,
  43. \Magento\Framework\Data\FormFactory $formFactory,
  44. \Magento\Backend\Model\Auth\Session $authSession,
  45. \Magento\Framework\Locale\ListsInterface $localeLists,
  46. array $data = [],
  47. OptionInterface $deployedLocales = null
  48. ) {
  49. $this->_authSession = $authSession;
  50. $this->_LocaleLists = $localeLists;
  51. $this->deployedLocales = $deployedLocales
  52. ?: ObjectManager::getInstance()->get(OptionInterface::class);
  53. parent::__construct($context, $registry, $formFactory, $data);
  54. }
  55. /**
  56. * Prepare form fields
  57. *
  58. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  59. * @return \Magento\Backend\Block\Widget\Form
  60. */
  61. protected function _prepareForm()
  62. {
  63. /** @var $model \Magento\User\Model\User */
  64. $model = $this->_coreRegistry->registry('permissions_user');
  65. /** @var \Magento\Framework\Data\Form $form */
  66. $form = $this->_formFactory->create();
  67. $form->setHtmlIdPrefix('user_');
  68. $baseFieldset = $form->addFieldset('base_fieldset', ['legend' => __('Account Information')]);
  69. if ($model->getUserId()) {
  70. $baseFieldset->addField('user_id', 'hidden', ['name' => 'user_id']);
  71. } else {
  72. if (!$model->hasData('is_active')) {
  73. $model->setIsActive(1);
  74. }
  75. }
  76. $baseFieldset->addField(
  77. 'username',
  78. 'text',
  79. [
  80. 'name' => 'username',
  81. 'label' => __('User Name'),
  82. 'id' => 'username',
  83. 'title' => __('User Name'),
  84. 'required' => true
  85. ]
  86. );
  87. $baseFieldset->addField(
  88. 'firstname',
  89. 'text',
  90. [
  91. 'name' => 'firstname',
  92. 'label' => __('First Name'),
  93. 'id' => 'firstname',
  94. 'title' => __('First Name'),
  95. 'required' => true
  96. ]
  97. );
  98. $baseFieldset->addField(
  99. 'lastname',
  100. 'text',
  101. [
  102. 'name' => 'lastname',
  103. 'label' => __('Last Name'),
  104. 'id' => 'lastname',
  105. 'title' => __('Last Name'),
  106. 'required' => true
  107. ]
  108. );
  109. $baseFieldset->addField(
  110. 'email',
  111. 'text',
  112. [
  113. 'name' => 'email',
  114. 'label' => __('Email'),
  115. 'id' => 'customer_email',
  116. 'title' => __('User Email'),
  117. 'class' => 'required-entry validate-email',
  118. 'required' => true
  119. ]
  120. );
  121. $isNewObject = $model->isObjectNew();
  122. if ($isNewObject) {
  123. $passwordLabel = __('Password');
  124. } else {
  125. $passwordLabel = __('New Password');
  126. }
  127. $confirmationLabel = __('Password Confirmation');
  128. $this->_addPasswordFields($baseFieldset, $passwordLabel, $confirmationLabel, $isNewObject);
  129. $baseFieldset->addField(
  130. 'interface_locale',
  131. 'select',
  132. [
  133. 'name' => 'interface_locale',
  134. 'label' => __('Interface Locale'),
  135. 'title' => __('Interface Locale'),
  136. 'values' => $this->deployedLocales->getOptionLocales(),
  137. 'class' => 'select'
  138. ]
  139. );
  140. if ($this->_authSession->getUser()->getId() != $model->getUserId()) {
  141. $baseFieldset->addField(
  142. 'is_active',
  143. 'select',
  144. [
  145. 'name' => 'is_active',
  146. 'label' => __('This account is'),
  147. 'id' => 'is_active',
  148. 'title' => __('Account Status'),
  149. 'class' => 'input-select',
  150. 'options' => ['1' => __('Active'), '0' => __('Inactive')]
  151. ]
  152. );
  153. }
  154. $baseFieldset->addField('user_roles', 'hidden', ['name' => 'user_roles', 'id' => '_user_roles']);
  155. $currentUserVerificationFieldset = $form->addFieldset(
  156. 'current_user_verification_fieldset',
  157. ['legend' => __('Current User Identity Verification')]
  158. );
  159. $currentUserVerificationFieldset->addField(
  160. self::CURRENT_USER_PASSWORD_FIELD,
  161. 'password',
  162. [
  163. 'name' => self::CURRENT_USER_PASSWORD_FIELD,
  164. 'label' => __('Your Password'),
  165. 'id' => self::CURRENT_USER_PASSWORD_FIELD,
  166. 'title' => __('Your Password'),
  167. 'class' => 'input-text validate-current-password required-entry',
  168. 'required' => true
  169. ]
  170. );
  171. $data = $model->getData();
  172. unset($data['password']);
  173. unset($data[self::CURRENT_USER_PASSWORD_FIELD]);
  174. $form->setValues($data);
  175. $this->setForm($form);
  176. return parent::_prepareForm();
  177. }
  178. /**
  179. * Add password input fields
  180. *
  181. * @param \Magento\Framework\Data\Form\Element\Fieldset $fieldset
  182. * @param string $passwordLabel
  183. * @param string $confirmationLabel
  184. * @param bool $isRequired
  185. * @return void
  186. */
  187. protected function _addPasswordFields(
  188. \Magento\Framework\Data\Form\Element\Fieldset $fieldset,
  189. $passwordLabel,
  190. $confirmationLabel,
  191. $isRequired = false
  192. ) {
  193. $requiredFieldClass = $isRequired ? ' required-entry' : '';
  194. $fieldset->addField(
  195. 'password',
  196. 'password',
  197. [
  198. 'name' => 'password',
  199. 'label' => $passwordLabel,
  200. 'id' => 'customer_pass',
  201. 'title' => $passwordLabel,
  202. 'class' => 'input-text validate-admin-password' . $requiredFieldClass,
  203. 'required' => $isRequired
  204. ]
  205. );
  206. $fieldset->addField(
  207. 'confirmation',
  208. 'password',
  209. [
  210. 'name' => 'password_confirmation',
  211. 'label' => $confirmationLabel,
  212. 'id' => 'confirmation',
  213. 'title' => $confirmationLabel,
  214. 'class' => 'input-text validate-cpassword' . $requiredFieldClass,
  215. 'required' => $isRequired
  216. ]
  217. );
  218. }
  219. }