All files / libs/kernel/auth/shared/src/abilities/teams definitions.ts

95.45% Statements 84/88
100% Branches 13/13
62.5% Functions 10/16
95.45% Lines 84/88

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 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 891x 1x 1x 1x 1x 1x 1x 1x 135x 135x 135x 135x 1x 1x 1x 45x 45x 1x 1x 1x     1x 1x 1x     1x 1x 1x 28x 28x 1x 1x 1x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 50x 1x 1x 1x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 43x 1x 1x 1x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { SubsetAccessEnum } from '../../subsets/enums';
import { SubjectsEnum, type DefinePermissions, type UserRoleForAccessControl } from '../../types';
 
import { TeamsActions } from './actions';
import { type ViewAssignmentsTeamSubject, type ViewTeamSubject } from './subjects';
 
export const teamsAbilityDefinitions = {
  ADMIN(_, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, { predicate: () => true, subset: SubsetAccessEnum.EVERYTHING });
    can(TeamsActions.modify, SubjectsEnum.Team);
    can(TeamsActions.view_assignments, SubjectsEnum.Team);
    can(TeamsActions.modify_assignments, SubjectsEnum.Team);
  },
 
  READ_ONLY_ADMIN(_, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, { predicate: () => true, subset: SubsetAccessEnum.EVERYTHING });
    can(TeamsActions.view_assignments, SubjectsEnum.Team);
  },
 
  SCOPED_ADMIN(_, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, { predicate: () => false, subset: SubsetAccessEnum.NOTHING });
    can(TeamsActions.view_assignments, SubjectsEnum.Team);
  },
 
  SCOPED_READ_ONLY_ADMIN(_, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, { predicate: () => false, subset: SubsetAccessEnum.NOTHING });
    can(TeamsActions.view_assignments, SubjectsEnum.Team);
  },
 
  FINANCE(_, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, { predicate: () => true, subset: SubsetAccessEnum.EVERYTHING });
    can(TeamsActions.view_assignments, SubjectsEnum.Team);
  },
 
  MANAGER({ hierarchy }, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, {
      predicate: ({ team, date }: ViewTeamSubject) =>
        !!team.id && hierarchy.getTeamIdsWhereUserIsManager(date ?? new Date()).includes(team.id),
      subset: SubsetAccessEnum.MATCH_TEAMS,
    });
 
    can(
      TeamsActions.view_assignments,
      SubjectsEnum.Team,
      ({ id }: ViewAssignmentsTeamSubject) => !!id && hierarchy.getTeamIdsWhereUserIsManager(new Date()).includes(id),
    );
  },
 
  READ_ONLY_MANAGER({ hierarchy }, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, {
      predicate: ({ team, date }: ViewTeamSubject) =>
        !!team.id && hierarchy.getTeamIdsWhereUserIsManager(date ?? new Date()).includes(team.id),
      subset: SubsetAccessEnum.MATCH_TEAMS,
    });
 
    can(
      TeamsActions.view_assignments,
      SubjectsEnum.Team,
      ({ id }: ViewAssignmentsTeamSubject) => !!id && hierarchy.getTeamIdsWhereUserIsManager(new Date()).includes(id),
    );
  },
 
  EMPLOYEE({ hierarchy }, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, {
      predicate: ({ team, date }: ViewTeamSubject) => !!team.id && hierarchy.isTeamMember(team.id, date ?? new Date()),
      subset: SubsetAccessEnum.MATCH_TEAMS,
    });
 
    can(
      TeamsActions.view_assignments,
      SubjectsEnum.Team,
      ({ id }: ViewAssignmentsTeamSubject) => !!id && hierarchy.isTeamMember(id, new Date()),
    );
  },
 
  READ_ONLY_EMPLOYEE({ hierarchy }, { can }) {
    can(TeamsActions.view, SubjectsEnum.Team, {
      predicate: ({ team, date }: ViewTeamSubject) => !!team.id && hierarchy.isTeamMember(team.id, date ?? new Date()),
      subset: SubsetAccessEnum.MATCH_TEAMS,
    });
 
    can(
      TeamsActions.view_assignments,
      SubjectsEnum.Team,
      ({ id }: ViewAssignmentsTeamSubject) => !!id && hierarchy.isTeamMember(id, new Date()),
    );
  },
} as const satisfies Partial<Record<UserRoleForAccessControl, DefinePermissions>>;