/**
  * {@inheritdoc}
  */
 function validate($password, $user_context)
 {
     unset($user_context['uid']);
     $userData = array_values($user_context);
     $configuration = $this->getConfiguration();
     $validation = new PasswordPolicyValidation();
     $password_strength = new \Drupal\password_strength\PasswordStrength();
     $strength = $password_strength->passwordStrength($password, $userData);
     if ($strength['score'] < $configuration['strength_score']) {
         $validation->setErrorMessage($this->t('The password has a score of @password-score but the policy requires a score of at least @policy-score', array('@password-score' => $strength['score'], '@policy-score' => $configuration['strength_score'])));
     }
     return $validation;
 }
Ejemplo n.º 2
0
 /**
  * {@inheritdoc}
  */
 function validate($password, $user_context)
 {
     $configuration = $this->getConfiguration();
     $validation = new PasswordPolicyValidation();
     switch ($configuration['character_operation']) {
         case 'minimum':
             if (strlen($password) < $configuration['character_length']) {
                 $validation->setErrorMessage($this->t('The length of the password is !count characters and needs to be at least @length characters', ['!count' => strlen($password), '@length' => $configuration['character_length']]));
             }
             break;
         case 'maximum':
             if (strlen($password) > $configuration['character_length']) {
                 $validation->setErrorMessage($this->t('The length of the password is !count characters and can only be at most @length characters', ['!count' => strlen($password), '@length' => $configuration['character_length']]));
             }
             break;
     }
     return $validation;
 }
Ejemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 function validate($password, $user_context)
 {
     $configuration = $this->getConfiguration();
     $validation = new PasswordPolicyValidation();
     if (empty($user_context['uid'])) {
         return $validation;
     }
     $password_service = \Drupal::service('password');
     //query for users hashes
     $hashes = db_select('password_policy_history', 'pph')->fields('pph', array('pass_hash'))->condition('uid', $user_context['uid'])->execute()->fetchAll();
     $repeats = 0;
     foreach ($hashes as $hash) {
         if ($password_service->check($password, $hash->pass_hash)) {
             $repeats++;
         }
     }
     if ($repeats > $configuration['history_repeats']) {
         $validation->setErrorMessage($this->t('You cannot use the same password more than @history-repeats time(s) and this has been used @number-repeats time(s)', array('@history-repeats' => $configuration['history_repeats'] + 1, '@number-repeats' => $repeats)));
     }
     return $validation;
 }
 /**
  * {@inheritdoc}
  */
 function validate($password, $user_context)
 {
     $configuration = $this->getConfiguration();
     $validation = new PasswordPolicyValidation();
     $character_distribution = count_chars($password);
     $count_upper = 0;
     $count_lower = 0;
     $count_special = 0;
     $count_numeric = 0;
     foreach ($character_distribution as $i => $val) {
         if ($val) {
             $char = chr($i);
             if (is_numeric($char)) {
                 $count_numeric++;
             } else {
                 if (ctype_upper($char)) {
                     $count_upper++;
                 } else {
                     if (ctype_lower($char)) {
                         $count_lower++;
                     } else {
                         $count_special++;
                     }
                 }
             }
         }
     }
     switch ($configuration['character_type']) {
         case 'uppercase':
             if ($count_upper < $configuration['character_count']) {
                 $validation->setErrorMessage($this->t('The password only has @count uppercase characters and needs have at least @length characters', ['@count' => $count_upper, '@length' => $configuration['character_count']]));
             }
             break;
         case 'lowercase':
             if ($count_lower < $configuration['character_count']) {
                 $validation->setErrorMessage($this->t('The password only has @count lowercase characters and needs have at least @length characters', ['@count' => $count_lower, '@length' => $configuration['character_count']]));
             }
             break;
         case 'special':
             if ($count_special < $configuration['character_count']) {
                 $validation->setErrorMessage($this->t('The password only has @count special characters and needs have at least @length characters', ['@count' => $count_special, '@length' => $configuration['character_count']]));
             }
             break;
         case 'numeric':
             if ($count_numeric < $configuration['character_count']) {
                 $validation->setErrorMessage($this->t('The password only has @count numeric characters and needs have at least @length characters', ['@count' => $count_numeric, '@length' => $configuration['character_count']]));
             }
             break;
     }
     return $validation;
 }