Fixed password generation and added unit tests

This commit is contained in:
MatteoF96
2022-01-11 13:01:34 +01:00
parent 43fd2fdc34
commit 0ca0241cfc
2 changed files with 175 additions and 2 deletions

View File

@ -11,6 +11,7 @@
namespace Da\User\Helper;
use Yii;
use yii\base\Exception;
use yii\base\Security;
use yii\base\InvalidConfigException;
@ -61,17 +62,29 @@ class SecurityHelper
return $this->security->validatePassword($password, $hash);
}
public function generatePassword($length, $minPasswordRequirements)
public function generatePassword($length, $minPasswordRequirements = null)
{
$sets = [
'lower' => 'abcdefghjkmnpqrstuvwxyz',
'upper' => 'ABCDEFGHJKMNPQRSTUVWXYZ',
'digit' => '123456789',
'special' => '!#$%&()*+,-./:;<=>?@[\]^_{|}~'
'special' => '!#$%&*+,-.:;<=>?@_~'
];
$all = '';
$password = '';
if (!isset($minPasswordRequirements)) {
if (isset(Yii::$app->getModule('user')->minPasswordRequirements)) {
$minPasswordRequirements = Yii::$app->getModule('user')->minPasswordRequirements;
}
else {
$minPasswordRequirements = [
'lower' => 1,
'digit' => 1,
'upper' => 1,
];
}
}
if (isset($minPasswordRequirements['min']) && $length < $minPasswordRequirements['min']) {
$length = $minPasswordRequirements['min'];
}