Skip to content

Commit f1d1f94

Browse files
authored
Merge pull request #138 from Stunc0/adding-password-regexp-validation
Adding passwords regexp validation.
2 parents f9d45f4 + c709f7f commit f1d1f94

File tree

5 files changed

+36
-2
lines changed

5 files changed

+36
-2
lines changed

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ Configuration
5555

5656
// 'enableRegistration' => true,
5757

58+
// Add regexp validation to passwords. Default pattern does not restrict user and can enter any set of characters.
59+
// The example below allows user to enter :
60+
// any set of characters
61+
// (?=\S{8,}): of at least length 8
62+
// (?=\S*[a-z]): containing at least one lowercase letter
63+
// (?=\S*[A-Z]): and at least one uppercase letter
64+
// (?=\S*[\d]): and at least one number
65+
// $: anchored to the end of the string
66+
67+
//'passwordRegexp' => '^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$',
68+
69+
5870
// Here you can set your handler to change layout for any controller or action
5971
// Tip: you can use this event in any module
6072
'on beforeAction'=>function(yii\base\ActionEvent $event) {
@@ -254,5 +266,3 @@ FAQ
254266
4) Define your own UserManagementModule::$registrationFormClass. In this class you can do whatever you want like validating custom forms and saving profiles
255267

256268
5) Create your controller where user can view profiles
257-
258-

UserManagementModule.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ class UserManagementModule extends \yii\base\Module
112112
*/
113113
public $registrationBlackRegexp = '/^(.)*admin(.)*$/i';
114114

115+
/**
116+
* Pattern that will be applied for password.
117+
* Default pattern does not restrict user and can enter any set of characters.
118+
*
119+
* example of pattern :
120+
* '^\S*(?=\S{8,})(?=\S*[a-z])(?=\S*[A-Z])(?=\S*[\d])\S*$'
121+
*
122+
* This example pattern allow user enter only:
123+
*
124+
* ^: anchored to beginning of string
125+
* \S*: any set of characters
126+
* (?=\S{8,}): of at least length 8
127+
* (?=\S*[a-z]): containing at least one lowercase letter
128+
* (?=\S*[A-Z]): and at least one uppercase letter
129+
* (?=\S*[\d]): and at least one number
130+
* $: anchored to the end of the string
131+
*
132+
* @var string
133+
*/
134+
public $passwordRegexp = '/^(.*)+$/';
135+
115136
/**
116137
* Affects only web interface in "/user-management/user-permission/set" route. Tt means you still can assign
117138
* multiple roles (for example via migrations) even if this attribute is "false"

models/User.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ public function rules()
264264
['password', 'required', 'on'=>['newUser', 'changePassword']],
265265
['password', 'string', 'max' => 255, 'on'=>['newUser', 'changePassword']],
266266
['password', 'trim', 'on'=>['newUser', 'changePassword']],
267+
['password', 'match', 'pattern' => Yii::$app->getModule('user-management')->passwordRegexp],
267268

268269
['repeat_password', 'required', 'on'=>['newUser', 'changePassword']],
269270
['repeat_password', 'compare', 'compareAttribute'=>'password'],

models/forms/ChangeOwnPasswordForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public function rules()
3535
[['password', 'repeat_password'], 'required'],
3636
[['password', 'repeat_password', 'current_password'], 'string', 'max'=>255],
3737
[['password', 'repeat_password', 'current_password'], 'trim'],
38+
['password', 'match', 'pattern' => Yii::$app->getModule('user-management')->passwordRegexp],
3839

3940
['repeat_password', 'compare', 'compareAttribute'=>'password'],
4041

models/forms/RegistrationForm.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function rules()
3333
['username', 'purgeXSS'],
3434

3535
['password', 'string', 'max' => 255],
36+
['password', 'match', 'pattern' => Yii::$app->getModule('user-management')->passwordRegexp],
3637

3738
['repeat_password', 'compare', 'compareAttribute'=>'password'],
3839
];

0 commit comments

Comments
 (0)