Skip to content

Commit 1044eef

Browse files
avsdev-cwlcharette
authored andcommitted
Config settings for password min/max length (#993)
Created config settings for password min/max length and updated all references to use those values.
1 parent 1b2f828 commit 1044eef

11 files changed

Lines changed: 71 additions & 14 deletions

File tree

app/sprinkles/account/config/default.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,8 @@
122122
* Account Site Settings
123123
* ----------------------------------------------------------------------
124124
* "Site" settings that are automatically passed to Twig. Use theses
125-
* settings to control the login and registration process
125+
* settings to control the login, password (re)set and registration
126+
* processes
126127
*/
127128
'site' => [
128129
'login' => [
@@ -141,6 +142,12 @@
141142
],
142143
],
143144
],
145+
'password' => [
146+
'length' => [
147+
'min' => 8,
148+
'max' => 25,
149+
],
150+
],
144151
],
145152

146153
/*

app/sprinkles/account/locale/es_ES/messages.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
'PASSWORD' => [
100100
'@TRANSLATION' => 'Contraseña',
101101

102-
'BETWEEN' => 'Entre {{min}} - {{max}} (recomendado 12)',
102+
'BETWEEN' => 'Entre {{min}} - {{max}}',
103103

104104
'CONFIRM' => 'Confirmar contraseña',
105105
'CONFIRM_CURRENT' => 'Por favor, confirma tu contraseña actual',

app/sprinkles/account/src/Bakery/CreateAdminUser.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,11 @@ protected function validateLastName($lastName)
298298
*/
299299
protected function askPassword($password = '', $requireConfirmation = true)
300300
{
301+
$passwordMin = $this->ci->config['site.password.length.min'];
302+
$passwordMax = $this->ci->config['site.password.length.max'];
303+
301304
while (!isset($password) || !$this->validatePassword($password) || !$this->confirmPassword($password, $requireConfirmation)) {
302-
$password = $this->io->askHidden('Enter password (12-255 characters)');
305+
$password = $this->io->askHidden('Enter password (' . $passwordMin . '-' . $passwordMax . ' characters)');
303306
}
304307

305308
return $password;
@@ -314,9 +317,11 @@ protected function askPassword($password = '', $requireConfirmation = true)
314317
*/
315318
protected function validatePassword($password)
316319
{
317-
//TODO Config for this ??
318-
if (strlen($password) < 12 || strlen($password) > 255) {
319-
$this->io->error('Password must be between 12-255 characters');
320+
$passwordMin = $this->ci->config['site.password.length.min'];
321+
$passwordMax = $this->ci->config['site.password.length.max'];
322+
323+
if (strlen($password) < $passwordMin || strlen($password) > $passwordMax) {
324+
$this->io->error('Password must be between ' . $passwordMin . ' and ' . $passwordMax . ' characters');
320325

321326
return false;
322327
}

app/sprinkles/account/src/Controller/AccountController.php

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,10 @@ public function pageRegister(Request $request, Response $response, $args)
519519

520520
// Load validation rules
521521
$schema = new RequestSchema('schema://requests/register.yaml');
522+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
523+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
524+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
525+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
522526
$validatorRegister = new JqueryValidationAdapter($schema, $this->ci->translator);
523527

524528
// Get locale information
@@ -593,11 +597,18 @@ public function pageResendVerification(Request $request, Response $response, $ar
593597
*/
594598
public function pageResetPassword(Request $request, Response $response, $args)
595599
{
600+
/** @var \UserFrosting\Support\Repository\Repository $config */
601+
$config = $this->ci->config;
602+
596603
// Insert the user's secret token from the link into the password reset form
597604
$params = $request->getQueryParams();
598605

599606
// Load validation rules - note this uses the same schema as "set password"
600607
$schema = new RequestSchema('schema://requests/set-password.yaml');
608+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
609+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
610+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
611+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
601612
$validator = new JqueryValidationAdapter($schema, $this->ci->translator);
602613

603614
return $this->ci->view->render($response, 'pages/reset-password.html.twig', [
@@ -627,11 +638,18 @@ public function pageResetPassword(Request $request, Response $response, $args)
627638
*/
628639
public function pageSetPassword(Request $request, Response $response, $args)
629640
{
641+
/** @var \UserFrosting\Support\Repository\Repository $config */
642+
$config = $this->ci->config;
643+
630644
// Insert the user's secret token from the link into the password set form
631645
$params = $request->getQueryParams();
632646

633647
// Load validation rules
634648
$schema = new RequestSchema('schema://requests/set-password.yaml');
649+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
650+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
651+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
652+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
635653
$validator = new JqueryValidationAdapter($schema, $this->ci->translator);
636654

637655
return $this->ci->view->render($response, 'pages/set-password.html.twig', [
@@ -664,6 +682,9 @@ public function pageSetPassword(Request $request, Response $response, $args)
664682
*/
665683
public function pageSettings(Request $request, Response $response, $args)
666684
{
685+
/** @var \UserFrosting\Support\Repository\Repository $config */
686+
$config = $this->ci->config;
687+
667688
/** @var \UserFrosting\Sprinkle\Account\Authorize\AuthorizationManager */
668689
$authorizer = $this->ci->authorizer;
669690

@@ -677,14 +698,15 @@ public function pageSettings(Request $request, Response $response, $args)
677698

678699
// Load validation rules
679700
$schema = new RequestSchema('schema://requests/account-settings.yaml');
701+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
702+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
703+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
704+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
680705
$validatorAccountSettings = new JqueryValidationAdapter($schema, $this->ci->translator);
681706

682707
$schema = new RequestSchema('schema://requests/profile-settings.yaml');
683708
$validatorProfileSettings = new JqueryValidationAdapter($schema, $this->ci->translator);
684709

685-
/** @var \UserFrosting\Support\Repository\Repository $config */
686-
$config = $this->ci->config;
687-
688710
// Get a list of all locales
689711
$locales = $config->getDefined('site.locales.available');
690712

@@ -919,6 +941,10 @@ public function register(Request $request, Response $response, $args)
919941

920942
// Load the request schema
921943
$schema = new RequestSchema('schema://requests/register.yaml');
944+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
945+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
946+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
947+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
922948

923949
// Whitelist and set parameter defaults
924950
$transformer = new RequestDataTransformer($schema);
@@ -1117,6 +1143,10 @@ public function setPassword(Request $request, Response $response, $args)
11171143

11181144
// Load the request schema
11191145
$schema = new RequestSchema('schema://requests/set-password.yaml');
1146+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
1147+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
1148+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
1149+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
11201150

11211151
// Whitelist and set parameter defaults
11221152
$transformer = new RequestDataTransformer($schema);
@@ -1210,6 +1240,10 @@ public function settings(Request $request, Response $response, $args)
12101240

12111241
// Load the request schema
12121242
$schema = new RequestSchema('schema://requests/account-settings.yaml');
1243+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
1244+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
1245+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
1246+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
12131247

12141248
// Whitelist and set parameter defaults
12151249
$transformer = new RequestDataTransformer($schema);

app/sprinkles/account/templates/forms/settings-account.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
{% if page.visibility != "disabled" %}
1717
<div class="form-group">
1818
<label for="input-password" class="control-label">{{translate("PASSWORD.NEW")}}</label>
19-
<input type="password" id="input-password" class="form-control" name="password" placeholder="{{translate("PASSWORD.BETWEEN", {min: 12, max: 100})}} ({{translate("OPTIONAL")}})">
19+
<input type="password" id="input-password" class="form-control" name="password" placeholder="{{translate("PASSWORD.BETWEEN", {min: site.password.length.min, max: site.password.length.max})}} ({{translate("OPTIONAL")}})">
2020
</div>
2121
<div class="form-group">
2222
<label for="input-passwordc" class="control-label">{{translate("PASSWORD.CONFIRM_NEW")}}</label>

app/sprinkles/account/templates/pages/register.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</div>
4848
<div class="form-group">
4949
<label for="r-form-password">{{translate('PASSWORD')}}</label>
50-
<input type="password" name="password" placeholder="{{translate('PASSWORD.BETWEEN', {min: 12, max: 100})}}" class="form-control" id="r-form-password">
50+
<input type="password" name="password" placeholder="{{translate('PASSWORD.BETWEEN', {min: site.password.length.min, max: site.password.length.max})}}" class="form-control" id="r-form-password">
5151
</div>
5252
<div class="form-group">
5353
<label class="sr-only" for="r-form-passwordc">{{translate('PASSWORD.CONFIRM')}}</label>

app/sprinkles/account/templates/pages/reset-password.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<div class="form-group">
3232
<label class="sr-only" for="form-password">{{translate("PASSWORD.NEW")}}</label>
33-
<input type="password" name="password" placeholder="{{translate("PASSWORD.BETWEEN", {min: 12, max: 100})}}" class="form-control" id="form-password">
33+
<input type="password" name="password" placeholder="{{translate("PASSWORD.BETWEEN", {min: site.password.length.min, max: site.password.length.max})}}" class="form-control" id="form-password">
3434
</div>
3535

3636
<div class="form-group">

app/sprinkles/account/templates/pages/set-password.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<div class="form-group">
3232
<label class="sr-only" for="form-password">{{translate('PASSWORD')}}</label>
33-
<input type="password" name="password" placeholder="{{translate('PASSWORD.BETWEEN', {min: 12, max: 100})}}" class="form-control" id="form-password">
33+
<input type="password" name="password" placeholder="{{translate('PASSWORD.BETWEEN', {min: site.password.length.min, max: site.password.length.max})}}" class="form-control" id="form-password">
3434
</div>
3535
<div class="form-group">
3636
<label class="sr-only" for="form-passwordc">{{translate('PASSWORD.CONFIRM')}}</label>

app/sprinkles/admin/src/Controller/RoleController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,8 @@ public function updateField(Request $request, Response $response, $args)
957957

958958
// Load the request schema
959959
$schema = new RequestSchema('schema://requests/role/edit-field.yaml');
960+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
961+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
960962

961963
// Whitelist and set parameter defaults
962964
$transformer = new RequestDataTransformer($schema);

app/sprinkles/admin/src/Controller/UserController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,9 @@ public function getModalEditPassword(Request $request, Response $response, $args
765765
/** @var \UserFrosting\Sprinkle\Account\Database\Models\Interfaces\UserInterface $currentUser */
766766
$currentUser = $this->ci->currentUser;
767767

768+
/** @var \UserFrosting\Support\Repository\Repository $config */
769+
$config = $this->ci->config;
770+
768771
// Access-controlled resource - check that currentUser has permission to edit "password" field for this user
769772
if (!$authorizer->checkAccess($currentUser, 'update_user_field', [
770773
'user' => $user,
@@ -775,6 +778,10 @@ public function getModalEditPassword(Request $request, Response $response, $args
775778

776779
// Load validation rules
777780
$schema = new RequestSchema('schema://requests/user/edit-password.yaml');
781+
$schema->set('value.validators.length.min', $config['site.password.length.min']);
782+
$schema->set('value.validators.length.max', $config['site.password.length.max']);
783+
$schema->set('passwordc.validators.length.min', $config['site.password.length.min']);
784+
$schema->set('passwordc.validators.length.max', $config['site.password.length.max']);
778785
$validator = new JqueryValidationAdapter($schema, $this->ci->translator);
779786

780787
return $this->ci->view->render($response, 'modals/user-set-password.html.twig', [
@@ -1307,6 +1314,8 @@ public function updateField(Request $request, Response $response, $args)
13071314

13081315
// Load the request schema
13091316
$schema = new RequestSchema('schema://requests/user/edit-field.yaml');
1317+
$schema->set('password.validators.length.min', $config['site.password.length.min']);
1318+
$schema->set('password.validators.length.max', $config['site.password.length.max']);
13101319

13111320
// Whitelist and set parameter defaults
13121321
$transformer = new RequestDataTransformer($schema);

0 commit comments

Comments
 (0)