From eed99b2119b4658bf4ecdcbbed635abd6d07658a Mon Sep 17 00:00:00 2001 From: Art Date: Fri, 8 Oct 2021 14:10:05 +0300 Subject: [PATCH] 31.4. Settings Component Base Implementation (#31) --- .../settings/settings.component.html | 21 ++++++++- .../management/settings/settings.component.ts | 47 +++++++++++++++++-- .../src/app/service/authentication.service.ts | 15 ++++-- 3 files changed, 76 insertions(+), 7 deletions(-) diff --git a/support-portal-frontend/src/app/component/management/settings/settings.component.html b/support-portal-frontend/src/app/component/management/settings/settings.component.html index 4ab2a41..7677ccb 100644 --- a/support-portal-frontend/src/app/component/management/settings/settings.component.html +++ b/support-portal-frontend/src/app/component/management/settings/settings.component.html @@ -1 +1,20 @@ -

settings works!

+ +
+
+
+ User Password Management +
+ + + We'll never share your email with anyone else. +
+ +
+
+
+ diff --git a/support-portal-frontend/src/app/component/management/settings/settings.component.ts b/support-portal-frontend/src/app/component/management/settings/settings.component.ts index 4586f2d..8be9fff 100644 --- a/support-portal-frontend/src/app/component/management/settings/settings.component.ts +++ b/support-portal-frontend/src/app/component/management/settings/settings.component.ts @@ -1,15 +1,56 @@ -import {Component, OnInit} from '@angular/core'; +import {Component, OnDestroy, OnInit} from '@angular/core'; +import {AuthenticationService} from "../../../service/authentication.service"; +import {NgForm} from "@angular/forms"; +import {CustomHttpResponse} from "../../../dto/custom-http-response"; +import {NotificationType} from "../../../notification/notification-type"; +import {HttpErrorResponse} from "@angular/common/http"; +import {SubSink} from "subsink"; +import {UserService} from "../../../service/user.service"; +import {NotificationService} from "../../../service/notification.service"; @Component({ selector: 'app-settings', templateUrl: './settings.component.html', styleUrls: ['./settings.component.css'] }) -export class SettingsComponent implements OnInit { +export class SettingsComponent implements OnInit, OnDestroy { - constructor() { } + public refreshing: boolean = false; + + private subs = new SubSink(); + + constructor(private userService: UserService, + private notificationService: NotificationService, + private authenticationService: AuthenticationService) { + } ngOnInit(): void { } + ngOnDestroy(): void { + this.subs.unsubscribe(); + } + + public get isAdmin(): boolean { + return this.authenticationService.isLoggedUserHasRoleAdmin(); + } + + public onResetPassword(emailForm: NgForm): void { + this.refreshing = true; + let email = emailForm.value['reset-password-email']; + this.subs.sink = this.userService.resetPassword(email) + .subscribe( + (response: CustomHttpResponse) => { + this.notificationService.notify(NotificationType.SUCCESS, response.message); + }, + (errorResponse: HttpErrorResponse) => { + this.notificationService.notify(NotificationType.WARNING, errorResponse.error.message); + this.refreshing = false; + }, + () => { + this.refreshing = false; + emailForm.reset(); + } + ); + } } diff --git a/support-portal-frontend/src/app/service/authentication.service.ts b/support-portal-frontend/src/app/service/authentication.service.ts index c5ec89f..6db2667 100644 --- a/support-portal-frontend/src/app/service/authentication.service.ts +++ b/support-portal-frontend/src/app/service/authentication.service.ts @@ -5,6 +5,7 @@ import {UserLogin} from "../dto/user-login"; import {Observable} from "rxjs"; import {User} from "../model/user"; import {JwtHelperService} from "@auth0/angular-jwt"; +import {Role} from "../enum/role.enum"; @Injectable({ providedIn: 'root' @@ -16,7 +17,8 @@ export class AuthenticationService { private host: string = environment.apiUrl; private token: string | null; - private loggedInUser: string | null; + private loggedInUserName: string | null; + private loggedInUser: User | null; private storage = localStorage; //first install this module: `npm install @auth0/angular-jwt` @@ -37,7 +39,7 @@ export class AuthenticationService { public logout(): void { this.token = null; - this.loggedInUser = null; + this.loggedInUserName = null; this.storage.removeItem(this.JWT_TOKEN_STORAGE_KEY); this.storage.removeItem(this.USER_STORAGE_KEY); this.storage.removeItem("users"); @@ -71,7 +73,7 @@ export class AuthenticationService { let subject = this.jwtHelper.decodeToken(this.token).sub; if (subject != null || '') { if (!this.jwtHelper.isTokenExpired(this.token!)) { - this.loggedInUser = subject; + this.loggedInUserName = subject; return true; } } @@ -80,4 +82,11 @@ export class AuthenticationService { return false; } + public isLoggedUserHasRoleAdmin(): boolean { + if (!this.loggedInUser) + this.loggedInUser = this.getUserFromLocalStorage(); + return this.loggedInUser.role === Role.ADMIN || this.loggedInUser.role === Role.SUPER_ADMIN; + } + + }