From 98893a40f140ab412be6195031fa6f631feb79e1 Mon Sep 17 00:00:00 2001 From: Akhil Mohan Date: Wed, 6 Sep 2023 17:06:28 +0530 Subject: [PATCH] feat(rbac): made changes from testing with maidul --- .../src/controllers/v1/workspaceController.ts | 19 +- backend/src/routes/v1/workspace.ts | 8 + .../permissions/OrgPermissionCan.tsx | 10 + .../permissions/ProjectPermissionCan.tsx | 10 + .../src/components/v2/Checkbox/Checkbox.tsx | 2 +- frontend/src/hooks/api/roles/types.ts | 36 +- frontend/src/layouts/AppLayout/AppLayout.tsx | 48 +- frontend/src/pages/org/[id]/billing/index.tsx | 2 +- frontend/src/styles/globals.css | 12 +- .../src/views/DashboardPage/DashboardPage.tsx | 1 - .../SecretDropzone/SecretDropzone.tsx | 668 +++++++++--------- .../OrgRoleModifySection.utils.ts | 60 +- .../SimpleLevelPermissionOptions.tsx | 29 +- .../WorkspacePermission.tsx | 4 +- .../OrgRoleTabSection/OrgRoleTable.tsx | 73 +- .../ProjectRoleList/ProjectRoleList.tsx | 51 +- .../ProjectRoleModifySection.utils.ts | 98 ++- .../WsProjectPermission.tsx | 8 +- .../OrgGeneralTab/OrgGeneralTab.tsx | 2 - 19 files changed, 612 insertions(+), 529 deletions(-) diff --git a/backend/src/controllers/v1/workspaceController.ts b/backend/src/controllers/v1/workspaceController.ts index 1b5787671..6c017d6be 100644 --- a/backend/src/controllers/v1/workspaceController.ts +++ b/backend/src/controllers/v1/workspaceController.ts @@ -191,7 +191,15 @@ export const createWorkspace = async (req: Request, res: Response) => { * @returns */ export const deleteWorkspace = async (req: Request, res: Response) => { - const { workspaceId } = req.params; + const { + params: { workspaceId } + } = await validateRequest(reqValidator.DeleteWorkspaceV1, req); + + const { permission } = await getUserProjectPermissions(req.user._id, workspaceId); + ForbiddenError.from(permission).throwUnlessCan( + ProjectPermissionActions.Delete, + ProjectPermissionSub.Workspace + ); // delete workspace await deleteWork({ @@ -246,7 +254,14 @@ export const changeWorkspaceName = async (req: Request, res: Response) => { * @returns */ export const getWorkspaceIntegrations = async (req: Request, res: Response) => { - const { workspaceId } = req.params; + const { + params: { workspaceId } + } = await validateRequest(reqValidator.GetWorkspaceIntegrationsV1, req); + const { permission } = await getUserProjectPermissions(req.user._id, workspaceId); + ForbiddenError.from(permission).throwUnlessCan( + ProjectPermissionActions.Read, + ProjectPermissionSub.Integrations + ); const integrations = await Integration.find({ workspace: workspaceId diff --git a/backend/src/routes/v1/workspace.ts b/backend/src/routes/v1/workspace.ts index b6a28819a..4dbc121b9 100644 --- a/backend/src/routes/v1/workspace.ts +++ b/backend/src/routes/v1/workspace.ts @@ -44,6 +44,14 @@ router.post( workspaceController.createWorkspace ); +router.delete( + "/:workspaceId", + requireAuth({ + acceptedAuthModes: [AuthMode.JWT] + }), + workspaceController.deleteWorkspace +); + router.post( "/:workspaceId/name", requireAuth({ diff --git a/frontend/src/components/permissions/OrgPermissionCan.tsx b/frontend/src/components/permissions/OrgPermissionCan.tsx index 679fe1069..8d1f3fb7b 100644 --- a/frontend/src/components/permissions/OrgPermissionCan.tsx +++ b/frontend/src/components/permissions/OrgPermissionCan.tsx @@ -7,12 +7,18 @@ import { Tooltip } from "../v2"; type Props = { label?: ReactNode; + // this prop is used when there exist already a tooltip as helper text for users + // so when permission is allowed same tooltip will be reused to show helpertext + renderTooltip?: boolean; + allowedLabel?: string; } & BoundCanProps; export const OrgPermissionCan: FunctionComponent = ({ label = "Permission Denied. Kindly contact your org admin", children, passThrough = true, + renderTooltip, + allowedLabel, ...props }) => { const permission = useOrgPermission(); @@ -30,6 +36,10 @@ export const OrgPermissionCan: FunctionComponent = ({ return {finalChild}; } + if (isAllowed && renderTooltip) { + return {finalChild}; + } + if (!isAllowed) return null; return finalChild; diff --git a/frontend/src/components/permissions/ProjectPermissionCan.tsx b/frontend/src/components/permissions/ProjectPermissionCan.tsx index 2371450fb..6c76afde9 100644 --- a/frontend/src/components/permissions/ProjectPermissionCan.tsx +++ b/frontend/src/components/permissions/ProjectPermissionCan.tsx @@ -7,12 +7,18 @@ import { Tooltip } from "../v2"; type Props = { label?: ReactNode; + // this prop is used when there exist already a tooltip as helper text for users + // so when permission is allowed same tooltip will be reused to show helpertext + renderTooltip?: boolean; + allowedLabel?: string; } & BoundCanProps; export const ProjectPermissionCan: FunctionComponent = ({ label = "Permission Denied. Kindly contact your project admin", children, passThrough = true, + renderTooltip, + allowedLabel, ...props }) => { const permission = useProjectPermission(); @@ -30,6 +36,10 @@ export const ProjectPermissionCan: FunctionComponent = ({ return {finalChild}; } + if (isAllowed && renderTooltip) { + return {finalChild}; + } + if (!isAllowed) return null; return finalChild; diff --git a/frontend/src/components/v2/Checkbox/Checkbox.tsx b/frontend/src/components/v2/Checkbox/Checkbox.tsx index 64ec0a54a..8c79a5ff2 100644 --- a/frontend/src/components/v2/Checkbox/Checkbox.tsx +++ b/frontend/src/components/v2/Checkbox/Checkbox.tsx @@ -45,7 +45,7 @@ export const Checkbox = ({ -