From d23f5264c1ffe7b99a25721c82315c83ae4636f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adina=20=C8=9Aeudan?= Date: Tue, 27 Oct 2020 21:56:41 +0200 Subject: [PATCH] Hide buttons depending on user role --- apps/red-ui/src/app/app.module.ts | 15 +- .../app/auth/project-member-guard.service.ts | 53 +++ apps/red-ui/src/app/dialogs/dialog.service.ts | 2 +- .../file-preview-screen.component.html | 13 +- .../file-preview-screen.component.ts | 54 +-- .../project-listing-screen.component.html | 24 +- .../project-listing-screen.component.ts | 26 +- .../project-overview-screen.component.html | 24 +- .../project-overview-screen.component.ts | 439 +++++++++--------- .../red-ui/src/app/state/app-state.service.ts | 4 +- apps/red-ui/src/app/user/user.service.ts | 4 +- apps/red-ui/src/assets/i18n/en.json | 5 + package-lock.json | 0 13 files changed, 373 insertions(+), 290 deletions(-) create mode 100644 apps/red-ui/src/app/auth/project-member-guard.service.ts delete mode 100644 package-lock.json diff --git a/apps/red-ui/src/app/app.module.ts b/apps/red-ui/src/app/app.module.ts index 1deb8bdc2..faa1ba462 100644 --- a/apps/red-ui/src/app/app.module.ts +++ b/apps/red-ui/src/app/app.module.ts @@ -59,6 +59,7 @@ import { AssignOwnerDialogComponent } from './dialogs/assign-owner-dialog/assign import { MatDatepickerModule } from '@angular/material/datepicker'; import { MatNativeDateModule } from '@angular/material/core'; import { MatInputModule } from '@angular/material/input'; +import { ProjectMemberGuard } from './auth/project-member-guard.service'; export function HttpLoaderFactory(httpClient: HttpClient) { return new TranslateHttpLoader(httpClient, '/assets/i18n/', '.json'); @@ -131,7 +132,12 @@ export function HttpLoaderFactory(httpClient: HttpClient) { component: ProjectOverviewScreenComponent, canActivate: [CompositeRouteGuard], data: { - routeGuards: [AuthGuard, RedRoleGuard, AppStateGuard] + routeGuards: [ + AuthGuard, + RedRoleGuard, + ProjectMemberGuard, + AppStateGuard + ] } }, { @@ -139,7 +145,12 @@ export function HttpLoaderFactory(httpClient: HttpClient) { component: FilePreviewScreenComponent, canActivate: [CompositeRouteGuard], data: { - routeGuards: [AuthGuard, RedRoleGuard, AppStateGuard] + routeGuards: [ + AuthGuard, + RedRoleGuard, + ProjectMemberGuard, + AppStateGuard + ] } } ] diff --git a/apps/red-ui/src/app/auth/project-member-guard.service.ts b/apps/red-ui/src/app/auth/project-member-guard.service.ts new file mode 100644 index 000000000..3121d971a --- /dev/null +++ b/apps/red-ui/src/app/auth/project-member-guard.service.ts @@ -0,0 +1,53 @@ +import { Injectable } from '@angular/core'; +import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; +import { UserService } from '../user/user.service'; +import { AppLoadStateService } from '../utils/app-load-state.service'; +import { Observable } from 'rxjs'; +import { AppStateService } from '../state/app-state.service'; +import { NotificationService, NotificationType } from '../notification/notification.service'; +import { TranslateService } from '@ngx-translate/core'; + +@Injectable({ + providedIn: 'root' +}) +export class ProjectMemberGuard implements CanActivate { + constructor( + private readonly _router: Router, + private readonly _appLoadStateService: AppLoadStateService, + private readonly _appStateService: AppStateService, + private readonly _userService: UserService, + private readonly _notificationService: NotificationService, + private readonly _translateService: TranslateService + ) {} + + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { + return new Observable((obs) => { + if (this._userService.isManager(this._userService.user)) { + obs.next(true); + obs.complete(); + return; + } + + const projectId = route.params.projectId; + this._appStateService.loadAllProjects().then(() => { + const isProjectMember = this._appStateService.allProjects + .find((pw) => pw.project.projectId === projectId) + ?.project.memberIds.includes(this._userService.user.id); + if (!isProjectMember) { + this._router.navigate(['ui', 'projects']); + this._appLoadStateService.pushLoadingEvent(false); + this._notificationService.showToastNotification( + this._translateService.instant('project-member-guard.access-denied.label'), + null, + NotificationType.ERROR + ); + obs.next(false); + obs.complete(); + } else { + obs.next(true); + obs.complete(); + } + }); + }); + } +} diff --git a/apps/red-ui/src/app/dialogs/dialog.service.ts b/apps/red-ui/src/app/dialogs/dialog.service.ts index 64efdb9d6..a8dd76ed4 100644 --- a/apps/red-ui/src/app/dialogs/dialog.service.ts +++ b/apps/red-ui/src/app/dialogs/dialog.service.ts @@ -231,7 +231,7 @@ export class DialogService { return ref; } - public openAssignFileOwnerDialog( + public openAssignFileReviewerDialog( $event: MouseEvent, file: FileStatus, cb?: Function diff --git a/apps/red-ui/src/app/screens/file/file-preview-screen/file-preview-screen.component.html b/apps/red-ui/src/app/screens/file/file-preview-screen/file-preview-screen.component.html index 84e6a33fa..1e8d933d7 100644 --- a/apps/red-ui/src/app/screens/file/file-preview-screen/file-preview-screen.component.html +++ b/apps/red-ui/src/app/screens/file/file-preview-screen/file-preview-screen.component.html @@ -12,13 +12,21 @@
- -
@@ -73,18 +74,19 @@
@@ -122,26 +124,28 @@
diff --git a/apps/red-ui/src/app/screens/project-listing-screen/project-listing-screen.component.ts b/apps/red-ui/src/app/screens/project-listing-screen/project-listing-screen.component.ts index a68e9b68c..4186d35d2 100644 --- a/apps/red-ui/src/app/screens/project-listing-screen/project-listing-screen.component.ts +++ b/apps/red-ui/src/app/screens/project-listing-screen/project-listing-screen.component.ts @@ -1,12 +1,11 @@ -import { Component, OnInit } from '@angular/core'; -import { FileUploadControllerService, Project } from '@redaction/red-ui-http'; -import { AppStateService, ProjectWrapper } from '../../state/app-state.service'; -import { UserService } from '../../user/user.service'; -import { DoughnutChartConfig } from '../../components/simple-doughnut-chart/simple-doughnut-chart.component'; -import { SortingOption } from '../../utils/types'; -import { groupBy } from '../../utils/functions'; -import { DialogService } from '../../dialogs/dialog.service'; -import { download } from '../../utils/file-download-utils'; +import {Component, OnInit} from '@angular/core'; +import {Project} from '@redaction/red-ui-http'; +import {AppStateService, ProjectWrapper} from '../../state/app-state.service'; +import {UserService} from '../../user/user.service'; +import {DoughnutChartConfig} from '../../components/simple-doughnut-chart/simple-doughnut-chart.component'; +import {SortingOption} from '../../utils/types'; +import {groupBy} from '../../utils/functions'; +import {DialogService} from '../../dialogs/dialog.service'; @Component({ selector: 'redaction-project-listing-screen', @@ -28,8 +27,7 @@ export class ProjectListingScreenComponent implements OnInit { constructor( public readonly appStateService: AppStateService, - private readonly _fileUploadControllerService: FileUploadControllerService, - private readonly _userService: UserService, + public readonly userService: UserService, private readonly _dialogService: DialogService ) {} @@ -54,7 +52,7 @@ export class ProjectListingScreenComponent implements OnInit { } public get user() { - return this._userService.user; + return this.userService.user; } public get totalPages() { @@ -84,6 +82,10 @@ export class ProjectListingScreenComponent implements OnInit { return 1; } + public canOpenProject(pw: ProjectWrapper): boolean { + return this.userService.isManager(this.user) || pw.project.memberIds.includes(this.user.id); + } + public openAddProjectDialog(): void { this._dialogService.openAddProjectDialog(() => { this._calculateData(); diff --git a/apps/red-ui/src/app/screens/project-overview-screen/project-overview-screen.component.html b/apps/red-ui/src/app/screens/project-overview-screen/project-overview-screen.component.html index 49a0cb53c..4395556ac 100644 --- a/apps/red-ui/src/app/screens/project-overview-screen/project-overview-screen.component.html +++ b/apps/red-ui/src/app/screens/project-overview-screen/project-overview-screen.component.html @@ -194,6 +194,9 @@ -