View source
<?php
namespace Drupal\change_pwd_page\Form;
use Drupal\user\Entity\User;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\UserInterface;
use Drupal\Core\Url;
use Drupal\Core\Password\PasswordInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ChangePasswordForm extends FormBase {
protected $passwordHasher;
protected $userProfile;
public function __construct(PasswordInterface $password_hasher) {
$this->passwordHasher = $password_hasher;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('password'));
}
public function getFormId() {
return 'change_pwd_form';
}
public function buildForm(array $form, FormStateInterface $form_state, UserInterface $user = NULL) {
$this->userProfile = $account = $user;
$user = $this
->currentUser();
$config = \Drupal::config('user.settings');
$form['#cache']['tags'] = $config
->getCacheTags();
$register = $account
->isAnonymous();
$form['account'] = [
'#type' => 'container',
'#weight' => -10,
];
if (!$register) {
$form['account']['pass'] = [
'#type' => 'password_confirm',
'#size' => 25,
'#description' => $this
->t('To change the current user password, enter the new password in both fields.'),
'#required' => TRUE,
];
if (!$form_state
->get('user_pass_reset') && ($token = $this
->getRequest()
->get('pass-reset-token'))) {
$session_key = 'pass_reset_' . $account
->id();
$user_pass_reset = isset($_SESSION[$session_key]) && hash_equals($_SESSION[$session_key], $token);
$form_state
->set('user_pass_reset', $user_pass_reset);
}
if ($user
->id() == $account
->id()) {
$form['account']['current_pass'] = [
'#type' => 'password',
'#title' => $this
->t('Current password'),
'#size' => 25,
'#access' => !$form_state
->get('user_pass_reset'),
'#weight' => -5,
'#attributes' => [
'autocomplete' => 'off',
],
'#required' => TRUE,
];
$form_state
->set('user', $account);
if (!$form_state
->get('user_pass_reset')) {
$form['account']['current_pass']['#description'] = $this
->t('Required if you want to change the %pass below. <a href=":request_new_url" title="Send password reset instructions via email.">Reset your password</a>.', [
'%pass' => $this
->t('Password'),
':request_new_url' => Url::fromRoute('user.pass')
->toString(),
]);
}
}
$roles = array_map([
'\\Drupal\\Component\\Utility\\Html',
'escape',
], user_role_names(TRUE));
$form['account']['roles'] = [
'#type' => 'checkboxes',
'#title' => $this
->t('Roles'),
'#default_value' => !$register ? $account
->getRoles() : [],
'#options' => $roles,
'#access' => FALSE,
];
}
$form['actions'] = [
'#type' => 'actions',
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Submit'),
];
return $form;
}
public function validateForm(array &$form, FormStateInterface $form_state) {
$current_pass_input = trim($form_state
->getValue('current_pass'));
if ($current_pass_input) {
$user = User::load(\Drupal::currentUser()
->id());
if (!$this->passwordHasher
->check($current_pass_input, $user
->getPassword())) {
$form_state
->setErrorByName('current_pass', $this
->t('The current password you provided is incorrect.'));
}
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$user = User::load($this->userProfile
->id());
$user
->setPassword($form_state
->getValue('pass'));
$user
->save();
$this
->messenger()
->addStatus($this
->t('Your password has been changed.'));
}
public function getEntity() {
return $this->userProfile;
}
}