From 592b5341a8c653d08f3d11e7b8dd06d5d726eaa3 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Fri, 16 Feb 2024 11:27:45 +0100 Subject: [PATCH] fix(core): generate valid regex in guard also for wildcard routes (#2231) --- .../abstract-permission.guard.spec.ts | 46 +++++++++++++++++++ .../abstract-permission.guard.ts | 1 + 2 files changed, 47 insertions(+) create mode 100644 src/app/core/permissions/permission-guard/abstract-permission.guard.spec.ts diff --git a/src/app/core/permissions/permission-guard/abstract-permission.guard.spec.ts b/src/app/core/permissions/permission-guard/abstract-permission.guard.spec.ts new file mode 100644 index 0000000000..cc44dc4353 --- /dev/null +++ b/src/app/core/permissions/permission-guard/abstract-permission.guard.spec.ts @@ -0,0 +1,46 @@ +import { TestBed } from "@angular/core/testing"; +import { Router, Routes } from "@angular/router"; +import { AbstractPermissionGuard } from "./abstract-permission.guard"; +import { Injectable } from "@angular/core"; +import { DynamicComponentConfig } from "../../config/dynamic-components/dynamic-component-config.interface"; + +@Injectable() +class TestPermissionGuard extends AbstractPermissionGuard { + constructor(router: Router) { + super(router); + } + + protected async canAccessRoute( + routeData: DynamicComponentConfig, + ): Promise { + return routeData?.config; + } +} + +describe("EntityPermissionGuard", () => { + let guard: TestPermissionGuard; + + let testRoutes: Routes; + + beforeEach(() => { + testRoutes = [{ path: "**", data: { config: true } }]; + + TestBed.configureTestingModule({ + providers: [ + TestPermissionGuard, + { provide: Router, useValue: { config: testRoutes } }, + ], + }); + guard = TestBed.inject(TestPermissionGuard); + }); + + it("should be created", () => { + expect(guard).toBeTruthy(); + }); + + it("should get route config also for '**' path", async () => { + const result = await guard.checkRoutePermissions("url"); + + expect(result).toBeTrue(); + }); +}); diff --git a/src/app/core/permissions/permission-guard/abstract-permission.guard.ts b/src/app/core/permissions/permission-guard/abstract-permission.guard.ts index a59e038f59..cd0d0e76d6 100644 --- a/src/app/core/permissions/permission-guard/abstract-permission.guard.ts +++ b/src/app/core/permissions/permission-guard/abstract-permission.guard.ts @@ -63,6 +63,7 @@ export abstract class AbstractPermissionGuard implements CanActivate { function isPathMatch(genericPath: string, path: string) { const routeRegex = genericPath + .replace(/\*/g, ".*") // allow for wildcard routes in regex .split("/") // replace params with wildcard regex .map((part) => (part.startsWith(":") ? "[^/]*" : part))