feat: add Workflow resource to PBAC system with permission enforcement (#22845)
Co-authored-by: [email protected] <[email protected]> Co-authored-by: Carina Wollendorfer <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: CarinaWolli <[email protected]>
This commit is contained in:
18 files changed
+820
-139
No files matched your search
@@ -1,5 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import type { PermissionString } from "@calcom/features/pbac/domain/types/permission-registry";
|
||||
import type { MembershipRole } from "@calcom/prisma/enums";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
|
||||
import type { CreateBtnProps, Option } from "./CreateButton";
|
||||
@@ -11,10 +13,20 @@ export function CreateButtonWithTeamsList(
|
||||
onlyShowWithNoTeams?: boolean;
|
||||
isAdmin?: boolean;
|
||||
includeOrg?: boolean;
|
||||
withPermission?: {
|
||||
permission: PermissionString;
|
||||
fallbackRoles?: MembershipRole[];
|
||||
};
|
||||
}
|
||||
) {
|
||||
const query = trpc.viewer.loggedInViewerRouter.teamsAndUserProfilesQuery.useQuery({
|
||||
includeOrg: props.includeOrg,
|
||||
withPermission: props.withPermission
|
||||
? {
|
||||
permission: props.withPermission.permission,
|
||||
fallbackRoles: props.withPermission.fallbackRoles,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
if (!query.data) return null;
|
||||
|
||||
|
||||
@@ -23,6 +23,14 @@ import WorkflowStepContainer from "./WorkflowStepContainer";
|
||||
|
||||
type User = RouterOutputs["viewer"]["me"]["get"];
|
||||
|
||||
interface WorkflowPermissions {
|
||||
canView: boolean;
|
||||
canUpdate: boolean;
|
||||
canDelete: boolean;
|
||||
canManage: boolean;
|
||||
readOnly: boolean; // Keep for backward compatibility
|
||||
}
|
||||
|
||||
interface Props {
|
||||
form: UseFormReturn<FormValues>;
|
||||
workflowId: number;
|
||||
@@ -33,13 +41,31 @@ interface Props {
|
||||
readOnly: boolean;
|
||||
isOrg: boolean;
|
||||
allOptions: Option[];
|
||||
permissions?: WorkflowPermissions;
|
||||
}
|
||||
|
||||
export default function WorkflowDetailsPage(props: Props) {
|
||||
const { form, workflowId, selectedOptions, setSelectedOptions, teamId, isOrg, allOptions } = props;
|
||||
const {
|
||||
form,
|
||||
workflowId,
|
||||
selectedOptions,
|
||||
setSelectedOptions,
|
||||
teamId,
|
||||
isOrg,
|
||||
allOptions,
|
||||
permissions: _permissions,
|
||||
} = props;
|
||||
const { t } = useLocale();
|
||||
const router = useRouter();
|
||||
|
||||
const permissions = _permissions || {
|
||||
canView: !teamId ? true : !props.readOnly,
|
||||
canUpdate: !teamId ? true : !props.readOnly,
|
||||
canDelete: !teamId ? true : !props.readOnly,
|
||||
canManage: !teamId ? true : !props.readOnly,
|
||||
readOnly: !teamId ? false : props.readOnly,
|
||||
};
|
||||
|
||||
const [isAddActionDialogOpen, setIsAddActionDialogOpen] = useState(false);
|
||||
|
||||
const [reload, setReload] = useState(false);
|
||||
@@ -160,7 +186,7 @@ export default function WorkflowDetailsPage(props: Props) {
|
||||
/>
|
||||
</div>
|
||||
<div className="md:border-subtle my-7 border-transparent md:border-t" />
|
||||
{!props.readOnly && (
|
||||
{permissions.canDelete && (
|
||||
<Button
|
||||
type="button"
|
||||
StartIcon="trash-2"
|
||||
|
||||
@@ -6,6 +6,7 @@ import { useState } from "react";
|
||||
|
||||
import { getPlaceholderAvatar } from "@calcom/lib/defaultAvatarImage";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import type { WorkflowPermissions } from "@calcom/lib/server/repository/workflow-permissions";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import classNames from "@calcom/ui/classNames";
|
||||
import { ArrowButton } from "@calcom/ui/components/arrow-button";
|
||||
@@ -52,7 +53,8 @@ export type WorkflowType = Workflow & {
|
||||
};
|
||||
};
|
||||
}[];
|
||||
readOnly?: boolean;
|
||||
readOnly?: boolean; // Keep for backward compatibility
|
||||
permissions?: WorkflowPermissions;
|
||||
isOrg?: boolean;
|
||||
};
|
||||
interface Props {
|
||||
@@ -136,7 +138,7 @@ export default function WorkflowListPage({ workflows }: Props) {
|
||||
: "Untitled"}
|
||||
</div>
|
||||
<div>
|
||||
{workflow.readOnly && (
|
||||
{(workflow.permissions?.readOnly ?? workflow.readOnly) && (
|
||||
<Badge variant="gray" className="ml-2 ">
|
||||
{t("readonly")}
|
||||
</Badge>
|
||||
@@ -257,7 +259,9 @@ export default function WorkflowListPage({ workflows }: Props) {
|
||||
color="secondary"
|
||||
variant="icon"
|
||||
StartIcon="pencil"
|
||||
disabled={workflow.readOnly}
|
||||
disabled={
|
||||
workflow.permissions ? !workflow.permissions.canUpdate : workflow.readOnly
|
||||
}
|
||||
onClick={async () => await router.replace(`/workflows/${workflow.id}`)}
|
||||
data-testid="edit-button"
|
||||
/>
|
||||
@@ -270,40 +274,52 @@ export default function WorkflowListPage({ workflows }: Props) {
|
||||
}}
|
||||
color="secondary"
|
||||
variant="icon"
|
||||
disabled={workflow.readOnly}
|
||||
disabled={
|
||||
workflow.permissions ? !workflow.permissions.canDelete : workflow.readOnly
|
||||
}
|
||||
StartIcon="trash-2"
|
||||
data-testid="delete-button"
|
||||
/>
|
||||
</Tooltip>
|
||||
</ButtonGroup>
|
||||
</div>
|
||||
{!workflow.readOnly && (
|
||||
{(workflow.permissions?.canUpdate ||
|
||||
workflow.permissions?.canDelete ||
|
||||
(!workflow.permissions && !workflow.readOnly)) && (
|
||||
<div className="block sm:hidden">
|
||||
<Dropdown>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<Button type="button" color="minimal" variant="icon" StartIcon="ellipsis" />
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent>
|
||||
<DropdownMenuItem>
|
||||
<DropdownItem
|
||||
type="button"
|
||||
StartIcon="pencil"
|
||||
onClick={async () => await router.replace(`/workflows/${workflow.id}`)}>
|
||||
{t("edit")}
|
||||
</DropdownItem>
|
||||
</DropdownMenuItem>
|
||||
<DropdownMenuItem>
|
||||
<DropdownItem
|
||||
type="button"
|
||||
color="destructive"
|
||||
StartIcon="trash-2"
|
||||
onClick={() => {
|
||||
setDeleteDialogOpen(true);
|
||||
setwWorkflowToDeleteId(workflow.id);
|
||||
}}>
|
||||
{t("delete")}
|
||||
</DropdownItem>
|
||||
</DropdownMenuItem>
|
||||
{(workflow.permissions
|
||||
? workflow.permissions.canUpdate
|
||||
: !workflow.readOnly) && (
|
||||
<DropdownMenuItem>
|
||||
<DropdownItem
|
||||
type="button"
|
||||
StartIcon="pencil"
|
||||
onClick={async () => await router.replace(`/workflows/${workflow.id}`)}>
|
||||
{t("edit")}
|
||||
</DropdownItem>
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
{(workflow.permissions
|
||||
? workflow.permissions.canDelete
|
||||
: !workflow.readOnly) && (
|
||||
<DropdownMenuItem>
|
||||
<DropdownItem
|
||||
type="button"
|
||||
color="destructive"
|
||||
StartIcon="trash-2"
|
||||
onClick={() => {
|
||||
setDeleteDialogOpen(true);
|
||||
setwWorkflowToDeleteId(workflow.id);
|
||||
}}>
|
||||
{t("delete")}
|
||||
</DropdownItem>
|
||||
</DropdownMenuItem>
|
||||
)}
|
||||
</DropdownMenuContent>
|
||||
</Dropdown>
|
||||
</div>
|
||||
|
||||
@@ -84,6 +84,10 @@ function WorkflowsPage({ filteredList }: PageProps) {
|
||||
disableMobileButton={true}
|
||||
onlyShowWithNoTeams={true}
|
||||
includeOrg={true}
|
||||
withPermission={{
|
||||
permission: "workflow.create",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
}}
|
||||
/>
|
||||
) : null
|
||||
}>
|
||||
@@ -99,6 +103,10 @@ function WorkflowsPage({ filteredList }: PageProps) {
|
||||
disableMobileButton={true}
|
||||
onlyShowWithTeams={true}
|
||||
includeOrg={true}
|
||||
withPermission={{
|
||||
permission: "workflow.create",
|
||||
fallbackRoles: ["ADMIN", "OWNER"],
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -10,9 +10,8 @@ import Shell, { ShellMain } from "@calcom/features/shell/Shell";
|
||||
import { SENDER_ID } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { HttpError } from "@calcom/lib/http-error";
|
||||
import type { WorkflowRepository } from "@calcom/lib/server/repository/workflow";
|
||||
import type { TimeUnit, WorkflowTriggerEvents } from "@calcom/prisma/enums";
|
||||
import { MembershipRole, WorkflowActions } from "@calcom/prisma/enums";
|
||||
import { WorkflowActions } from "@calcom/prisma/enums";
|
||||
import type { RouterOutputs } from "@calcom/trpc/react";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import useMeQuery from "@calcom/trpc/react/hooks/useMeQuery";
|
||||
@@ -43,17 +42,9 @@ export type FormValues = {
|
||||
|
||||
type PageProps = {
|
||||
workflow: number;
|
||||
workflowData?: Awaited<ReturnType<typeof WorkflowRepository.getById>>;
|
||||
verifiedNumbers?: Awaited<ReturnType<typeof WorkflowRepository.getVerifiedNumbers>>;
|
||||
verifiedEmails?: Awaited<ReturnType<typeof WorkflowRepository.getVerifiedEmails>>;
|
||||
};
|
||||
|
||||
function WorkflowPage({
|
||||
workflow: workflowId,
|
||||
workflowData: workflowDataProp,
|
||||
verifiedNumbers: verifiedNumbersProp,
|
||||
verifiedEmails: verifiedEmailsProp,
|
||||
}: PageProps) {
|
||||
function WorkflowPage({ workflow: workflowId }: PageProps) {
|
||||
const { t, i18n } = useLocale();
|
||||
const session = useSession();
|
||||
|
||||
@@ -73,35 +64,23 @@ function WorkflowPage({
|
||||
|
||||
const {
|
||||
data: workflowData,
|
||||
isError: _isError,
|
||||
isError,
|
||||
error,
|
||||
isPending: _isPendingWorkflow,
|
||||
} = trpc.viewer.workflows.get.useQuery(
|
||||
{ id: +workflowId },
|
||||
{
|
||||
enabled: workflowDataProp ? false : !!workflowId,
|
||||
}
|
||||
);
|
||||
isPending: isPendingWorkflow,
|
||||
} = trpc.viewer.workflows.get.useQuery({ id: +workflowId });
|
||||
|
||||
const workflow = workflowDataProp || workflowData;
|
||||
const isPendingWorkflow = workflowDataProp ? false : _isPendingWorkflow;
|
||||
const isError = workflowDataProp ? false : _isError;
|
||||
const workflow = workflowData;
|
||||
|
||||
const { data: verifiedNumbersData } = trpc.viewer.workflows.getVerifiedNumbers.useQuery(
|
||||
const { data: verifiedNumbers } = trpc.viewer.workflows.getVerifiedNumbers.useQuery(
|
||||
{ teamId: workflow?.team?.id },
|
||||
{
|
||||
enabled: verifiedNumbersProp ? false : !!workflow?.id,
|
||||
enabled: !!workflow?.id,
|
||||
}
|
||||
);
|
||||
const verifiedNumbers = verifiedNumbersProp || verifiedNumbersData;
|
||||
|
||||
const { data: verifiedEmailsData } = trpc.viewer.workflows.getVerifiedEmails.useQuery(
|
||||
{
|
||||
teamId: workflow?.team?.id,
|
||||
},
|
||||
{ enabled: !verifiedEmailsProp }
|
||||
);
|
||||
const verifiedEmails = verifiedEmailsProp || verifiedEmailsData;
|
||||
const { data: verifiedEmails } = trpc.viewer.workflows.getVerifiedEmails.useQuery({
|
||||
teamId: workflow?.team?.id,
|
||||
});
|
||||
|
||||
const isOrg = workflow?.team?.isOrganization ?? false;
|
||||
|
||||
@@ -126,9 +105,7 @@ function WorkflowPage({
|
||||
});
|
||||
}
|
||||
|
||||
const readOnly =
|
||||
workflow?.team?.members?.find((member) => member.userId === session.data?.user.id)?.role ===
|
||||
MembershipRole.MEMBER;
|
||||
const readOnly = !workflow?.permissions.canUpdate;
|
||||
|
||||
const isPending = isPendingWorkflow || isPendingEventTypes;
|
||||
|
||||
@@ -210,8 +187,8 @@ function WorkflowPage({
|
||||
const updateMutation = trpc.viewer.workflows.update.useMutation({
|
||||
onSuccess: async ({ workflow }) => {
|
||||
if (workflow) {
|
||||
utils.viewer.workflows.get.setData({ id: +workflow.id }, workflow);
|
||||
setFormData(workflow);
|
||||
await utils.viewer.workflows.get.invalidate({ id: +workflow.id });
|
||||
|
||||
showToast(
|
||||
t("workflow_updated_successfully", {
|
||||
workflowName: workflow.name,
|
||||
@@ -348,6 +325,7 @@ function WorkflowPage({
|
||||
{isAllDataLoaded && user ? (
|
||||
<>
|
||||
<WorkflowDetailsPage
|
||||
permissions={workflow?.permissions}
|
||||
form={form}
|
||||
workflowId={+workflowId}
|
||||
user={user}
|
||||
|
||||
@@ -7,6 +7,7 @@ export enum Resource {
|
||||
Booking = "booking",
|
||||
Insights = "insights",
|
||||
Role = "role",
|
||||
Workflow = "workflow",
|
||||
}
|
||||
|
||||
export enum CrudAction {
|
||||
@@ -355,6 +356,42 @@ export const PERMISSION_REGISTRY: PermissionRegistry = {
|
||||
descriptionI18nKey: "pbac_desc_view_team_insights",
|
||||
},
|
||||
},
|
||||
[Resource.Workflow]: {
|
||||
_resource: {
|
||||
i18nKey: "pbac_resource_workflow",
|
||||
},
|
||||
[CrudAction.Create]: {
|
||||
description: "Create workflows",
|
||||
category: "workflow",
|
||||
i18nKey: "pbac_action_create",
|
||||
descriptionI18nKey: "pbac_desc_create_workflows",
|
||||
},
|
||||
[CrudAction.Read]: {
|
||||
description: "View workflows",
|
||||
category: "workflow",
|
||||
i18nKey: "pbac_action_read",
|
||||
descriptionI18nKey: "pbac_desc_view_workflows",
|
||||
},
|
||||
[CrudAction.Update]: {
|
||||
description: "Update workflows",
|
||||
category: "workflow",
|
||||
i18nKey: "pbac_action_update",
|
||||
descriptionI18nKey: "pbac_desc_update_workflows",
|
||||
},
|
||||
[CrudAction.Delete]: {
|
||||
description: "Delete workflows",
|
||||
category: "workflow",
|
||||
i18nKey: "pbac_action_delete",
|
||||
descriptionI18nKey: "pbac_desc_delete_workflows",
|
||||
},
|
||||
[CrudAction.Manage]: {
|
||||
description: "Manage workflows",
|
||||
category: "workflow",
|
||||
i18nKey: "pbac_action_manage",
|
||||
descriptionI18nKey: "pbac_desc_manage_workflows",
|
||||
scope: [Scope.Organization],
|
||||
},
|
||||
},
|
||||
[Resource.Attributes]: {
|
||||
_resource: {
|
||||
i18nKey: "pbac_resource_attributes",
|
||||
|
||||
Reference in new issue
Block a user