Commit 45f9bf64 authored by Marcos Lima's avatar Marcos Lima Committed by Paulo Henrique Cota Starling
Browse files

Issue #2849271 by JeroenT, paulocs, cslevy, Nigel Cunningham, tatarbj,...

Issue #2849271 by JeroenT, paulocs, cslevy, Nigel Cunningham, tatarbj, voleger, lucasbaralm, kevin.dutra, marcos_lima, tucho, ThomWilhelm, mroycroft, lucassc, vsujeetkumar, rpayanm, the_g_bomb, sophiavs, andregp, apathak: Decouple display of policy table and policy validation
parent 029a696b
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -32,6 +32,9 @@ password_policy.password_policy.*:
      sequence:
        type: password_policy.constraint.plugin.[id]
        label: 'Constraint'
    show_policy_table:
      type: boolean
      label: 'Show policy table'
    roles:
      type: sequence
      label: 'Roles'
+5 −42
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@
 * 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;
@@ -73,23 +72,8 @@ function password_policy_form_user_form_alter(&$form, FormStateInterface $form_s
    $form['field_pending_expire_sent']['#access'] = FALSE;
  }

  // @todo Password editing of existing account is broken, AJAX reloads
  // current password and password multiple times
  // user interface changes
  // @todo Consider hiding Password Strength indicator and Password
  // Recommendations.
  $form['account']['roles']['#weight'] = '0';
  $form['account']['mail']['#weight'] = '1';
  $form['account']['name']['#weight'] = '2';
  $form['account']['status']['#weight'] = '5';
  $form['account']['notify']['#weight'] = '6';
  $form['account']['pass']['#weight'] = '3';

  // Check for specific conditions.
  $show_password_policy_status = _password_policy_show_policy(get_defined_vars());

  // Load form if relevant.
  if ($show_password_policy_status) {
  if (\Drupal::service('password_policy.validation_manager')->tableShouldBeVisible()) {
    $form['account']['password_policy_status'] = [
      '#theme' => 'password_policy_status',
      '#prefix' => '<div id="password-policy-status">',
@@ -110,7 +94,9 @@ function password_policy_form_user_form_alter(&$form, FormStateInterface $form_s
      'method' => 'replace',
      'wrapper' => 'password-policy-status',
    ];
  }

  if (\Drupal::service('password_policy.validation_manager')->validationShouldRun()) {
    $form['#validate'][] = '_password_policy_user_profile_form_validate';
    $form['#after_build'][] = '_password_policy_user_profile_form_after_build';
  }
@@ -128,29 +114,6 @@ function password_policy_element_info_alter(array &$types) {
  }
}

/**
 * Determine if the password policy should be shown.
 *
 * @param array $context
 *   Context of function execution.
 *
 * @return bool
 *   Result of whether the password policy should be shown.
 */
function _password_policy_show_policy(array $context = []) {
  $account = \Drupal::currentUser();
  $config = \Drupal::config('user.settings');
  $show_password_policy_status = TRUE;

  if ($account->isAnonymous() and $config->get('verify_mail')) {
    $show_password_policy_status = FALSE;
  }

  Drupal::moduleHandler()
    ->alter('password_policy_show_policy', $show_password_policy_status, $context);
  return $show_password_policy_status;
}

/**
 * Custom callback to update the password confirm element.
 *
@@ -168,7 +131,7 @@ function password_policy_check_constraints_password_confirm_process(array $eleme
  $form_object = $form_state->getFormObject();

  if (method_exists($form_object, 'getEntity') && $form_object->getEntity() instanceof UserInterface) {
    if (_password_policy_show_policy(get_defined_vars())) {
    if (\Drupal::service('password_policy.validation_manager')->tableShouldBeVisible()) {
      $element['pass1']['#ajax'] = [
        'event' => 'change',
        'callback' => '_password_policy_check_constraints',
@@ -201,7 +164,7 @@ function password_policy_check_constraints_password_confirm_process(array $eleme
function _password_policy_user_profile_form_after_build($form, FormStateInterface &$form_state) {
  $password_invisible = empty($form['account']['pass']) || (isset($form['account']['pass']['#access']) ? !$form['account']['pass']['#access'] : FALSE);

  if ($password_invisible) {
  if ($password_invisible && isset($form['account']['password_policy_status'])) {
    $form['account']['password_policy_status']['#access'] = FALSE;
  }

+8 −1
Original line number Diff line number Diff line
@@ -4,6 +4,13 @@ services:
    class: Drupal\password_policy\PasswordConstraintPluginManager
    parent: default_plugin_manager

  password_policy.validation_manager:
    class: Drupal\password_policy\PasswordPolicyValidationManager
    arguments:
      - '@config.factory'
      - '@current_user'
      - '@entity_type.manager'

  password_policy_event_subscriber:
      class: Drupal\password_policy\EventSubscriber\PasswordPolicyEventSubscriber
      arguments:
@@ -16,4 +23,4 @@ services:
        - {name: event_subscriber}
  password_policy.validator:
    class: Drupal\password_policy\PasswordPolicyValidator
    arguments: ['@entity_type.manager', '@plugin.manager.password_policy.password_constraint']
    arguments: ['@entity_type.manager', '@plugin.manager.password_policy.password_constraint', '@module_handler']
+19 −16
Original line number Diff line number Diff line
@@ -46,6 +46,7 @@ use Drupal\password_policy\PasswordPolicyInterface;
 *     "send_reset_email",
 *     "send_pending_email",
 *     "roles",
 *     "show_policy_table",
 *   }
 * )
 */
@@ -107,6 +108,13 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
   */
  protected $constraintsCollection;

  /**
   * Indicate whether the policy table should get displayed.
   *
   * @var bool
   */
  protected $show_policy_table = TRUE;

  /**
   * {@inheritdoc}
   */
@@ -122,20 +130,14 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
  }

  /**
   * Return the constraints from the policy.
   *
   * @return array
   *   The policies constraints.
   * {@inheritdoc}
   */
  public function getConstraints() {
    return $this->policy_constraints;
  }

  /**
   * Return a specific constraint from the policy.
   *
   * @return \Drupal\password_policy\PasswordConstraintInterface
   *   A specific constraint in the policy.
   * {@inheritdoc}
   */
  public function getConstraint($key) {
    if (!isset($this->policy_constraints[$key])) {
@@ -145,10 +147,7 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
  }

  /**
   * Return the password reset setting from the policy.
   *
   * @return int
   *   The number of days between password resets.
   * {@inheritdoc}
   */
  public function getPasswordReset() {
    return $this->password_reset;
@@ -189,10 +188,7 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
  }

  /**
   * Return the user roles for the policy.
   *
   * @return array
   *   The user roles assigned to the policy.
   * {@inheritdoc}
   */
  public function getRoles() {
    return $this->roles;
@@ -216,4 +212,11 @@ class PasswordPolicy extends ConfigEntityBase implements PasswordPolicyInterface
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function isPolicyTableShown() {
    return $this->show_policy_table;
  }

}
+7 −0
Original line number Diff line number Diff line
@@ -104,6 +104,13 @@ abstract class PasswordPolicyForm extends EntityForm {
      '#default_value' => implode(',', $this->entity->getPasswordPendingValue()),
    ];

    $form['show_policy_table'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Show policy table'),
      '#description' => $this->t('Indicate whether this policy should show the password policy table on the user add/edit form.'),
      '#default_value' => $this->entity->isPolicyTableShown(),
    ];

    return $form;
  }

Loading