-
Notifications
You must be signed in to change notification settings - Fork 5
feat: permissions guards actions #1355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import { Injectable, inject } from '@angular/core'; | ||
| import { CanActivate, Router } from '@angular/router'; | ||
| import { UserService } from '@services/user.service'; | ||
|
|
||
| @Injectable({ | ||
| providedIn: 'root' | ||
| }) | ||
| export class ApplicationsAccessGuard implements CanActivate { | ||
| private readonly userService = inject(UserService); | ||
| private readonly router = inject(Router); | ||
|
|
||
| canActivate(): boolean { | ||
| const permissions = this.userService.user?.permissions ?? []; | ||
| const hasPermission = permissions.includes('Applications:ListApplications'); | ||
|
|
||
| if (!hasPermission) { | ||
| this.router.navigate(['/dashboard']); | ||
| return false; | ||
| } | ||
|
|
||
| return true; | ||
| } | ||
|
Comment on lines
+8
to
+22
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you create an abstract class with just an abstract member We would have in the end: export class ApplicationsAccessGuard extends AbstractAcessGuard {
permission = 'Applications:ListApplications';
} |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,11 @@ | ||
| import { Route } from '@angular/router'; | ||
| import { ApplicationsAccessGuard } from './guards/applications-access.guard'; | ||
| import { IndexComponent } from './index.component'; | ||
|
|
||
| export const APPLICATIONS_ROUTES: Route[] = [ | ||
| { path: '', component: IndexComponent }, | ||
| { | ||
| path: '', | ||
| component: IndexComponent, | ||
| canActivate: [ApplicationsAccessGuard] | ||
| }, | ||
| ]; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,11 @@ | ||
| <app-view-tasks-by-status | ||
| [defaultQueryParams]="queryParams" | ||
| [loading]="loading" | ||
| [statusesGroups]="statusesGroups" | ||
| [statusesCount]="statusesCount()" | ||
| > | ||
| </app-view-tasks-by-status> | ||
| @if (hasCountPermission) { | ||
| <app-view-tasks-by-status | ||
| [defaultQueryParams]="queryParams" | ||
| [loading]="loading" | ||
| [statusesGroups]="statusesGroups" | ||
| [statusesCount]="statusesCount()" | ||
| > | ||
| </app-view-tasks-by-status> | ||
| } @else { | ||
| <span i18n>No permission to view tasks count</span> | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,7 @@ import { TasksStatusesGroup } from '@app/dashboard/types'; | |
| import { TasksFiltersService } from '@app/tasks/services/tasks-filters.service'; | ||
| import { TasksGrpcService } from '@app/tasks/services/tasks-grpc.service'; | ||
| import { StatusCount, TaskSummaryFilters } from '@app/tasks/types'; | ||
| import { UserService } from '@services/user.service'; | ||
| import { Observable, Subject, of } from 'rxjs'; | ||
| import { CountTasksByStatusComponent } from './count-tasks-by-status.component'; | ||
|
|
||
|
|
@@ -43,12 +44,20 @@ describe('CountTasksByStatusComponent', () => { | |
| const refresh$ = new Subject<void>(); | ||
| const refreshSpy = jest.spyOn(refresh$, 'next'); | ||
|
|
||
| const mockUserService = { | ||
| user: { | ||
| permissions: ['Tasks:CountTasksByStatus'] | ||
| } | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| component = TestBed.configureTestingModule({ | ||
| providers: [ | ||
| CountTasksByStatusComponent, | ||
| { provide: TasksGrpcService, useValue: mockTasksGrpcService }, | ||
| TasksFiltersService | ||
| TasksFiltersService, | ||
| { provide: UserService, useValue: mockUserService }, | ||
| { provide: UserService, useValue: mockUserService } | ||
| ] | ||
| }).inject(CountTasksByStatusComponent); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you provide a test for:
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { TestBed } from '@angular/core/testing'; | |
| import { ByteArrayService } from '@services/byte-array.service'; | ||
| import { IconsService } from '@services/icons.service'; | ||
| import { NotificationService } from '@services/notification.service'; | ||
| import { UserService } from '@services/user.service'; | ||
| import { ByteArrayComponent } from './byte-array.component'; | ||
|
|
||
| describe('ByteArrayComponent', () => { | ||
|
|
@@ -25,6 +26,12 @@ describe('ByteArrayComponent', () => { | |
| success: jest.fn(), | ||
| }; | ||
|
|
||
| const mockUserService = { | ||
| user: { | ||
| permissions: ['Results:DownloadResultData'] | ||
| } | ||
| }; | ||
|
|
||
| let dataContent = ''; | ||
| for(let i = 0; i !== 129; i++) { | ||
| dataContent += ' '; | ||
|
|
@@ -44,6 +51,7 @@ describe('ByteArrayComponent', () => { | |
| { provide: IconsService, useValue: mockIconsService }, | ||
| { provide: Clipboard, useValue: mockClipboard }, | ||
| { provide: NotificationService, useValue: mockNotificationService }, | ||
| { provide: UserService, useValue: mockUserService }, | ||
| ] | ||
| }).inject(ByteArrayComponent); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide test for hasDownloadPermission |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { PrettyPipe } from '@pipes/pretty.pipe'; | |
| import { ByteArrayService } from '@services/byte-array.service'; | ||
| import { IconsService } from '@services/icons.service'; | ||
| import { NotificationService } from '@services/notification.service'; | ||
| import { UserService } from '@services/user.service'; | ||
|
|
||
| /** | ||
| * Displays a byte array in armonik inspection pages. | ||
|
|
@@ -50,6 +51,7 @@ export class ByteArrayComponent { | |
| private readonly iconsService = inject(IconsService); | ||
| private readonly clipboard = inject(Clipboard); | ||
| private readonly notificationService = inject(NotificationService); | ||
| private readonly userService = inject(UserService); | ||
|
|
||
| /** | ||
| * Returns the icon associated with that name. | ||
|
|
@@ -90,4 +92,9 @@ export class ByteArrayComponent { | |
| this.notificationService.success('Copied to clipboard'); | ||
| } | ||
| } | ||
|
|
||
| get hasDownloadPermission(): boolean { | ||
| const permissions = this.userService.user?.permissions ?? []; | ||
| return permissions.includes('Results:DownloadResultData'); | ||
| } | ||
|
Comment on lines
+96
to
+99
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not generic enough. if we have another download action, we will have to add it there, and it will not be easy to do it in every component. |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ import { TestBed } from '@angular/core/testing'; | |
| import { ByteArrayService } from '@services/byte-array.service'; | ||
| import { IconsService } from '@services/icons.service'; | ||
| import { NotificationService } from '@services/notification.service'; | ||
| import { UserService } from '@services/user.service'; | ||
| import { ByteArrayComponent } from './byte-array-cell.component'; | ||
|
|
||
| describe('ByteArrayComponent', () => { | ||
|
|
@@ -25,6 +26,12 @@ describe('ByteArrayComponent', () => { | |
| success: jest.fn(), | ||
| }; | ||
|
|
||
| const mockUserService = { | ||
| user: { | ||
| permissions: ['Results:DownloadResultData'] | ||
| } | ||
| }; | ||
|
|
||
|
|
||
| let dataContent = ''; | ||
| for(let i = 0; i !== 128; i++) { | ||
|
|
@@ -45,6 +52,7 @@ describe('ByteArrayComponent', () => { | |
| { provide: IconsService, useValue: mockIconsService }, | ||
| { provide: Clipboard, useValue: mockClipboard }, | ||
| { provide: NotificationService, useValue: mockNotificationService }, | ||
| { provide: UserService, useValue: mockUserService }, | ||
| ] | ||
| }).inject(ByteArrayComponent); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide test for hasDownloadPermission |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import { MatTooltipModule } from '@angular/material/tooltip'; | |
| import { ByteArrayService } from '@services/byte-array.service'; | ||
| import { IconsService } from '@services/icons.service'; | ||
| import { NotificationService } from '@services/notification.service'; | ||
| import { UserService } from '@services/user.service'; | ||
|
|
||
| /** | ||
| * Displays a byte array in armonik tables. | ||
|
|
@@ -51,6 +52,7 @@ export class ByteArrayComponent { | |
| private readonly iconsService = inject(IconsService); | ||
| readonly clipboard = inject(Clipboard); | ||
| private readonly notificationService = inject(NotificationService); | ||
| private readonly userService = inject(UserService); | ||
|
|
||
| /** | ||
| * Download the byteArray in a binary file. | ||
|
|
@@ -90,4 +92,9 @@ export class ByteArrayComponent { | |
| getIcon(name: string): string { | ||
| return this.iconsService.getIcon(name); | ||
| } | ||
|
|
||
| get hasDownloadPermission(): boolean { | ||
| const permissions = this.userService.user?.permissions ?? []; | ||
| return permissions.includes('Results:DownloadResultData'); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kinda like the You will need to update the type by adding a |
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,26 @@ | ||
| <h2 mat-dialog-title i18n="Dialog title">Add a line</h2> | ||
|
|
||
| <form [formGroup]="formGroup" (ngSubmit)="onSubmit()"> | ||
| @if (hasAvailableTypes) { | ||
| <form [formGroup]="formGroup" (ngSubmit)="onSubmit()"> | ||
| <mat-dialog-content> | ||
| <mat-form-field appearance="outline"> | ||
| <mat-label for="name" i18n="Name of the statuses group"> Name </mat-label> | ||
| <input matInput id="name" type="text" formControlName="name" i18n-placeholder="Placeholder" placeholder="Name of your line" required i18n="Input error"> | ||
| </mat-form-field> | ||
| <app-autocomplete [options]="types" [value]="type" [label]="typeLabel" (valueChange)="onTypeChange($event)" /> | ||
| </mat-dialog-content> | ||
|
|
||
| <mat-dialog-actions align="end"> | ||
| <button mat-button (click)="onCancel()" type="button" i18n="Dialog action"> Cancel </button> | ||
| <button mat-flat-button type="submit" color="primary" [disabled]="!validType" i18n="Dialog action"> Confirm </button> | ||
| </mat-dialog-actions> | ||
| </form> | ||
| } @else { | ||
| <mat-dialog-content> | ||
| <mat-form-field appearance="outline"> | ||
| <mat-label for="name" i18n="Name of the statuses group"> Name </mat-label> | ||
| <input matInput id="name" type="text" formControlName="name" i18n-placeholder="Placeholder" placeholder="Name of your line" required i18n="Input error"> | ||
| </mat-form-field> | ||
| <app-autocomplete [options]="types" [value]="type" [label]="typeLabel" (valueChange)="onTypeChange($event)" /> | ||
| <p i18n="No permissions message">You don't have permissions to add any type of line.</p> | ||
| </mat-dialog-content> | ||
|
|
||
| <mat-dialog-actions align="end"> | ||
| <button mat-button (click)="onCancel()" type="button" i18n="Dialog action"> Cancel </button> | ||
| <button mat-flat-button type="submit" color="primary" [disabled]="!validType" i18n="Dialog action"> Confirm </button> | ||
| <button mat-button (click)="onCancel()" type="button" i18n="Dialog action"> Close </button> | ||
| </mat-dialog-actions> | ||
| </form> | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think you have to do this, since it already initialize the sidebar on its construction (when the service is provided for the first time)