Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move permission hierarchy to the FE #49

Merged
merged 3 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/__tests__/roles/[roleId]/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,6 @@ describe("pages/roles/[roleId]/index", () => {
})
).toBeChecked();
});

// todo test heirachy
});
33 changes: 0 additions & 33 deletions src/frontend/docs/roles.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,39 +94,6 @@ export function RolesDocumentation(props: IDocumentationRootProps) {
</b>
: enables users to reset passwords.
</p>

<h4>Permissions Dependencies </h4>
<p>
Enabling some permissions will automatically grant some other
permission. For example, giving someone permission to reset a user&apos;
password without giving them permission to manage users doesn&apos;t
make sense as they need to be able to access users first to get to where
they will be able to reset the password. This is what we call
permissions dependencies.
</p>
<p>
These are the permission dependencies.
<ul>
<li>
<code>{userFriendlyCase(USER_PERMISSIONS.CAN_RESET_PASSWORD)}</code>{" "}
- <code>{userFriendlyCase(USER_PERMISSIONS.CAN_MANAGE_USERS)}</code>
</li>
<li>
<code>
{userFriendlyCase(USER_PERMISSIONS.CAN_MANAGE_INTEGRATIONS)}
</code>{" "}
-{" "}
<code>{userFriendlyCase(USER_PERMISSIONS.CAN_CONFIGURE_APP)}</code>
</li>
<li>
<code>{userFriendlyCase(USER_PERMISSIONS.CAN_CONFIGURE_APP)}</code>{" "}
-{" "}
<code>
{userFriendlyCase(USER_PERMISSIONS.CAN_MANAGE_ALL_ENTITIES)}
</code>
</li>
</ul>
</p>
</DocumentationRoot>
);
}
5 changes: 4 additions & 1 deletion src/frontend/views/entity/Crud/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,13 @@ function useEntityCrudView() {

if (field === "details" && !newState.details) {
newState.update = false;
newState.delete = false;
} else if (field === "update" && newState.update) {
newState.details = true;
} else if (field === "delete" && newState.delete) {
newState.details = true;
}

setEntityCrudSettingsState(newState);
upsertCrudSettingsMutation.mutateAsync(
newState as unknown as Record<string, string>
Expand Down
49 changes: 43 additions & 6 deletions src/frontend/views/roles/Permissions/MutatePermission.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,50 @@ import {
useRolePermissionDeletionMutation,
useRolePermissions,
} from "../permissions.store";
import { USER_PERMISSIONS } from "shared/constants/user";
import { PORTAL_PERMISSION_HEIRACHIES } from "shared/logic/permissions/portal";

interface IProps {
permissionList: ILabelValue[];
overAchingPermission?: string;
singular: string;
}

/*
IMPORTANT NOTE:
LESSER PERMISSION FIRST
*/
const PERMISSION_HEIRACHIES = {
[USER_PERMISSIONS.CAN_MANAGE_USERS]: USER_PERMISSIONS.CAN_RESET_PASSWORD,
[USER_PERMISSIONS.CAN_CONFIGURE_APP]:
USER_PERMISSIONS.CAN_MANAGE_INTEGRATIONS,
[USER_PERMISSIONS.CAN_MANAGE_ALL_ENTITIES]:
USER_PERMISSIONS.CAN_CONFIGURE_APP,
...PORTAL_PERMISSION_HEIRACHIES,
};

export const getPermissionChildren = (
permission: string,
mainKey: 1 | 0,
permissions: string[] = []
): string[] => {
permissions.push(permission);

const permissionHeirachy = Object.entries<string>(PERMISSION_HEIRACHIES).find(
(value) => value[mainKey === 1 ? 0 : 1] === permission
);

if (!permissionHeirachy) {
return permissions;
}

return getPermissionChildren(
permissionHeirachy[mainKey],
mainKey,
permissions
);
};

export function MutatePermission({
permissionList,
singular,
Expand Down Expand Up @@ -79,13 +116,13 @@ export function MutatePermission({
selected: isPermissionSelected,
onChange: () => {
if (isPermissionSelected) {
rolePermissionDeletionMutation.mutate([
menuItem.value,
]);
rolePermissionDeletionMutation.mutate(
getPermissionChildren(menuItem.value, 1)
);
} else {
rolePermissionCreationMutation.mutate([
menuItem.value,
]);
rolePermissionCreationMutation.mutate(
getPermissionChildren(menuItem.value, 0)
);
}
},
}
Expand Down
1 change: 1 addition & 0 deletions src/frontend/views/roles/Permissions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const mapPermissionStringToLabelValue = (permissionStringList: string[]) => {
}));
};

// TODO sort by heirachy
const adminPermissionList: ILabelValue[] = mapPermissionStringToLabelValue(
Object.values(BASE_USER_PERMISSIONS)
);
Expand Down
22 changes: 1 addition & 21 deletions src/shared/logic/permissions/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,7 @@ describe("user role checks", () => {
).toBe(false);
});
});
describe("Heirachy", () => {
it("should return true for PERMISSION_HEIRACHIES down", () => {
expect(
doesPermissionAllowPermission(
["CAN_RESET_PASSWORD", "PERMISSION_2"],
"CAN_MANAGE_USERS",
false
)
).toBe(true);
});
it("should return false for PERMISSION_HEIRACHIES down", () => {
expect(
doesPermissionAllowPermission(
["CAN_MANAGE_USERS", "PERMISSION_2"],
"CAN_RESET_PASSWORD",
false
)
).toBe(false);
});
});
describe.only("Check Granular", () => {
describe("Check Granular", () => {
it("should pass check granular when granular is true", () => {
expect(
doesPermissionAllowPermission(
Expand Down
35 changes: 2 additions & 33 deletions src/shared/logic/permissions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,7 @@ import {
} from "shared/constants/user";
import { GranularEntityPermissions, SystemRoles } from "shared/types/user";
import { replaceGranular } from "shared/constants/user/shared";
import {
portalMetaPermissionCheck,
PORTAL_PERMISSION_HEIRACHIES,
} from "./portal";

/*
IMPORTANT NOTE:
LESSER PERMISSION FIRST
*/
// TODO move this logic to the FE for more visibility (For contributors)
const PERMISSION_HEIRACHIES = {
[USER_PERMISSIONS.CAN_MANAGE_USERS]: USER_PERMISSIONS.CAN_RESET_PASSWORD,
[USER_PERMISSIONS.CAN_CONFIGURE_APP]:
USER_PERMISSIONS.CAN_MANAGE_INTEGRATIONS,
[USER_PERMISSIONS.CAN_MANAGE_ALL_ENTITIES]:
USER_PERMISSIONS.CAN_CONFIGURE_APP,
...PORTAL_PERMISSION_HEIRACHIES,
};
import { portalMetaPermissionCheck } from "./portal";

const doMetaPermissionCheck =
(permissions: string[], requiredPermission: string) =>
Expand Down Expand Up @@ -77,21 +60,7 @@ export const doesPermissionAllowPermission = (
);
}

const can = permissions.includes(requiredPermission);

if (can) {
return true;
}

if (!PERMISSION_HEIRACHIES[requiredPermission]) {
return false;
}

return doesPermissionAllowPermission(
permissions,
PERMISSION_HEIRACHIES[requiredPermission],
checkGranular
);
return permissions.includes(requiredPermission);
};

const doSystemRoleCheck = (
Expand Down
Loading