π OpenHands: AI-Driven Development
feat: split automation permissions into view/manage with creator escape hatch (#17105)
Tim O'Farrell5e3bd0147f14
diff --git a/__tests__/components/automations/automation-card.test.tsx b/__tests__/components/automations/automation-card.test.tsxindex 1961bdc6b5..5f6a03118a 100644--- a/__tests__/components/automations/automation-card.test.tsx+++ b/__tests__/components/automations/automation-card.test.tsx@@ -32,8 +32,13 @@ vi.mock("#/context/navigation-context", () => ({useNavigation: () => ({ navigate: vi.fn(), currentPath: "/" }),}));-vi.mock("#/hooks/use-has-permission", () => ({- useHasPermission: () => true,+vi.mock("#/hooks/use-automation-permissions", () => ({+ useAutomationPermissions: () => ({+ canView: true,+ canManage: true,+ isLoading: false,+ }),+ useIsAutomationOwner: () => true,}));// The pinned package predates the `impact` field, so an entry carrying one isdiff --git a/__tests__/components/automations/automation-list-row.test.tsx b/__tests__/components/automations/automation-list-row.test.tsxindex 7f1c56756a..e2ff941fd4 100644--- a/__tests__/components/automations/automation-list-row.test.tsx+++ b/__tests__/components/automations/automation-list-row.test.tsx@@ -20,8 +20,13 @@ vi.mock("#/context/navigation-context", () => ({useNavigation: () => ({ navigate: vi.fn(), currentPath: "/" }),}));-vi.mock("#/hooks/use-has-permission", () => ({- useHasPermission: () => true,+vi.mock("#/hooks/use-automation-permissions", () => ({+ useAutomationPermissions: () => ({+ canView: true,+ canManage: true,+ isLoading: false,+ }),+ useIsAutomationOwner: () => true,}));// The pinned package predates the `impact` field, so an entry carrying one isdiff --git a/__tests__/components/features/home/featured-automations-section.test.tsx b/__tests__/components/features/home/featured-automations-section.test.tsxindex c14d8011e7..93b55fec6e 100644--- a/__tests__/components/features/home/featured-automations-section.test.tsx+++ b/__tests__/components/features/home/featured-automations-section.test.tsx@@ -46,6 +46,17 @@ vi.mock("#/utils/custom-toast-handlers", () => ({displayErrorToast: vi.fn(),}));+// Mock permission hooks so home automation components don't need a real+// ActiveBackendProvider or /me endpoint.+vi.mock("#/hooks/use-automation-permissions", () => ({+ useAutomationPermissions: () => ({+ canView: true,+ canManage: true,+ isLoading: false,+ }),+ useIsAutomationOwner: () => true,+}));+vi.mock("#/api/conversation-service/agent-server-conversation-service.api",() => ({diff --git a/__tests__/routes/automation-detail.test.tsx b/__tests__/routes/automation-detail.test.tsxindex ecdf1e1dad..0adf6db235 100644--- a/__tests__/routes/automation-detail.test.tsx+++ b/__tests__/routes/automation-detail.test.tsx@@ -31,6 +31,16 @@ vi.mock("#/api/automation-service/automation-service.api", () => ({},}));+// Mock permission hooks so cloud-backend tests don't need a real /me endpoint.+vi.mock("#/hooks/use-automation-permissions", () => ({+ useAutomationPermissions: () => ({+ canView: true,+ canManage: true,+ isLoading: false,+ }),+ useIsAutomationOwner: () => true,+}));+const localBackend: Backend = {id: "local-1",name: "Local 1",diff --git a/__tests__/routes/automations-list.test.tsx b/__tests__/routes/automations-list.test.tsxindex 99293ac506..63975621b8 100644--- a/__tests__/routes/automations-list.test.tsx+++ b/__tests__/routes/automations-list.test.tsx@@ -40,6 +40,16 @@ vi.mock("#/utils/custom-toast-handlers", () => ({displayErrorToast: vi.fn(),}));+// Mock permission hooks so cloud-backend tests don't need a real /me endpoint.+vi.mock("#/hooks/use-automation-permissions", () => ({+ useAutomationPermissions: () => ({+ canView: true,+ canManage: true,+ isLoading: false,+ }),+ useIsAutomationOwner: () => true,+}));+const localBackend: Backend = {id: "local-1",name: "Local 1",diff --git a/src/components/features/automations/automation-card.tsx b/src/components/features/automations/automation-card.tsxindex 3a1ff2ee75..2cd1b84926 100644--- a/src/components/features/automations/automation-card.tsx+++ b/src/components/features/automations/automation-card.tsx@@ -5,7 +5,10 @@ import { I18nKey } from "#/i18n/declaration";import type { Automation } from "#/types/automation";import { getAutomationRunDisplay } from "#/utils/automation-run-display";import { KebabMenu } from "./kebab-menu";-import { useHasPermission } from "#/hooks/use-has-permission";+import {+ useAutomationPermissions,+ useIsAutomationOwner,+} from "#/hooks/use-automation-permissions";import { useNavigation } from "#/context/navigation-context";import PlayIcon from "#/icons/play.svg?react";import { SkillCardPillRow } from "#/components/features/skills/skill-card-pill-row";@@ -63,7 +66,10 @@ export function AutomationCard({}: AutomationCardProps) {const { navigate } = useNavigation();const { t, i18n } = useTranslation("openhands");- const canManage = useHasPermission("manage_automations");+ const { canManage: hasManagePermission } = useAutomationPermissions();+ const isOwner = useIsAutomationOwner(automation);+ // Write actions on a specific automation: manage OR creator (escape hatch).+ const canManage = hasManagePermission || isOwner;const scheduleLabel =automation.trigger.schedule_human || automation.trigger.type;diff --git a/src/components/features/automations/automation-list-row.tsx b/src/components/features/automations/automation-list-row.tsxindex a0fb8f6397..92449d188f 100644--- a/src/components/features/automations/automation-list-row.tsx+++ b/src/components/features/automations/automation-list-row.tsx@@ -5,7 +5,10 @@ import { I18nKey } from "#/i18n/declaration";import type { Automation } from "#/types/automation";import { getAutomationRunDisplay } from "#/utils/automation-run-display";import { KebabMenu } from "./kebab-menu";-import { useHasPermission } from "#/hooks/use-has-permission";+import {+ useAutomationPermissions,+ useIsAutomationOwner,+} from "#/hooks/use-automation-permissions";import { useNavigation } from "#/context/navigation-context";import { NavigationLink } from "#/components/shared/navigation-link";import PlayIcon from "#/icons/play.svg?react";@@ -59,7 +62,10 @@ export function AutomationListRow({}: AutomationListRowProps) {const { navigate } = useNavigation();const { t, i18n } = useTranslation("openhands");- const canManage = useHasPermission("manage_automations");+ const { canManage: hasManagePermission } = useAutomationPermissions();+ const isOwner = useIsAutomationOwner(automation);+ // Write actions on a specific automation: manage OR creator (escape hatch).+ const canManage = hasManagePermission || isOwner;const handleView = () => {navigate?.(`/automations/${automation.id}`);diff --git a/src/components/features/automations/detail/detail-header.tsx b/src/components/features/automations/detail/detail-header.tsxindex 1d85736b94..a40feaef73 100644--- a/src/components/features/automations/detail/detail-header.tsx+++ b/src/components/features/automations/detail/detail-header.tsx@@ -8,7 +8,6 @@ import DownloadIcon from "#/icons/download.svg?react";import TrashIcon from "#/icons/trash.svg?react";import EditIcon from "#/icons/u-edit.svg?react";import PlayIcon from "#/icons/play.svg?react";-import { useHasPermission } from "#/hooks/use-has-permission";import { ActiveStatusBadge } from "./active-status-badge";interface DetailHeaderProps {@@ -24,6 +23,8 @@ interface DetailHeaderProps {onDownloadTarball: () => void;onRunNow?: () => void;isRunningNow?: boolean;+ /** Whether the caller may mutate this automation (manage perm or owner). */+ canManage?: boolean;}export function DetailHeader({@@ -35,27 +36,12 @@ export function DetailHeader({onDownloadTarball,onRunNow,isRunningNow = false,+ canManage = true,}: DetailHeaderProps) {const { t } = useTranslation("openhands");- const canManage = useHasPermission("manage_automations");const kebabItems = [- ...(onEdit- ? [- {- label: t(I18nKey.AUTOMATIONS$EDIT),- icon: <EditIcon className="size-4" />,- onClick: onEdit,- },- ]- : []),- {- label: automation.enabled- ? t(I18nKey.AUTOMATIONS$TURN_OFF)- : t(I18nKey.AUTOMATIONS$TURN_ON),- icon: <PowerIcon className="size-4" />,- onClick: onToggle,- },+ // Read-only items are always available to anyone who can view.{label: t(I18nKey.AUTOMATIONS$EXPORT),icon: <DownloadIcon className="size-4" />,@@ -66,11 +52,32 @@ export function DetailHeader({icon: <DownloadIcon className="size-4" />,onClick: onDownloadTarball,},- {- label: t(I18nKey.AUTOMATIONS$DELETE),- icon: <TrashIcon className="size-4" />,- onClick: onDelete,- },+ // Write items β only when the user may mutate this automation.+ ...(canManage && onEdit+ ? [+ {+ label: t(I18nKey.AUTOMATIONS$EDIT),+ icon: <EditIcon className="size-4" />,+ onClick: onEdit,+ },+ ]+ : []),+ ...(canManage+ ? [+ {+ label: automation.enabled+ ? t(I18nKey.AUTOMATIONS$TURN_OFF)+ : t(I18nKey.AUTOMATIONS$TURN_ON),+ icon: <PowerIcon className="size-4" />,+ onClick: onToggle,+ },+ {+ label: t(I18nKey.AUTOMATIONS$DELETE),+ icon: <TrashIcon className="size-4" />,+ onClick: onDelete,+ },+ ]+ : []),];return (@@ -107,7 +114,7 @@ export function DetailHeader({onToggle={onToggle}/>)}- {canManage && <KebabMenu items={kebabItems} />}+ <KebabMenu items={kebabItems} /></div></div></div>diff --git a/src/hooks/use-automation-permissions.ts b/src/hooks/use-automation-permissions.tsnew file mode 100644index 0000000000..d189ced0a9--- /dev/null+++ b/src/hooks/use-automation-permissions.ts@@ -0,0 +1,102 @@+import { useQuery } from "@tanstack/react-query";+import { getCloudOrganizationMe } from "#/api/cloud/organization-service.api";+import { useActiveBackend } from "#/contexts/active-backend-context";+import { useCloudCurrentUserId } from "./query/use-cloud-current-user-id";+import type { Automation } from "#/types/automation";++/**+ * Server-defined permission strings (mirror the automation service's+ * `view_automations` / `manage_automations` and the enterprise+ * `authorization.Permission` enum). The `/me` endpoint returns whichever+ * subset the caller's role grants.+ */+export const VIEW_AUTOMATIONS = "view_automations";+export const MANAGE_AUTOMATIONS = "manage_automations";++export interface AutomationPermissionsResult {+ /** Read-only access (list, get, list runs, capabilities, git-sync status). */+ canView: boolean;+ /** Full write access (create, update, delete, dispatch, git-sync config). */+ canManage: boolean;+ /**+ * `true` when permissions are still being resolved on cloud. Local always+ * resolves synchronously. Mutating controls should stay hidden while this is+ * `true` so a member never sees a flash of write controls before the+ * server-defined permission set arrives.+ */+ isLoading: boolean;+}++/**+ * Resolve the caller's automation-level permissions on the active backend.+ *+ * - Local agent-server (OSS): always `{ canView: true, canManage: true }`.+ * - Cloud: reads the server-defined `permissions` array from+ * `GET /api/organizations/{orgId}/me` (same query `useCloudCurrentUserId`+ * and `useCanManageOrgProfiles` use, so no extra request is issued) and+ * falls back to a role check (`owner`/`admin` β both, `member` β view+ * only) when an older app-server doesn't return `permissions`.+ */+export function useAutomationPermissions(): AutomationPermissionsResult {+ const { backend, orgId } = useActiveBackend();+ const isCloud = backend.kind === "cloud";++ // eslint-disable-next-line @tanstack/query/exhaustive-deps+ const { data, isLoading } = useQuery({+ queryKey: [+ "cloud-current-user",+ backend.id,+ orgId,+ backend.connectionRevision ?? 0,+ ],+ queryFn: () => getCloudOrganizationMe(orgId!, backend),+ enabled: isCloud && !!orgId,+ staleTime: 1000 * 60 * 5,+ retry: false,+ meta: { disableToast: true },+ });++ if (!isCloud) {+ return { canView: true, canManage: true, isLoading: false };+ }++ if (isLoading || !data) {+ return { canView: false, canManage: false, isLoading };+ }++ if (data.permissions) {+ const canView = data.permissions.includes(VIEW_AUTOMATIONS);+ const canManage = data.permissions.includes(MANAGE_AUTOMATIONS);+ return { canView, canManage, isLoading: false };+ }++ // Fall back to role check for older app-servers without `permissions`.+ const isOwnerOrAdmin = data.role === "owner" || data.role === "admin";+ return {+ canView: isOwnerOrAdmin || data.role === "member",+ canManage: isOwnerOrAdmin,+ isLoading: false,+ };+}++/**+ * Resolve whether the current user is the creator of the given automation.+ *+ * - Local: always `true` (the local user owns every automation; and since+ * local always has `manage_automations`, the distinction is moot).+ * - Cloud: compares `automation.user_id` with the caller's `user_id` from+ * `GET /api/organizations/{orgId}/me`. Returns `false` while the id is+ * still loading so the creator escape hatch never flashes open prematurely.+ */+export function useIsAutomationOwner(automation: Automation): boolean {+ const { backend } = useActiveBackend();+ // Always call the hook so rules-of-hooks is satisfied; it's a no-op for+ // local backends since the early return below short-circuits before use.+ const userIds = useCloudCurrentUserId();++ if (backend.kind !== "cloud") return true;++ const entry = userIds[backend.id];+ if (!entry || entry.isLoading || !entry.userId) return false;+ return automation.user_id === entry.userId;+}diff --git a/src/hooks/use-has-permission.ts b/src/hooks/use-has-permission.tsdeleted file mode 100644index f631b04b0c..0000000000--- a/src/hooks/use-has-permission.ts+++ /dev/null@@ -1,7 +0,0 @@-// Simple permission hook for OSS agent-canvas-// In the OSS context, all authenticated users have full access-// eslint-disable-next-line @typescript-eslint/no-unused-vars-export function useHasPermission(permission: string): boolean {- // In OSS mode, every permission string is granted.- return true;-}diff --git a/src/hooks/use-home-automation-actions.ts b/src/hooks/use-home-automation-actions.tsindex 05c49bbe48..93a362c8fd 100644--- a/src/hooks/use-home-automation-actions.ts+++ b/src/hooks/use-home-automation-actions.ts@@ -7,7 +7,10 @@ import {useDispatchAutomation,useToggleAutomation,} from "#/hooks/query/use-automations";-import { useHasPermission } from "#/hooks/use-has-permission";+import {+ useAutomationPermissions,+ useIsAutomationOwner,+} from "#/hooks/use-automation-permissions";import { isHomeAutomationsDemoEnabled } from "#/fixtures/home-automations-demo";import { I18nKey } from "#/i18n/declaration";import {@@ -41,7 +44,11 @@ export function useHomeAutomationActions(const { t } = useTranslation("openhands");const { navigate } = useNavigation();const active = useActiveBackend();- const canManage = useHasPermission("manage_automations");+ const { canManage: hasManagePermission } = useAutomationPermissions();+ const isOwner = useIsAutomationOwner(automation);+ // Write actions on a specific automation are allowed when the user has+ // manage permission OR is the automation's creator (creator escape hatch).+ const canManage = hasManagePermission || isOwner;const canEdit = active.backend.kind === "local";const isDemo = isHomeAutomationsDemoEnabled();diff --git a/src/routes/automation-detail.tsx b/src/routes/automation-detail.tsxindex c152624af9..c26b57560c 100644--- a/src/routes/automation-detail.tsx+++ b/src/routes/automation-detail.tsx@@ -35,13 +35,33 @@ import { BackendNotConfigured } from "#/components/features/automations/backend-import { DeleteConfirmationModal } from "#/components/features/automations/delete-confirmation-modal";import { EditAutomationModal } from "#/components/features/automations/detail/edit-automation-modal";import { useTracking } from "#/hooks/use-tracking";+import {+ useAutomationPermissions,+ useIsAutomationOwner,+} from "#/hooks/use-automation-permissions";import AutomationService from "#/api/automation-service/automation-service.api";+import type { Automation } from "#/types/automation";import {getAutomationExportFilename,serializeAutomation,} from "#/utils/automation-export";import { downloadBlob } from "#/utils/utils";+/**+ * Placeholder automation used to keep `useIsAutomationOwner` hooked before+ * the real automation has loaded (rules-of-hooks). Its `user_id` won't match+ * any real user, so the owner check safely returns `false` while loading.+ */+const nullAutomation: Automation = {+ id: "",+ name: "",+ prompt: null,+ trigger: { type: "cron" },+ enabled: false,+ created_at: "",+ updated_at: "",+};+/*** The page renders the interface manifest's copy, so without an admitted* manifest there is nothing to render: a 404, which the layout's error@@ -97,6 +117,10 @@ export default function AutomationDetail() {const toggleMutation = useToggleAutomation();const deleteMutation = useDeleteAutomation();const dispatchMutation = useDispatchAutomation();+ // Permission hooks must run before any early return (rules-of-hooks). The+ // owner check is a no-op while the automation hasn't loaded yet.+ const { canManage: hasManagePermission } = useAutomationPermissions();+ const isOwner = useIsAutomationOwner(automation ?? nullAutomation);const is404 = isError && getErrorStatus(error) === 404;@@ -196,6 +220,8 @@ export default function AutomationDetail() {// Edit is a local-backend-only feature in MVP β cloud automations// are managed elsewhere and we don't yet surface them here.const canEdit = active.backend.kind === "local";+ // Write actions on a specific automation: manage OR creator (escape hatch).+ const canManage = hasManagePermission || isOwner;return (<div className="min-h-full">@@ -213,6 +239,7 @@ export default function AutomationDetail() {}onRunNow={handleRunNow}isRunningNow={dispatchMutation.isPending}+ canManage={canManage}/>{automation.prompt && <PromptSection prompt={automation.prompt} />}<ConfigurationSection automation={automation} />diff --git a/src/routes/automation-git-sync.tsx b/src/routes/automation-git-sync.tsxindex 9cb2073b4c..df24eea3f3 100644--- a/src/routes/automation-git-sync.tsx+++ b/src/routes/automation-git-sync.tsx@@ -10,7 +10,7 @@ import {} from "#/hooks/query/use-git-sync";import { useAutomationHealth } from "#/hooks/query/use-automation-health";import { useActiveBackend } from "#/contexts/active-backend-context";-import { useHasPermission } from "#/hooks/use-has-permission";+import { useAutomationPermissions } from "#/hooks/use-automation-permissions";import { BackLink } from "#/components/features/automations/detail/back-link";import { ErrorState } from "#/components/features/automations/error-state";import { BackendNotConfigured } from "#/components/features/automations/backend-not-configured";@@ -53,7 +53,12 @@ const runningSince = (status: GitSyncStatus): SyncActivity => ({export default function AutomationGitSync() {const { t } = useTranslation("openhands");const active = useActiveBackend();- const canManage = useHasPermission("manage_automations");+ // Git sync is an org-level operation (not per-automation), so there is no+ // creator escape hatch β only users with manage_automations may configure,+ // check, or trigger a sync. The status read requires view_automations, but+ // since the page is local-only and local users always have manage, the+ // distinction only matters for cloud deployments that don't reach this page.+ const { canManage } = useAutomationPermissions();const [activity, setActivity] = useState<SyncActivity>({ state: "idle" });const isRunning = activity.state === "running";diff --git a/src/routes/automations-list.tsx b/src/routes/automations-list.tsxindex ec275c358b..19c006eac1 100644--- a/src/routes/automations-list.tsx+++ b/src/routes/automations-list.tsx@@ -38,6 +38,7 @@ import { ImportAutomationModal } from "#/components/features/automations/import-import { RecommendedAutomationsLauncher } from "#/components/features/automations/recommended-automations-launcher";import { BrandButton } from "#/components/features/settings/brand-button";import { useTracking } from "#/hooks/use-tracking";+import { useAutomationPermissions } from "#/hooks/use-automation-permissions";import type { Automation, AutomationSpec } from "#/types/automation";import {getAutomationExportFilename,@@ -121,6 +122,9 @@ export default function AutomationsList() {// Edit is a local-backend-only feature in MVP β cloud automations// are managed elsewhere and we don't yet surface them here.const canEdit = active.backend.kind === "local";+ // Creating an automation requires manage_automations (no owner escape hatch+ // β it's a new record, not a mutation of an existing one).+ const { canManage } = useAutomationPermissions();const {data: healthData,@@ -385,10 +389,12 @@ export default function AutomationsList() {{t(I18nKey.AUTOMATIONS$GIT_SYNC$NAV_BUTTON)}</BrandButton>)}- <AddAutomationMenu- onAdd={() => setIsAddAutomationOpen(true)}- onImport={() => setIsImportOpen(true)}- />+ {canManage ? (+ <AddAutomationMenu+ onAdd={() => setIsAddAutomationOpen(true)}+ onImport={() => setIsImportOpen(true)}+ />+ ) : null}</div></div>diff --git a/src/types/automation.ts b/src/types/automation.tsindex 374ad4ae08..f0616d8ab9 100644--- a/src/types/automation.ts+++ b/src/types/automation.ts@@ -26,6 +26,13 @@ export interface Automation {name: string;trigger: AutomationTrigger;enabled: boolean;+ /**+ * UUID of the user who created this automation. The backend returns it in+ * `AutomationResponse.user_id`; the frontend uses it to implement the+ * "creator escape hatch" β a member (view-only) may still edit their own+ * automations even without `manage_automations`.+ */+ user_id?: string;repository?: string;/** LLM/model profile name used for automation runs. */model?: string | null;