when using admin/people/create the password policy isn't enforced.

It is enforced for user/$/edit to edit the password, however.

Comments

windmaomao’s picture

i tried to add a user, seems it's not currently enforced.

pbz1912’s picture

I'm working on a patch.

Sponsored by British Council.

pbz1912’s picture

I've run out of time to work on this. Sorry.

At first I thought it would be simple to implement with password_policy_form_user_register_form_alter()

However due to the way the validation is done for the user/$/edit page
$account = $form['#user'];
It uses the user info that was there before the form was rendered. This could also cause issues if you were to have different password policies for different roles and change the role(s) at the same time as changing the password.

Then there is a lot of chasing around to work out whats actually using something from that array $account, which after the best part of a day I still hadn't completely got through it.

This is what I added to password_policy.module it obviously doesn't work but thought I'd put it here to help with the fixing of this.

function password_policy_form_user_register_form_alter(&$form, $form_state) {
  password_policy_password_element_alter($form['account']['pass'], $form['#user']);
  $form['#validate'][] = 'password_policy_user_register_form_validate';
  $form['#submit'][] = 'password_policy_password_submit';
}

function password_policy_user_register_form_validate($form, &$form_state) {
  if (!empty($form_state['values']['pass'])) {
    $account = array(
        'roles' => $form_state['values']['roles'],
        'name'  => $form_state['values']['name']
    ); // This is a pain, what can I pass through here?
    $policies = PasswordPolicy::matched_policies($account);
    $errors = array();
    foreach ($policies as $policy) {
      $errors = $errors + $policy->constraint($form_state['values']['pass'], $account);
    }
    if (!empty($errors)) {
      form_set_error('pass', theme('item_list', array('items' => $errors)));
    }
  }
}
Delphine Lepers’s picture

The problem also occurs for the edit form if you add/remove a role to the user, the related policy is not enforced at save, but only the next time the password is changed.

IMO the policy should be checked for both cases NOT on the existing roles but on the checked roles (we expect new level(s) of security for a given new role(s) level(s)).
It is less of an issue on the edit form because users rarely modify their own role, but same logic should apply.
I will work on a patch.

coltrane’s picture

Issue summary: View changes
Status: Active » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

artatum’s picture

Hi
Currently on 7.x-1.9 and Drupal 7.34, when creating a new user, the pwd policy is not enforced at all. The module doesnt do what it's supposed to do at this time. It's supposed to be fixed one year ago, but not for me. :/
It's only the second time you edit the user password that the rules are enforced.
Any idea welcome.

aohrvetpv’s picture

artatum, when a policy applies to the "authenticated user" role it will also apply for user registration. This is because a newly created user has the "authenticated user" role. Check that your policy is enabled and the "authenticated user" role is selected.

coderider’s picture

agree with #8
this solve the issue Password policy not working on user create page