Skip to content

Commit 171243c

Browse files
fix(auth): use settings permission for saved project actions
1 parent b9c0bc6 commit 171243c

5 files changed

Lines changed: 77 additions & 24 deletions

File tree

‎apps/web/src/components/projectScriptEditor.test.tsx‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
AuthOrchestrationOperateScope,
3+
AuthSettingsWriteScope,
34
EnvironmentId,
45
type AuthEnvironmentScope,
56
} from "@t3tools/contracts";
@@ -86,11 +87,15 @@ describe("ProjectScriptEditorDialog", () => {
8687
onSubmit = vi.fn().mockResolvedValue(AsyncResult.success(undefined)),
8788
editorRequest = request,
8889
actions = { onDelete: vi.fn(), onClose: vi.fn() },
90+
editScope:
91+
| typeof AuthOrchestrationOperateScope
92+
| typeof AuthSettingsWriteScope = AuthOrchestrationOperateScope,
8993
) {
9094
await act(() => {
9195
renderer = create(
9296
<ProjectScriptEditorDialog
9397
environmentId={EnvironmentId.make("script-editor-test")}
98+
editScope={editScope}
9499
request={editorRequest}
95100
scripts={[]}
96101
onSubmit={onSubmit}
@@ -102,6 +107,52 @@ describe("ProjectScriptEditorDialog", () => {
102107
return renderer!.root;
103108
}
104109

110+
it.each([null, "test"])(
111+
"saves a settings action without task access (id: %s)",
112+
async (scriptId) => {
113+
permissions.canEditProject = false;
114+
permissions.canWriteSettings = true;
115+
const onSubmit = vi.fn().mockResolvedValue(AsyncResult.success(undefined));
116+
const root = await openEditor(
117+
onSubmit,
118+
{ ...request, scriptId },
119+
undefined,
120+
AuthSettingsWriteScope,
121+
);
122+
const submit = root.findByType("form").props.onSubmit;
123+
permissions.canWriteSettings = false;
124+
await act(async () => {
125+
await submit({ preventDefault() {} });
126+
});
127+
expect(onSubmit).not.toHaveBeenCalled();
128+
permissions.canWriteSettings = true;
129+
await act(async () => {
130+
await submit({ preventDefault() {} });
131+
});
132+
expect(onSubmit).toHaveBeenCalledExactlyOnceWith(
133+
scriptId,
134+
expect.objectContaining({ command: "vp test" }),
135+
);
136+
},
137+
);
138+
139+
it("rechecks settings access before deleting a settings action", async () => {
140+
permissions.canEditProject = false;
141+
permissions.canWriteSettings = true;
142+
const actions = { onDelete: vi.fn(), onClose: vi.fn() };
143+
const root = await openEditor(undefined, request, actions, AuthSettingsWriteScope);
144+
await act(() => {
145+
root.findByProps({ variant: "destructive-outline" }).props.onClick();
146+
});
147+
const confirmDelete = root.findByProps({ variant: "destructive" }).props.onClick;
148+
permissions.canWriteSettings = false;
149+
await act(() => confirmDelete());
150+
expect(actions.onDelete).not.toHaveBeenCalled();
151+
permissions.canWriteSettings = true;
152+
await act(() => confirmDelete());
153+
expect(actions.onDelete).toHaveBeenCalledExactlyOnceWith(request.scriptId);
154+
});
155+
105156
it("clears an old shortcut when adding an action with no shortcut", async () => {
106157
permissions.canWriteSettings = true;
107158
let persistedKeybinding: string | null = "mod+k";

‎apps/web/src/components/projectScriptEditor.tsx‎

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,15 @@ export function editorRequestForScript(
136136
*/
137137
export function ProjectScriptEditorDialog({
138138
environmentId,
139+
editScope = AuthOrchestrationOperateScope,
139140
request,
140141
scripts,
141142
onSubmit,
142143
onDelete,
143144
onClose,
144145
}: {
145146
environmentId: EnvironmentId;
147+
editScope?: typeof AuthOrchestrationOperateScope | typeof AuthSettingsWriteScope;
146148
request: ProjectScriptEditorRequest | null;
147149
/** Existing scripts, used to derive a unique id for new scripts. */
148150
scripts: ReadonlyArray<ProjectScript>;
@@ -153,19 +155,19 @@ export function ProjectScriptEditorDialog({
153155
onDelete: (scriptId: string) => void;
154156
onClose: () => void;
155157
}) {
156-
const canEditProject = useEnvironmentScope(environmentId, AuthOrchestrationOperateScope);
158+
const canEditActions = useEnvironmentScope(environmentId, editScope);
157159
const canWriteSettings = useEnvironmentScope(environmentId, AuthSettingsWriteScope);
158160
const formId = React.useId();
159161
const [name, setName] = useState("");
160162
const [command, setCommand] = useState("");
161163
const [icon, setIcon] = useState<ProjectScriptIcon>("play");
162-
const [iconPickerOpen, setIconPickerOpen] = useComposerMenuState(!canEditProject);
164+
const [iconPickerOpen, setIconPickerOpen] = useComposerMenuState(!canEditActions);
163165
const [runOnWorktreeCreate, setRunOnWorktreeCreate] = useState(false);
164166
const [keybinding, setKeybinding] = useState("");
165167
const [previewUrl, setPreviewUrl] = useState("");
166168
const [autoOpenPreview, setAutoOpenPreview] = useState(false);
167169
const [validationError, setValidationError] = useState<string | null>(null);
168-
const [deleteConfirmOpen, setDeleteConfirmOpen] = useComposerMenuState(!canEditProject);
170+
const [deleteConfirmOpen, setDeleteConfirmOpen] = useComposerMenuState(!canEditActions);
169171

170172
const isOpen = request !== null;
171173
const isEditing = request?.scriptId != null;
@@ -185,7 +187,7 @@ export function ProjectScriptEditorDialog({
185187
}, [request, setIconPickerOpen]);
186188

187189
const captureKeybinding = (event: KeyboardEvent<HTMLInputElement>) => {
188-
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) return;
190+
if (!readEnvironmentScope(environmentId, editScope)) return;
189191
if (!readEnvironmentScope(environmentId, AuthSettingsWriteScope)) return;
190192
if (event.key === "Tab") return;
191193
event.preventDefault();
@@ -201,7 +203,7 @@ export function ProjectScriptEditorDialog({
201203
const submit = async (event: FormEvent) => {
202204
event.preventDefault();
203205
if (!request) return;
204-
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) {
206+
if (!readEnvironmentScope(environmentId, editScope)) {
205207
setValidationError("This connection cannot change project actions.");
206208
return;
207209
}
@@ -284,7 +286,7 @@ export function ProjectScriptEditorDialog({
284286
</DialogDescription>
285287
</DialogHeader>
286288
<DialogPanel>
287-
{!canEditProject && (
289+
{!canEditActions && (
288290
<p className="mb-4 text-sm text-muted-foreground">
289291
This connection cannot change project actions.
290292
</p>
@@ -293,15 +295,15 @@ export function ProjectScriptEditorDialog({
293295
<div className="space-y-1.5">
294296
<Label htmlFor="script-name">Name</Label>
295297
<div className="flex items-center gap-2">
296-
<Popover onOpenChange={setIconPickerOpen} open={iconPickerOpen && canEditProject}>
298+
<Popover onOpenChange={setIconPickerOpen} open={iconPickerOpen && canEditActions}>
297299
<PopoverTrigger
298300
render={
299301
<Button
300302
type="button"
301303
variant="outline"
302304
className="size-9 shrink-0 hover:bg-popover active:bg-popover data-pressed:bg-popover data-pressed:shadow-xs/5 data-pressed:before:shadow-[0_1px_--theme(--color-black/4%)] dark:border-transparent dark:bg-white/[0.035] dark:data-pressed:before:shadow-none"
303305
aria-label="Choose icon"
304-
disabled={!canEditProject}
306+
disabled={!canEditActions}
305307
/>
306308
}
307309
>
@@ -315,7 +317,7 @@ export function ProjectScriptEditorDialog({
315317
<button
316318
key={entry.id}
317319
type="button"
318-
disabled={!canEditProject}
320+
disabled={!canEditActions}
319321
className={`relative flex flex-col items-center gap-2 rounded-md border px-2 py-2 text-xs dark:border-transparent ${
320322
isSelected
321323
? "border-primary/70 bg-primary/10 dark:ring-1 dark:ring-primary/30"
@@ -336,7 +338,7 @@ export function ProjectScriptEditorDialog({
336338
</Popover>
337339
<Input
338340
id="script-name"
339-
disabled={!canEditProject}
341+
disabled={!canEditActions}
340342
autoFocus
341343
placeholder="Test"
342344
value={name}
@@ -350,7 +352,7 @@ export function ProjectScriptEditorDialog({
350352
id="script-keybinding"
351353
placeholder="Press shortcut"
352354
value={keybinding}
353-
disabled={!canEditProject || !canWriteSettings}
355+
disabled={!canEditActions || !canWriteSettings}
354356
readOnly
355357
onKeyDown={captureKeybinding}
356358
/>
@@ -368,7 +370,7 @@ export function ProjectScriptEditorDialog({
368370
<Label htmlFor="script-command">Command</Label>
369371
<Textarea
370372
id="script-command"
371-
disabled={!canEditProject}
373+
disabled={!canEditActions}
372374
placeholder="bun test"
373375
value={command}
374376
onChange={(event) => setCommand(event.target.value)}
@@ -378,7 +380,7 @@ export function ProjectScriptEditorDialog({
378380
<Label htmlFor="script-preview-url">Preview URL (optional)</Label>
379381
<Input
380382
id="script-preview-url"
381-
disabled={!canEditProject}
383+
disabled={!canEditActions}
382384
placeholder="http://localhost:5173"
383385
value={previewUrl}
384386
onChange={(event) => setPreviewUrl(event.target.value)}
@@ -391,7 +393,7 @@ export function ProjectScriptEditorDialog({
391393
<span>Run automatically on worktree creation</span>
392394
<Switch
393395
checked={runOnWorktreeCreate}
394-
disabled={!canEditProject}
396+
disabled={!canEditActions}
395397
onCheckedChange={(checked) => setRunOnWorktreeCreate(Boolean(checked))}
396398
/>
397399
</label>
@@ -403,7 +405,7 @@ export function ProjectScriptEditorDialog({
403405
<span>Open preview automatically when this action runs</span>
404406
<Switch
405407
checked={autoOpenPreview}
406-
disabled={!canEditProject || previewUrl.trim().length === 0}
408+
disabled={!canEditActions || previewUrl.trim().length === 0}
407409
onCheckedChange={(checked) => setAutoOpenPreview(Boolean(checked))}
408410
/>
409411
</label>
@@ -416,7 +418,7 @@ export function ProjectScriptEditorDialog({
416418
type="button"
417419
variant="destructive-outline"
418420
className="mr-auto"
419-
disabled={!canEditProject}
421+
disabled={!canEditActions}
420422
onClick={() => setDeleteConfirmOpen(true)}
421423
>
422424
Delete
@@ -425,14 +427,14 @@ export function ProjectScriptEditorDialog({
425427
<Button type="button" variant="outline" onClick={onClose}>
426428
Cancel
427429
</Button>
428-
<Button form={formId} type="submit" disabled={!canEditProject}>
430+
<Button form={formId} type="submit" disabled={!canEditActions}>
429431
{isEditing ? "Save changes" : "Save action"}
430432
</Button>
431433
</DialogFooter>
432434
</DialogPopup>
433435
</Dialog>
434436

435-
<AlertDialog open={deleteConfirmOpen && canEditProject} onOpenChange={setDeleteConfirmOpen}>
437+
<AlertDialog open={deleteConfirmOpen && canEditActions} onOpenChange={setDeleteConfirmOpen}>
436438
<AlertDialogPopup>
437439
<AlertDialogHeader>
438440
<AlertDialogTitle>Delete action "{name}"?</AlertDialogTitle>
@@ -442,10 +444,10 @@ export function ProjectScriptEditorDialog({
442444
<AlertDialogClose render={<Button variant="outline" />}>Cancel</AlertDialogClose>
443445
<Button
444446
variant="destructive"
445-
disabled={!canEditProject}
447+
disabled={!canEditActions}
446448
onClick={() => {
447449
if (!request?.scriptId) return;
448-
if (!readEnvironmentScope(environmentId, AuthOrchestrationOperateScope)) {
450+
if (!readEnvironmentScope(environmentId, editScope)) {
449451
setValidationError("This connection cannot change project actions.");
450452
return;
451453
}

‎apps/web/src/components/settings/ProjectDefaultActionsSettings.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { EnvironmentId } from "@t3tools/contracts";
1+
import { AuthSettingsWriteScope, type EnvironmentId } from "@t3tools/contracts";
22
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
33
import { PlusIcon } from "lucide-react";
44
import { useState } from "react";
@@ -105,6 +105,7 @@ export function ProjectDefaultActionsSettings({
105105
// The editor's controls follow one machine's grant; the write itself
106106
// checks every selected machine before it touches any of them.
107107
<ProjectScriptEditorDialog
108+
editScope={AuthSettingsWriteScope}
108109
environmentId={editorEnvironmentId}
109110
request={request}
110111
scripts={scripts}

‎apps/web/src/components/settings/ProjectSettingsPanel.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,7 @@ function ProjectDetail({
15201520
</SettingsPageContainer>
15211521

15221522
<ProjectScriptEditorDialog
1523+
editScope={AuthSettingsWriteScope}
15231524
environmentId={selectedCheckout.environmentId}
15241525
request={editorRequest}
15251526
scripts={scripts}

‎apps/web/src/editorPreferences.ts‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ export function usePreferredEditor(availableEditors: ReadonlyArray<EditorId>) {
5656
return [effectiveEditor, setLastEditor] as const;
5757
}
5858

59-
export function resolveAndPersistPreferredEditor(
60-
availableEditors: readonly EditorId[],
61-
): EditorId | null {
59+
function resolveAndPersistPreferredEditor(availableEditors: readonly EditorId[]): EditorId | null {
6260
const availableEditorIds = new Set(availableEditors);
6361
const stored = getLocalStorageItem(LAST_EDITOR_KEY, EditorId);
6462
if (stored && availableEditorIds.has(stored)) return stored;

0 commit comments

Comments
 (0)