Loading password_policy.module +30 −11 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ * Module file for the Password Policy module. */ use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Session\AccountInterface; use Drupal\password_policy\Entity\PasswordPolicy; use Drupal\Core\Entity\EntityInterface; Loading Loading @@ -114,8 +115,8 @@ function password_policy_form_user_form_alter(&$form, FormStateInterface $form_s $form['#after_build'][] = '_password_policy_user_profile_form_after_build'; } // Add the submit handler. $form['actions']['submit']['#submit'][] = '_password_policy_user_profile_form_submit'; // Add validation handler for field updates. $form['#validate'][] = '_password_policy_user_profile_form_update_fields'; } /** Loading Loading @@ -273,23 +274,41 @@ function _password_policy_get_edited_user_roles(&$form, FormStateInterface $form } /** * Set last password reset and expiration fields on password update. * Update Password Policy's fields during user profile form validation. * * If this form submission includes a password change, update Password Policy's * fields to record the change datetime and set the password as not expired. * This must be done in a validation handler (as opposed to submit handler) in * order to support changing the fields values before any submit hooks are run. * * @param array $form * Form array. * @param \Drupal\Core\Form\FormStateInterface $form_state * Form state object. */ function _password_policy_user_profile_form_submit(array &$form, FormStateInterface $form_state) { function _password_policy_user_profile_form_update_fields(array &$form, FormStateInterface $form_state) { // Only execute on submission (i.e. not for AJAX operations). if (!$form_state->isSubmitted()) { return; } // Get current and new password values. $current_pass = $form_state->getValue('current_pass'); $new_pass = $form_state->getValue('pass'); // Get the uid from user object. // Get User ID from User object. /** @var \Drupal\user\UserInterface $user */ $user = $form_state->getFormObject()->getEntity(); $uid = $user->id(); // Update if both current and new password fields are filled out. Depending // on policy settings, user may be allowed to use same password again. if ($uid && ($current_pass || $form_state->get('user_pass_reset')) && $new_pass) { $date = \Drupal::service('date.formatter')->format(\Drupal::time()->getRequestTime(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE); $user->set('field_last_password_reset', $date); $user->set('field_password_expiration', '0'); $user->set('field_pending_expire_sent', '0'); $user->save(); $form_state->setValue( 'field_last_password_reset', [['value' => new DrupalDateTime()]] ); $form_state->setValue('field_password_expiration', ['value' => '0']); $form_state->setValue('field_pending_expire_sent', ['value' => '0']); } } Loading Loading
password_policy.module +30 −11 Original line number Diff line number Diff line Loading @@ -5,6 +5,7 @@ * Module file for the Password Policy module. */ use Drupal\Core\Datetime\DrupalDateTime; use Drupal\Core\Session\AccountInterface; use Drupal\password_policy\Entity\PasswordPolicy; use Drupal\Core\Entity\EntityInterface; Loading Loading @@ -114,8 +115,8 @@ function password_policy_form_user_form_alter(&$form, FormStateInterface $form_s $form['#after_build'][] = '_password_policy_user_profile_form_after_build'; } // Add the submit handler. $form['actions']['submit']['#submit'][] = '_password_policy_user_profile_form_submit'; // Add validation handler for field updates. $form['#validate'][] = '_password_policy_user_profile_form_update_fields'; } /** Loading Loading @@ -273,23 +274,41 @@ function _password_policy_get_edited_user_roles(&$form, FormStateInterface $form } /** * Set last password reset and expiration fields on password update. * Update Password Policy's fields during user profile form validation. * * If this form submission includes a password change, update Password Policy's * fields to record the change datetime and set the password as not expired. * This must be done in a validation handler (as opposed to submit handler) in * order to support changing the fields values before any submit hooks are run. * * @param array $form * Form array. * @param \Drupal\Core\Form\FormStateInterface $form_state * Form state object. */ function _password_policy_user_profile_form_submit(array &$form, FormStateInterface $form_state) { function _password_policy_user_profile_form_update_fields(array &$form, FormStateInterface $form_state) { // Only execute on submission (i.e. not for AJAX operations). if (!$form_state->isSubmitted()) { return; } // Get current and new password values. $current_pass = $form_state->getValue('current_pass'); $new_pass = $form_state->getValue('pass'); // Get the uid from user object. // Get User ID from User object. /** @var \Drupal\user\UserInterface $user */ $user = $form_state->getFormObject()->getEntity(); $uid = $user->id(); // Update if both current and new password fields are filled out. Depending // on policy settings, user may be allowed to use same password again. if ($uid && ($current_pass || $form_state->get('user_pass_reset')) && $new_pass) { $date = \Drupal::service('date.formatter')->format(\Drupal::time()->getRequestTime(), 'custom', DateTimeItemInterface::DATETIME_STORAGE_FORMAT, DateTimeItemInterface::STORAGE_TIMEZONE); $user->set('field_last_password_reset', $date); $user->set('field_password_expiration', '0'); $user->set('field_pending_expire_sent', '0'); $user->save(); $form_state->setValue( 'field_last_password_reset', [['value' => new DrupalDateTime()]] ); $form_state->setValue('field_password_expiration', ['value' => '0']); $form_state->setValue('field_pending_expire_sent', ['value' => '0']); } } Loading