Skip to content
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

fix(core): correctly check permissions for menu items to details pages #1994

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/app/core/permissions/permission-guard/user-role.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,25 @@ describe("UserRoleGuard", () => {
it("should check permissions of a given route (checkRoutePermissions)", () => {
mockConfigService.getConfig.and.callFake((id) => {
switch (id) {
case PREFIX_VIEW_CONFIG + "free":
return {} as any;
case PREFIX_VIEW_CONFIG + "restricted":
return { permittedUserRoles: ["admin"] } as any;
case PREFIX_VIEW_CONFIG + "pathA":
return {} as any;
case PREFIX_VIEW_CONFIG + "pathA/:id":
// details view restricted
return { permittedUserRoles: ["admin"] } as any;
}
});

mockSessionService.getCurrentUser.and.returnValue(normalUser);
expect(guard.checkRoutePermissions("free")).toBeTrue();
expect(guard.checkRoutePermissions("/free")).toBeTrue();
expect(guard.checkRoutePermissions("restricted")).toBeFalse();
expect(guard.checkRoutePermissions("pathA")).toBeTrue();
expect(guard.checkRoutePermissions("/pathA")).toBeTrue();
expect(guard.checkRoutePermissions("pathA/1")).toBeFalse();

mockSessionService.getCurrentUser.and.returnValue(adminUser);
expect(guard.checkRoutePermissions("free")).toBeTrue();
expect(guard.checkRoutePermissions("restricted")).toBeTrue();
expect(guard.checkRoutePermissions("pathA")).toBeTrue();
expect(guard.checkRoutePermissions("pathA/1")).toBeTrue();
});
});
16 changes: 13 additions & 3 deletions src/app/core/permissions/permission-guard/user-role.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,23 @@ export class UserRoleGuard implements CanActivate {
}

public checkRoutePermissions(path: string) {
// removing leading slash
path = path.replace(/^\//, "");
const userRoles = this.configService.getConfig<ViewConfig>(

let viewConfig = this.configService.getConfig<ViewConfig>(
PREFIX_VIEW_CONFIG + path,
)?.permittedUserRoles;
);
if (!viewConfig) {
// search for details route ("path/:id" for any id)
const detailsPath = path.replace(/\/[^\/]*$/, "/:id");
viewConfig = this.configService.getConfig<ViewConfig>(
PREFIX_VIEW_CONFIG + detailsPath,
);
}

return this.canActivate({
routeConfig: { path: path },
data: { permittedUserRoles: userRoles },
data: { permittedUserRoles: viewConfig?.permittedUserRoles },
} as any);
}
}