All files / libs/kernel/auth/shared/src/abilities/user-profiles definitions.ts

90.9% Statements 130/143
100% Branches 25/25
87.5% Functions 14/16
90.9% Lines 130/143

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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 135x 1x 1x 1x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 45x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x 1x 1x                           1x 1x 1x 50x 50x 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 43x 43x 1x 1x 1x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 64x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 28x 1x  
import { canUserBeInvited } from '@amalia/tenants/users/profile/types';
 
import { SubsetAccessEnum } from '../../subsets/enums';
import { SubjectsEnum, type DefinePermissions, type UserRoleForAccessControl } from '../../types';
 
import { UserProfilesActions } from './actions';
import { type InviteProfileSubject, type UserProfileSubject } from './subjects';
 
export const userProfilesAbilityDefinitions = {
  ADMIN({ user }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      predicate: () => true,
      subset: SubsetAccessEnum.EVERYTHING,
    });
 
    can(UserProfilesActions.modify, SubjectsEnum.UserProfile);
 
    can(UserProfilesActions.invite, SubjectsEnum.UserProfile, (user: InviteProfileSubject) => canUserBeInvited(user));
 
    can(
      UserProfilesActions.toggle_deactivation,
      SubjectsEnum.UserProfile,
      // Can't deactivate themselves
      ({ id: userId }: UserProfileSubject) => user.id !== userId,
    );
  },
 
  READ_ONLY_ADMIN({ user }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      predicate: () => true,
      subset: SubsetAccessEnum.EVERYTHING,
    });
 
    can(
      UserProfilesActions.modify,
      SubjectsEnum.UserProfile,
      ['language'],
      /* can modify their own locale */ ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
  },
 
  FINANCE({ user }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      predicate: () => true,
      subset: SubsetAccessEnum.EVERYTHING,
    });
 
    can(
      UserProfilesActions.modify,
      SubjectsEnum.UserProfile,
      ['language'],
      /* can modify their own locale */ ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
  },
 
  SCOPED_ADMIN({ user, adminScopesContainer }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      /* can view their own profile and the profile of subordinates */
      predicate: ({ id: userId, date }: UserProfileSubject) =>
        userId === user.id || adminScopesContainer!.userIsInScope(userId, date ?? new Date()),
      subset: SubsetAccessEnum.IN_MY_SCOPE,
    });

    can(
      UserProfilesActions.modify,
      SubjectsEnum.UserProfile,
      ['language'],
      ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
  },
 
  MANAGER({ user, hierarchy }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      /* can view their own profile and the profile of subordinates */
      predicate: ({ id: userId, date }: UserProfileSubject) =>
        userId === user.id || hierarchy.isManagerOf(userId, date ?? new Date()),
      subset: SubsetAccessEnum.MATCH_MANAGEES,
    });
 
    can(
      UserProfilesActions.modify,
      SubjectsEnum.UserProfile,
      ['language'],
      /* can modify their own locale */ ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
  },
 
  READ_ONLY_MANAGER({ user, hierarchy }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      /* can view their own profile and the profile of subordinates */
      predicate: ({ id: userId, date }: UserProfileSubject) =>
        userId === user.id || hierarchy.isManagerOf(userId, date ?? new Date()),
      subset: SubsetAccessEnum.MATCH_MANAGEES,
    });
 
    can(
      UserProfilesActions.modify,
      SubjectsEnum.UserProfile,
      ['language'],
      /* can modify their own locale */ ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
  },
 
  EMPLOYEE({ user, hierarchy }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      /* can view their own profile and the profile of subordinates */
      predicate: ({ id: userId, date }: UserProfileSubject) =>
        userId === user.id || hierarchy.isManagerOf(userId, date ?? new Date()),
      subset: SubsetAccessEnum.MATCH_MANAGEES,
    });
 
    can(
      UserProfilesActions.modify,
      SubjectsEnum.UserProfile,
      ['language'],
      /* can modify their own locale */ ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
  },
 
  READ_ONLY_EMPLOYEE({ user, hierarchy }, { can }) {
    can(UserProfilesActions.view, SubjectsEnum.UserProfile, {
      /* can view their own profile and the profile of subordinates */
      predicate: ({ id: userId }: UserProfileSubject) =>
        userId === user.id || hierarchy.isManagerOf(userId, new Date()),
      subset: SubsetAccessEnum.MATCH_MANAGEES,
    });
  },
 
  DEACTIVATED_USER({ user }, { can }) {
    can(
      UserProfilesActions.view,
      SubjectsEnum.UserProfile,
      /* can view their own profile */ ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
 
    can(
      UserProfilesActions.modify,
      SubjectsEnum.UserProfile,
      ['language'],
      /* can modify their own locale */ ({ id: userId }: UserProfileSubject) => userId === user.id,
    );
  },
} as const satisfies Partial<Record<UserRoleForAccessControl, DefinePermissions>>;