Advertisement
djbob2000

Untitled

Aug 7th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { PermissionCheckerForAttribute } from "@server/meta/fd-meta-core/attributes/permission-checker-for-attribute";
  2. import { ResourcePermissionChecker } from "@server/meta/fd-meta-core/auth/permissions/resource-permission-checker";
  3. import { singleOrDefault } from "@server/fd/fd-toolbox/collections/single-warnings";
  4.  
  5. interface LifetimeScope {
  6.     resolve: <T>(type: new () => T) => T[];
  7. }
  8.  
  9. let permissionCheckers: ResourcePermissionChecker[] | null = null;
  10.  
  11. function getPermissionCheckers(lifetimeScope: LifetimeScope): ResourcePermissionChecker[] {
  12.     if (permissionCheckers === null) {
  13.         permissionCheckers = lifetimeScope.resolve(ResourcePermissionChecker);
  14.     }
  15.     return permissionCheckers;
  16. }
  17.  
  18. export function getPermissionChecker(resourceName: string, lifetimeScope: LifetimeScope) {
  19.     const checkers = getPermissionCheckers(lifetimeScope);
  20.  
  21.     const matchingCheckers = checkers.filter((checker) => {
  22.         const attr = Reflect.getMetadata(
  23.             "PermissionCheckerFor",
  24.             checker.constructor,
  25.         ) as PermissionCheckerForAttribute;
  26.         return attr?.ResourceName === resourceName;
  27.     });
  28.  
  29.     return singleOrDefault(matchingCheckers);
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement