Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { PermissionCheckerForAttribute } from "@server/meta/fd-meta-core/attributes/permission-checker-for-attribute";
- import { ResourcePermissionChecker } from "@server/meta/fd-meta-core/auth/permissions/resource-permission-checker";
- import { singleOrDefault } from "@server/fd/fd-toolbox/collections/single-warnings";
- interface LifetimeScope {
- resolve: <T>(type: new () => T) => T[];
- }
- let permissionCheckers: ResourcePermissionChecker[] | null = null;
- function getPermissionCheckers(lifetimeScope: LifetimeScope): ResourcePermissionChecker[] {
- if (permissionCheckers === null) {
- permissionCheckers = lifetimeScope.resolve(ResourcePermissionChecker);
- }
- return permissionCheckers;
- }
- export function getPermissionChecker(resourceName: string, lifetimeScope: LifetimeScope) {
- const checkers = getPermissionCheckers(lifetimeScope);
- const matchingCheckers = checkers.filter((checker) => {
- const attr = Reflect.getMetadata(
- "PermissionCheckerFor",
- checker.constructor,
- ) as PermissionCheckerForAttribute;
- return attr?.ResourceName === resourceName;
- });
- return singleOrDefault(matchingCheckers);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement