All files / src/redux/slices/accessControl accessControl.ts

83.33% Statements 10/12
50% Branches 1/2
66.66% Functions 2/3
81.81% Lines 9/11

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                      541x                         541x   541x       80081x 80081x 80081x     541x       2215x 2215x                                        
import { PayloadAction, createSlice } from "@reduxjs/toolkit";
import initialState from "./initialState";
import {
  IAccessControlState,
  TAccessControlResource,
  TIsAllowedSelectorOptions,
  TUserPermissions,
} from "./types";
import { RootState } from "@redux/types/store";
import { OPERATIONS } from "@constants/permissions";
 
const accessControlSlice = createSlice({
  name: "accessControl",
  initialState,
  reducers: {
    initAccessControl: (
      state: IAccessControlState,
      action: PayloadAction<TUserPermissions>,
    ) => {
      state.authorizations = action.payload;
    },
  },
});
 
export const { initAccessControl } = accessControlSlice.actions;
 
export const selectIsAllowed = (
  state: RootState,
  permission: TIsAllowedSelectorOptions,
) => {
  const { resource, operation } = permission;
  const action = state.accessControl.authorizations[resource]?.[operation];
  return action === "allow";
};
 
export const selectIsAllowedByResource = (
  state: RootState,
  resource: TAccessControlResource,
) => {
  const actions = state.accessControl.authorizations[resource];
  Eif (actions) return actions;
 
  return {
    [OPERATIONS.READ]: "deny",
    [OPERATIONS.LIST]: "deny",
    [OPERATIONS.CREATE]: "deny",
    [OPERATIONS.UPDATE]: "deny",
    [OPERATIONS.DELETE]: "deny",
    [OPERATIONS.EXPORT]: "deny",
    [OPERATIONS.SUBMIT]: "deny",
    [OPERATIONS.CANCEL]: "deny",
    [OPERATIONS.ATTACH]: "deny",
    [OPERATIONS.LINK]: "deny",
    [OPERATIONS.UNLINK]: "deny",
    [OPERATIONS.MANAGE]: "deny",
    [OPERATIONS.BLOCK]: "deny",
  };
};
 
export default accessControlSlice.reducer;