Skip to content

Commit e742c57

Browse files
obuchmannBen Thomson
authored andcommitted
Force a configurable minimum password length. (#353)
Sets the minimum length of passwords, by default, to 8 characters and allows administrators to set the minimum length via a setting in the Backend. Credit to @obuchmann. Fixes #373.
1 parent c1679ba commit e742c57

7 files changed

Lines changed: 42 additions & 6 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ By default the User plugin will use the email address as the login name. To swit
145145

146146
We can add any other additional fields here too, such as `phone`, `company`, etc.
147147

148+
## Password length requirements
149+
150+
By default, the User plugin requires a minimum password length of 8 characters for all users when registering or changing their password. You can change this length requirement by going to backend and navigating to System > Users > User Settings. Inside the Registration tab, a **Minimum password length** field is provided, allowing you to increase or decrease this limit to your preferred length.
151+
148152
## Error handling
149153

150154
### Flash messages

components/ResetPassword.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function onResetPassword()
101101
{
102102
$rules = [
103103
'code' => 'required',
104-
'password' => 'required|between:4,255'
104+
'password' => 'required|between:' . UserModel::getMinPasswordLength() . ',255'
105105
];
106106

107107
$validation = Validator::make(post(), $rules);

lang/de/lang.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@
9898
'notifications_tab' => 'Benachrichtigungen',
9999
'allow_registration' => 'Benutzerregistrierung erlauben',
100100
'allow_registration_comment' => 'Falls dies deaktivert ist, können Benutzer nur von Administratoren erstellt werden.',
101+
'min_password_length' => 'Minimale Passwortlänge',
102+
'min_password_length_comment' => 'Die minimale Passwortlänge für Benutzerpasswörter.',
101103
'activate_mode' => 'Aktivierungsmodus',
102104
'activate_mode_comment' => 'Wählen Sie aus, wie ein Benutzer aktiviert werden soll.',
103105
'activate_mode_auto' => 'Automatisch',

lang/en/lang.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@
7474
'notifications_tab' => 'Notifications',
7575
'allow_registration' => 'Allow user registration',
7676
'allow_registration_comment' => 'If this is disabled users can only be created by administrators.',
77+
'min_password_length' => 'Minimum password length',
78+
'min_password_length_comment' => 'The minimum length of characters required for user passwords.',
7779
'activate_mode' => 'Activation mode',
7880
'activate_mode_comment' => 'Select how a user account should be activated.',
7981
'activate_mode_auto' => 'Automatic',

models/Settings.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
<?php namespace RainLab\User\Models;
22

3-
use Lang;
43
use Model;
5-
use RainLab\User\Models\User as UserModel;
64

75
class Settings extends Model
86
{
@@ -16,13 +14,16 @@ class Settings extends Model
1614
public $settingsCode = 'user_settings';
1715
public $settingsFields = 'fields.yaml';
1816

17+
1918
const ACTIVATE_AUTO = 'auto';
2019
const ACTIVATE_USER = 'user';
2120
const ACTIVATE_ADMIN = 'admin';
2221

2322
const LOGIN_EMAIL = 'email';
2423
const LOGIN_USERNAME = 'username';
2524

25+
const MIN_PASSWORD_LENGTH_DEFAULT = 8;
26+
2627
public function initSettingsData()
2728
{
2829
$this->require_activation = true;
@@ -31,6 +32,7 @@ public function initSettingsData()
3132
$this->block_persistence = false;
3233
$this->allow_registration = true;
3334
$this->login_attribute = self::LOGIN_EMAIL;
35+
$this->min_password_length = self::MIN_PASSWORD_LENGTH_DEFAULT;
3436
}
3537

3638
public function getActivateModeOptions()

models/User.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class User extends UserBase
2424
'email' => 'required|between:6,255|email|unique:users',
2525
'avatar' => 'nullable|image|max:4000',
2626
'username' => 'required|between:2,255|unique:users',
27-
'password' => 'required:create|between:4,255|confirmed',
28-
'password_confirmation' => 'required_with:password|between:4,255',
27+
'password' => 'required:create|between:' . UserSettings::MIN_PASSWORD_LENGTH_DEFAULT . ',255|confirmed',
28+
'password_confirmation' => 'required_with:password|between:' . UserSettings::MIN_PASSWORD_LENGTH_DEFAULT . ',255',
2929
];
3030

3131
/**
@@ -183,6 +183,15 @@ public function getLoginName()
183183
return static::$loginAttribute = UserSettings::get('login_attribute', UserSettings::LOGIN_EMAIL);
184184
}
185185

186+
/**
187+
* Returns the minimum length for a new password from settings.
188+
* @return int
189+
*/
190+
public static function getMinPasswordLength()
191+
{
192+
return (int) UserSettings::get('min_password_length', UserSettings::MIN_PASSWORD_LENGTH_DEFAULT);
193+
}
194+
186195
//
187196
// Scopes
188197
//
@@ -225,6 +234,14 @@ public function beforeValidate()
225234
) {
226235
$this->username = $this->email;
227236
}
237+
238+
239+
/*
240+
* Apply Password Length Settings
241+
*/
242+
$minPasswordLength = static::getMinPasswordLength();
243+
$this->rules['password'] = "required:create|between:$minPasswordLength,255|confirmed";
244+
$this->rules['password_confirmation'] = "required_with:password|between:$minPasswordLength,255";
228245
}
229246

230247
/**
@@ -418,6 +435,6 @@ protected function sendInvitation()
418435
*/
419436
protected function generatePassword()
420437
{
421-
$this->password = $this->password_confirmation = Str::random(6);
438+
$this->password = $this->password_confirmation = Str::random(static::getMinPasswordLength());
422439
}
423440
}

models/settings/fields.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ tabs:
3636
type: switch
3737
tab: rainlab.user::lang.settings.registration_tab
3838

39+
# Minimum password length
40+
min_password_length:
41+
span: left
42+
commentAbove: rainlab.user::lang.settings.min_password_length_comment
43+
label: rainlab.user::lang.settings.min_password_length
44+
type: number
45+
tab: rainlab.user::lang.settings.registration_tab
46+
min: 1
47+
3948
# Require Activation
4049
require_activation:
4150
span: left

0 commit comments

Comments
 (0)