Handle permission for routing

This commit is contained in:
Sven Heidemann 2025-01-09 20:01:56 +01:00
parent 9d5ca8b123
commit ea3d1ec561
2 changed files with 52 additions and 37 deletions

View File

@ -1,28 +1,34 @@
import { NgModule } from "@angular/core"; import { NgModule } from '@angular/core';
import { CommonModule } from "@angular/common"; import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from "@angular/router"; import { RouterModule, Routes } from '@angular/router';
import { SharedModule } from "src/app/modules/shared/shared.module"; import { SharedModule } from 'src/app/modules/shared/shared.module';
import { PermissionGuard } from 'src/app/core/guard/permission.guard';
import { PermissionsEnum } from 'src/app/model/auth/permissionsEnum';
const routes: Routes = [ const routes: Routes = [
{ {
path: "groups", path: 'groups',
loadChildren: () => loadChildren: () =>
import("src/app/modules/admin/groups/groups.module").then( import('src/app/modules/admin/groups/groups.module').then(
(m) => m.GroupsModule, m => m.GroupsModule
), ),
canActivate: [PermissionGuard],
data: { permissions: [PermissionsEnum.groups] },
}, },
{ {
path: "urls", path: 'urls',
loadChildren: () => loadChildren: () =>
import("src/app/modules/admin/short-urls/short-urls.module").then( import('src/app/modules/admin/short-urls/short-urls.module').then(
(m) => m.ShortUrlsModule, m => m.ShortUrlsModule
), ),
canActivate: [PermissionGuard],
data: { permissions: [PermissionsEnum.shortUrls] },
}, },
{ {
path: "administration", path: 'administration',
loadChildren: () => loadChildren: () =>
import("src/app/modules/admin/administration/administration.module").then( import('src/app/modules/admin/administration/administration.module').then(
(m) => m.AdministrationModule, m => m.AdministrationModule
), ),
}, },
]; ];

View File

@ -1,18 +1,18 @@
import { Injectable } from "@angular/core"; import { Injectable } from '@angular/core';
import { BehaviorSubject } from "rxjs"; import { BehaviorSubject } from 'rxjs';
import { MenuElement } from "src/app/model/view/menu-element"; import { MenuElement } from 'src/app/model/view/menu-element';
import { AuthService } from "src/app/service/auth.service"; import { AuthService } from 'src/app/service/auth.service';
import { PermissionsEnum } from "src/app/model/auth/permissionsEnum"; import { PermissionsEnum } from 'src/app/model/auth/permissionsEnum';
@Injectable({ @Injectable({
providedIn: "root", providedIn: 'root',
}) })
export class SidebarService { export class SidebarService {
visible$ = new BehaviorSubject<boolean>(true); visible$ = new BehaviorSubject<boolean>(true);
elements$ = new BehaviorSubject<MenuElement[]>([]); elements$ = new BehaviorSubject<MenuElement[]>([]);
constructor(private auth: AuthService) { constructor(private auth: AuthService) {
this.auth.user$.subscribe((user) => { this.auth.user$.subscribe(user => {
if (user) { if (user) {
this.setElements().then(); this.setElements().then();
} }
@ -31,14 +31,18 @@ export class SidebarService {
async setElements() { async setElements() {
const elements: MenuElement[] = [ const elements: MenuElement[] = [
{ {
label: "common.groups", label: 'common.groups',
icon: "pi pi-tags", icon: 'pi pi-tags',
routerLink: ["/admin/groups"], routerLink: ['/admin/groups'],
visible: await this.auth.hasAnyPermissionLazy([PermissionsEnum.groups]),
}, },
{ {
label: "common.urls", label: 'common.urls',
icon: "pi pi-tag", icon: 'pi pi-tag',
routerLink: ["/admin/urls"], routerLink: ['/admin/urls'],
visible: await this.auth.hasAnyPermissionLazy([
PermissionsEnum.shortUrls,
]),
}, },
await this.groupAdministration(), await this.groupAdministration(),
]; ];
@ -47,30 +51,35 @@ export class SidebarService {
async groupAdministration() { async groupAdministration() {
return { return {
label: "sidebar.administration", label: 'sidebar.administration',
icon: "pi pi-wrench", icon: 'pi pi-wrench',
expanded: true, expanded: true,
visible: await this.auth.hasAnyPermissionLazy([
PermissionsEnum.users,
PermissionsEnum.roles,
PermissionsEnum.apiKeys,
]),
items: [ items: [
{ {
label: "sidebar.users", label: 'sidebar.users',
icon: "pi pi-user", icon: 'pi pi-user',
routerLink: ["/admin/administration/users"], routerLink: ['/admin/administration/users'],
visible: await this.auth.hasAnyPermissionLazy([ visible: await this.auth.hasAnyPermissionLazy([
PermissionsEnum.users, PermissionsEnum.users,
]), ]),
}, },
{ {
label: "sidebar.roles", label: 'sidebar.roles',
icon: "pi pi-user-edit", icon: 'pi pi-user-edit',
routerLink: ["/admin/administration/roles"], routerLink: ['/admin/administration/roles'],
visible: await this.auth.hasAnyPermissionLazy([ visible: await this.auth.hasAnyPermissionLazy([
PermissionsEnum.roles, PermissionsEnum.roles,
]), ]),
}, },
{ {
label: "sidebar.api_keys", label: 'sidebar.api_keys',
icon: "pi pi-key", icon: 'pi pi-key',
routerLink: ["/admin/administration/api-keys"], routerLink: ['/admin/administration/api-keys'],
visible: await this.auth.hasAnyPermissionLazy([ visible: await this.auth.hasAnyPermissionLazy([
PermissionsEnum.apiKeys, PermissionsEnum.apiKeys,
]), ]),