All files / libs/kernel/auth/shared/src/abilities/lti-grants definitions.ts

48.73% Statements 58/119
53.33% Branches 8/15
80% Functions 8/10
48.73% Lines 58/119

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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 1201x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 135x 135x 135x       1x 1x 1x 45x 45x 45x     1x 1x 1x                     1x 1x 1x                     1x 1x 1x 28x 28x 28x               1x 1x 1x 50x 50x 50x               1x 1x 1x 43x 43x 43x               1x 1x 1x 64x 64x 64x               1x 1x 1x 1x 1x 1x                 1x  
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
 
import { SubjectsEnum, type DefinePermissions, type UserRoleForAccessControl } from '../../types';
 
import { LtiGrantsActions } from './actions';
import { type LtiGrantSubject } from './subjects';
 
const isLtiFFEnabled = (authenticatedContext: AuthenticatedContext) =>
  !!authenticatedContext.user.company.featureFlags.LTI;
 
export const ltiGrantsAbilityDefinitions = {
  ADMIN(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(LtiGrantsActions.edit, SubjectsEnum.LtiGrant);
    can(LtiGrantsActions.view, SubjectsEnum.LtiGrant);
  },
 
  READ_ONLY_ADMIN(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(LtiGrantsActions.view, SubjectsEnum.LtiGrant);
  },
 
  SCOPED_ADMIN(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(
      LtiGrantsActions.view,
      SubjectsEnum.LtiGrant,
      ({ userId, invitationSentAt }: LtiGrantSubject) =>
        userId === authenticatedContext.user.id && invitationSentAt !== null,
    );
  },
 
  SCOPED_READ_ONLY_ADMIN(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(
      LtiGrantsActions.view,
      SubjectsEnum.LtiGrant,
      ({ userId, invitationSentAt }: LtiGrantSubject) =>
        userId === authenticatedContext.user.id && invitationSentAt !== null,
    );
  },
 
  FINANCE(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(
      LtiGrantsActions.view,
      SubjectsEnum.LtiGrant,
      ({ userId, invitationSentAt }: LtiGrantSubject) =>
        userId === authenticatedContext.user.id && invitationSentAt !== null,
    );
  },
 
  MANAGER(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(
      LtiGrantsActions.view,
      SubjectsEnum.LtiGrant,
      ({ userId, invitationSentAt }: LtiGrantSubject) =>
        userId === authenticatedContext.user.id && invitationSentAt !== null,
    );
  },
 
  READ_ONLY_MANAGER(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(
      LtiGrantsActions.view,
      SubjectsEnum.LtiGrant,
      ({ userId, invitationSentAt }: LtiGrantSubject) =>
        userId === authenticatedContext.user.id && invitationSentAt !== null,
    );
  },
 
  EMPLOYEE(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(
      LtiGrantsActions.view,
      SubjectsEnum.LtiGrant,
      ({ userId, invitationSentAt }: LtiGrantSubject) =>
        userId === authenticatedContext.user.id && invitationSentAt !== null,
    );
  },
 
  READ_ONLY_EMPLOYEE(authenticatedContext, { can }) {
    if (!isLtiFFEnabled(authenticatedContext)) {
      return;
    }

    can(
      LtiGrantsActions.view,
      SubjectsEnum.LtiGrant,
      ({ userId, invitationSentAt }: LtiGrantSubject) =>
        userId === authenticatedContext.user.id && invitationSentAt !== null,
    );
  },
} as const satisfies Partial<Record<UserRoleForAccessControl, DefinePermissions>>;