CST-6153 changes for current password field introduce

This commit is contained in:
nikunj59
2022-06-24 19:26:30 +05:30
parent f589b2f8de
commit 9e2a682a51
6 changed files with 56 additions and 12 deletions

View File

@@ -74,6 +74,19 @@ describe('ProfilePageSecurityFormComponent', () => {
expect(component.passwordValue.emit).toHaveBeenCalledWith('new-password'); expect(component.passwordValue.emit).toHaveBeenCalledWith('new-password');
})); }));
it('should emit the value on password change with current password for profile-page', fakeAsync(() => {
spyOn(component.passwordValue, 'emit');
spyOn(component.currentPasswordValue, 'emit');
component.FORM_PREFIX = 'profile.security.form.';
component.ngOnInit();
component.formGroup.patchValue({password: 'new-password'});
component.formGroup.patchValue({'current-password': 'current-password'});
tick(300);
expect(component.passwordValue.emit).toHaveBeenCalledWith('new-password');
expect(component.currentPasswordValue.emit).toHaveBeenCalledWith('current-password');
}))
}); });
}); });
}); });

View File

@@ -27,6 +27,10 @@ export class ProfilePageSecurityFormComponent implements OnInit {
* Emits the value of the password * Emits the value of the password
*/ */
@Output() passwordValue = new EventEmitter<string>(); @Output() passwordValue = new EventEmitter<string>();
/**
* Emits the value of the current-password
*/
@Output() currentPasswordValue = new EventEmitter<string>();
/** /**
* The form's input models * The form's input models
@@ -69,6 +73,14 @@ export class ProfilePageSecurityFormComponent implements OnInit {
} }
ngOnInit(): void { ngOnInit(): void {
if (this.FORM_PREFIX === 'profile.security.form.') {
this.formModel.unshift(new DynamicInputModel({
id: 'current-password',
name: 'current-password',
inputType: 'password',
required: true
}));
}
if (this.passwordCanBeEmpty) { if (this.passwordCanBeEmpty) {
this.formGroup = this.formService.createFormGroup(this.formModel, this.formGroup = this.formService.createFormGroup(this.formModel,
{validators: [this.checkPasswordsEqual, this.checkPasswordLength]}); {validators: [this.checkPasswordsEqual, this.checkPasswordLength]});
@@ -85,11 +97,7 @@ export class ProfilePageSecurityFormComponent implements OnInit {
this.subs.push(this.formGroup.statusChanges.pipe( this.subs.push(this.formGroup.statusChanges.pipe(
debounceTime(300), debounceTime(300),
map((status: string) => { map((status: string) => {
if (status !== 'VALID') { return status !== 'VALID';
return true;
} else {
return false;
}
})).subscribe((status) => this.isInvalid.emit(status)) })).subscribe((status) => this.isInvalid.emit(status))
); );
@@ -97,6 +105,9 @@ export class ProfilePageSecurityFormComponent implements OnInit {
debounceTime(300), debounceTime(300),
).subscribe((valueChange) => { ).subscribe((valueChange) => {
this.passwordValue.emit(valueChange.password); this.passwordValue.emit(valueChange.password);
if (this.FORM_PREFIX === 'profile.security.form.') {
this.currentPasswordValue.emit(valueChange['current-password']);
}
})); }));
} }

View File

@@ -24,6 +24,7 @@
[FORM_PREFIX]="'profile.security.form.'" [FORM_PREFIX]="'profile.security.form.'"
(isInvalid)="setInvalid($event)" (isInvalid)="setInvalid($event)"
(passwordValue)="setPasswordValue($event)" (passwordValue)="setPasswordValue($event)"
(currentPasswordValue)="setCurrentPasswordValue($event)"
></ds-profile-page-security-form> ></ds-profile-page-security-form>
</div> </div>
</div> </div>

View File

@@ -180,7 +180,7 @@ describe('ProfilePageComponent', () => {
beforeEach(() => { beforeEach(() => {
component.setPasswordValue(''); component.setPasswordValue('');
component.setCurrentPasswordValue('current-password');
result = component.updateSecurity(); result = component.updateSecurity();
}); });
@@ -199,6 +199,7 @@ describe('ProfilePageComponent', () => {
beforeEach(() => { beforeEach(() => {
component.setPasswordValue('test'); component.setPasswordValue('test');
component.setInvalid(true); component.setInvalid(true);
component.setCurrentPasswordValue('current-password');
result = component.updateSecurity(); result = component.updateSecurity();
}); });
@@ -215,8 +216,12 @@ describe('ProfilePageComponent', () => {
beforeEach(() => { beforeEach(() => {
component.setPasswordValue('testest'); component.setPasswordValue('testest');
component.setInvalid(false); component.setInvalid(false);
component.setCurrentPasswordValue('current-password');
operations = [{ op: 'add', path: '/password', value: 'testest' }]; operations = [
{ op: 'add', path: '/password', value: 'testest' },
{ op: 'add', path: '/challenge', value: 'current-password' }
];
result = component.updateSecurity(); result = component.updateSecurity();
}); });

View File

@@ -67,6 +67,10 @@ export class ProfilePageComponent implements OnInit {
* The password filled in, in the security form * The password filled in, in the security form
*/ */
private password: string; private password: string;
/**
* The current-password filled in, in the security form
*/
private currentPassword: string;
/** /**
* The authenticated user * The authenticated user
@@ -138,15 +142,15 @@ export class ProfilePageComponent implements OnInit {
*/ */
updateSecurity() { updateSecurity() {
const passEntered = isNotEmpty(this.password); const passEntered = isNotEmpty(this.password);
if (this.invalidSecurity) { if (this.invalidSecurity) {
this.notificationsService.error(this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'error.general')); this.notificationsService.error(this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'error.general'));
} }
if (!this.invalidSecurity && passEntered) { if (!this.invalidSecurity && passEntered) {
const operation = {op: 'add', path: '/password', value: this.password} as Operation; const operations = [
this.epersonService.patch(this.currentUser, [operation]).pipe( { op: 'add', path: '/password', value: this.password },
getFirstCompletedRemoteData() { op: 'add', path: '/challenge', value: this.currentPassword }
).subscribe((response: RemoteData<EPerson>) => { ] as Operation[];
this.epersonService.patch(this.currentUser, operations).pipe(getFirstCompletedRemoteData()).subscribe((response: RemoteData<EPerson>) => {
if (response.hasSucceeded) { if (response.hasSucceeded) {
this.notificationsService.success( this.notificationsService.success(
this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'success.title'), this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'success.title'),
@@ -170,6 +174,14 @@ export class ProfilePageComponent implements OnInit {
this.password = $event; this.password = $event;
} }
/**
* Set the current-password value based on the value emitted from the security form
* @param $event
*/
setCurrentPasswordValue($event: string) {
this.currentPassword = $event;
}
/** /**
* Submit of the security form that triggers the updateProfile method * Submit of the security form that triggers the updateProfile method
*/ */

View File

@@ -3046,6 +3046,8 @@
"profile.security.form.label.passwordrepeat": "Retype to confirm", "profile.security.form.label.passwordrepeat": "Retype to confirm",
"profile.security.form.label.current-password": "Current password",
"profile.security.form.notifications.success.content": "Your changes to the password were saved.", "profile.security.form.notifications.success.content": "Your changes to the password were saved.",
"profile.security.form.notifications.success.title": "Password saved", "profile.security.form.notifications.success.title": "Password saved",