Skip to content

Commit

Permalink
refactor: permission handling in components
Browse files Browse the repository at this point in the history
  • Loading branch information
StephGit committed Aug 23, 2024
1 parent 7bf43a8 commit 8db243f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 33 deletions.
6 changes: 5 additions & 1 deletion AMW_angular/io/src/app/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core';
import { BaseService } from '../base/base.service';
import { HttpClient } from '@angular/common/http';
import { Observable, startWith, Subject } from 'rxjs';
import { catchError, map, shareReplay, switchMap } from 'rxjs/operators';
import { catchError, shareReplay, switchMap } from 'rxjs/operators';
import { Restriction } from '../settings/permission/restriction';
import { toSignal } from '@angular/core/rxjs-interop';

Expand Down Expand Up @@ -44,3 +44,7 @@ export class AuthService extends BaseService {
);
}
}

export function isAllowed(action: string, role: string) {
return action === 'ALL' || action === role;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { IconComponent } from '../../shared/icon/icon.component';
import { AuthService } from '../../auth/auth.service';
import { AuthService, isAllowed } from '../../auth/auth.service';
import { Subject } from 'rxjs';
import { ToastService } from '../../shared/elements/toast/toast.service';

Expand Down Expand Up @@ -43,15 +43,9 @@ export class DeploymentParameterComponent implements OnInit, OnDestroy {
}

private getUserPermissions() {
this.authService.getActionsForPermission('MANAGE_DEPLOYMENT_PARAMETER').map((action) => {
if (action.indexOf('ALL') > -1) {
this.canDelete.set(true);
this.canCreate.set(true);
} else {
this.canCreate.set(action.indexOf('CREATE') > -1);
this.canDelete.set(action.indexOf('DELETE') > -1);
}
});
const actions = this.authService.getActionsForPermission('MANAGE_DEPLOYMENT_PARAMETER');
this.canCreate.set(actions.some((action) => isAllowed(action, 'CREATE')));
this.canDelete.set(actions.some((action) => isAllowed(action, 'DELETE')));
}

addKey(): void {
Expand Down
17 changes: 5 additions & 12 deletions AMW_angular/io/src/app/settings/releases/releases.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { ReleaseEditComponent } from './release-edit.component';
import { Release } from './release';
import { ReleasesService } from './releases.service';
import { NgbModal } from '@ng-bootstrap/ng-bootstrap';
import { AuthService } from '../../auth/auth.service';
import { AuthService, isAllowed } from '../../auth/auth.service';
import { map, takeUntil } from 'rxjs/operators';
import { ReleaseDeleteComponent } from './release-delete.component';
import { ToastService } from '../../shared/elements/toast/toast.service';
Expand Down Expand Up @@ -72,17 +72,10 @@ export class ReleasesComponent implements OnInit {
}

private getUserPermissions() {
this.authService.getActionsForPermission('RELEASE').map((action) => {
if (action.indexOf('ALL') > -1) {
this.canDelete.set(true);
this.canEdit.set(true);
this.canCreate.set(true);
} else {
this.canCreate.set(action.indexOf('CREATE') > -1);
this.canEdit.set(action.indexOf('UPDATE') > -1);
this.canDelete.set(action.indexOf('DELETE') > -1);
}
});
const actions = this.authService.getActionsForPermission('RELEASE');
this.canCreate.set(actions.some((action) => isAllowed(action, 'CREATE')));
this.canEdit.set(actions.some((action) => isAllowed(action, 'UPDATE')));
this.canDelete.set(actions.some((action) => isAllowed(action, 'DELETE')));
}

private getReleases() {
Expand Down
14 changes: 4 additions & 10 deletions AMW_angular/io/src/app/settings/tags/tags.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Component, inject, OnDestroy, OnInit, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { IconComponent } from '../../shared/icon/icon.component';
import { Subject } from 'rxjs';
import { AuthService } from '../../auth/auth.service';
import { AuthService, isAllowed } from '../../auth/auth.service';
import { ToastService } from '../../shared/elements/toast/toast.service';
import { TagsService } from './tags.service';

Expand Down Expand Up @@ -34,15 +34,9 @@ export class TagsComponent implements OnInit, OnDestroy {
}

private getUserPermissions() {
this.authService.getActionsForPermission('MANAGE_GLOBAL_TAGS').map((action) => {
if (action.indexOf('ALL') > -1) {
this.canCreate.set(true);
this.canDelete.set(true);
} else {
this.canCreate.set(action.indexOf('CREATE') > -1);
this.canDelete.set(action.indexOf('DELETE') > -1);
}
});
const actions = this.authService.getActionsForPermission('MANAGE_GLOBAL_TAGS');
this.canCreate.set(actions.some((action) => isAllowed(action, 'CREATE')));
this.canDelete.set(actions.some((action) => isAllowed(action, 'DELETE')));
}

addTag(): void {
Expand Down

0 comments on commit 8db243f

Please sign in to comment.