All files index.ts

100% Statements 7/7
100% Branches 7/7
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18    1x 1x   1x     2x 2x 2x   2x          
import { Union } from './types';
 
export class AccessControl<T, P, R, U = string> {
  #roles: U | Union<T, R, P>;
  constructor(roles: U | Union<T, R, P>) {
    this.#roles = roles;
  }
  can(role: keyof Union<T, R, P>, action: keyof P, resource: keyof R) {
    const selectedRole = this.#roles[role as string];
    const hasResource = !!selectedRole && !!selectedRole[resource];
    const hasAction = hasResource && !!selectedRole[resource][action];
 
    return !!selectedRole && hasResource && hasAction;
  }
}
 
export * from './types';