From a8636b0e5c99e4e956032f9560fbcfc81bfdb4ee Mon Sep 17 00:00:00 2001 From: Kristof De Langhe Date: Wed, 18 Mar 2020 11:43:56 +0100 Subject: [PATCH] 69432: Fix being able to send invalid password form + 6 character password validator --- resources/i18n/en.json5 | 4 ++++ .../profile-page-security-form.component.html | 1 + ...ofile-page-security-form.component.spec.ts | 8 +++---- .../profile-page-security-form.component.ts | 23 +++++++++++++++---- 4 files changed, 28 insertions(+), 8 deletions(-) diff --git a/resources/i18n/en.json5 b/resources/i18n/en.json5 index decad7309b..a1610c9fd9 100644 --- a/resources/i18n/en.json5 +++ b/resources/i18n/en.json5 @@ -1449,6 +1449,8 @@ "profile.security.form.error.matching-passwords": "The passwords do not match.", + "profile.security.form.error.password-length": "The password should be at least 6 characters long.", + "profile.security.form.info": "Optionally, you can enter a new password in the box below, and confirm it by typing it again into the second box. It should be at least six characters long.", "profile.security.form.label.password": "Password", @@ -1461,6 +1463,8 @@ "profile.security.form.notifications.error.title": "Error changing passwords", + "profile.security.form.notifications.error.not-long-enough": "The password has to be at least 6 characters long.", + "profile.security.form.notifications.error.not-same": "The provided passwords are not the same.", "profile.title": "Update Profile", diff --git a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.html b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.html index 81519e5a42..50a081c6f2 100644 --- a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.html +++ b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.html @@ -5,4 +5,5 @@ [formGroup]="formGroup" [displaySubmit]="false"> +
{{'profile.security.form.error.password-length' | translate}}
{{'profile.security.form.error.matching-passwords' | translate}}
diff --git a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.spec.ts b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.spec.ts index 2cb687d34d..324230ce9f 100644 --- a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.spec.ts +++ b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.spec.ts @@ -80,14 +80,14 @@ describe('ProfilePageSecurityFormComponent', () => { }); }); - describe('when both password fields are filled in and equal', () => { + describe('when both password fields are filled in, long enough and equal', () => { let result; let operations; beforeEach(() => { - setModelValue('password', 'test'); - setModelValue('passwordrepeat', 'test'); - operations = [{ op: 'replace', path: '/password', value: 'test' }]; + setModelValue('password', 'testest'); + setModelValue('passwordrepeat', 'testest'); + operations = [{ op: 'replace', path: '/password', value: 'testest' }]; result = component.updateSecurity(); }); diff --git a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts index f964b02280..b8ac07e6d8 100644 --- a/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts +++ b/src/app/profile-page/profile-page-security-form/profile-page-security-form.component.ts @@ -64,7 +64,7 @@ export class ProfilePageSecurityFormComponent implements OnInit { } ngOnInit(): void { - this.formGroup = this.formService.createFormGroup(this.formModel, { validators: this.checkPasswords }); + this.formGroup = this.formService.createFormGroup(this.formModel, { validators: [this.checkPasswordsEqual, this.checkPasswordLength] }); this.updateFieldTranslations(); this.translate.onLangChange .subscribe(() => { @@ -87,11 +87,21 @@ export class ProfilePageSecurityFormComponent implements OnInit { * Check if both password fields are filled in and equal * @param group The FormGroup to validate */ - checkPasswords(group: FormGroup) { + checkPasswordsEqual(group: FormGroup) { const pass = group.get('password').value; const repeatPass = group.get('passwordrepeat').value; - return isEmpty(repeatPass) || pass === repeatPass ? null : { notSame: true }; + return pass === repeatPass ? null : { notSame: true }; + } + + /** + * Check if the password is at least 6 characters long + * @param group The FormGroup to validate + */ + checkPasswordLength(group: FormGroup) { + const pass = group.get('password').value; + + return isEmpty(pass) || pass.length >= 6 ? null : { notLongEnough: true }; } /** @@ -109,7 +119,12 @@ export class ProfilePageSecurityFormComponent implements OnInit { const passEntered = isNotEmpty(pass); if (!this.formGroup.valid) { if (passEntered) { - this.notificationsService.error(this.translate.instant(this.NOTIFICATIONS_PREFIX + 'error.not-same')); + if (this.checkPasswordsEqual(this.formGroup) != null) { + this.notificationsService.error(this.translate.instant(this.NOTIFICATIONS_PREFIX + 'error.not-same')); + } + if (this.checkPasswordLength(this.formGroup) != null) { + this.notificationsService.error(this.translate.instant(this.NOTIFICATIONS_PREFIX + 'error.not-long-enough')); + } return true; } return false;