71304: Implement Forgot password components

This commit is contained in:
Yana De Pauw
2020-06-15 11:59:25 +02:00
parent 035a4b6e16
commit 6f9f4ec968
37 changed files with 1087 additions and 443 deletions

View File

@@ -2,7 +2,6 @@ import { Component, OnInit, ViewChild } from '@angular/core';
import { Observable } from 'rxjs/internal/Observable';
import { EPerson } from '../core/eperson/models/eperson.model';
import { ProfilePageMetadataFormComponent } from './profile-page-metadata-form/profile-page-metadata-form.component';
import { ProfilePageSecurityFormComponent } from './profile-page-security-form/profile-page-security-form.component';
import { NotificationsService } from '../shared/notifications/notifications.service';
import { TranslateService } from '@ngx-translate/core';
import { Group } from '../core/eperson/models/group.model';
@@ -11,9 +10,10 @@ import { PaginatedList } from '../core/data/paginated-list';
import { filter, switchMap, tap } from 'rxjs/operators';
import { EPersonDataService } from '../core/eperson/eperson-data.service';
import { getAllSucceededRemoteData, getRemoteDataPayload } from '../core/shared/operators';
import { hasValue } from '../shared/empty.util';
import { hasValue, isNotEmpty } from '../shared/empty.util';
import { followLink } from '../shared/utils/follow-link-config.model';
import { AuthService } from '../core/auth/auth.service';
import { ErrorResponse, RestResponse } from '../core/cache/response.models';
@Component({
selector: 'ds-profile-page',
@@ -26,15 +26,10 @@ export class ProfilePageComponent implements OnInit {
/**
* A reference to the metadata form component
*/
@ViewChild(ProfilePageMetadataFormComponent, { static: false }) metadataForm: ProfilePageMetadataFormComponent;
@ViewChild(ProfilePageMetadataFormComponent, {static: false}) metadataForm: ProfilePageMetadataFormComponent;
/**
* A reference to the security form component
*/
@ViewChild(ProfilePageSecurityFormComponent, { static: false }) securityForm: ProfilePageSecurityFormComponent;
/**
* The authenticated user
* The authenticated user as observable
*/
user$: Observable<EPerson>;
@@ -48,6 +43,26 @@ export class ProfilePageComponent implements OnInit {
*/
NOTIFICATIONS_PREFIX = 'profile.notifications.';
/**
* Prefix for the notification messages of this security form
*/
PASSWORD_NOTIFICATIONS_PREFIX = 'profile.security.form.notifications.';
/**
* The validity of the password filled in, in the security form
*/
private invalidSecurity: boolean;
/**
* The password filled in, in the security form
*/
private password: string;
/**
* The authenticated user
*/
private currentUser: EPerson;
constructor(private authService: AuthService,
private notificationsService: NotificationsService,
private translate: TranslateService,
@@ -59,7 +74,8 @@ export class ProfilePageComponent implements OnInit {
filter((user: EPerson) => hasValue(user.id)),
switchMap((user: EPerson) => this.epersonService.findById(user.id, followLink('groups'))),
getAllSucceededRemoteData(),
getRemoteDataPayload()
getRemoteDataPayload(),
tap((user: EPerson) => this.currentUser = user)
);
this.groupsRD$ = this.user$.pipe(switchMap((user: EPerson) => user.groups));
}
@@ -70,7 +86,7 @@ export class ProfilePageComponent implements OnInit {
*/
updateProfile() {
const metadataChanged = this.metadataForm.updateProfile();
const securityChanged = this.securityForm.updateSecurity();
const securityChanged = this.updateSecurity();
if (!metadataChanged && !securityChanged) {
this.notificationsService.warning(
this.translate.instant(this.NOTIFICATIONS_PREFIX + 'warning.no-changes.title'),
@@ -78,4 +94,61 @@ export class ProfilePageComponent implements OnInit {
);
}
}
/**
* Sets the validity of the password based on an emitted of the form
* @param $event
*/
setInvalid($event: boolean) {
this.invalidSecurity = $event;
}
/**
* Update the user's security details
*
* Sends a patch request for changing the user's password when a new password is present and the password confirmation
* matches the new password.
* Nothing happens when no passwords are filled in.
* An error notification is displayed when the password confirmation does not match the new password.
*
* Returns false when the password was empty
*/
updateSecurity() {
const passEntered = isNotEmpty(this.password);
if (this.invalidSecurity) {
this.notificationsService.error(this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'error.general'));
}
if (!this.invalidSecurity && passEntered) {
const operation = Object.assign({op: 'replace', path: '/password', value: this.password});
this.epersonService.patch(this.currentUser, [operation]).subscribe((response: RestResponse) => {
if (response.isSuccessful) {
this.notificationsService.success(
this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'success.title'),
this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'success.content')
);
} else {
this.notificationsService.error(
this.translate.instant(this.PASSWORD_NOTIFICATIONS_PREFIX + 'error.title'), (response as ErrorResponse).errorMessage
);
}
});
}
return passEntered;
}
/**
* Set the password value based on the value emitted from the security form
* @param $event
*/
setPasswordValue($event: string) {
this.password = $event;
}
/**
* Submit of the security form that triggers the updateProfile method
*/
submit() {
this.updateProfile();
}
}