From 0d2245cbfc489ab631e81097bfd67624c0ecef88 Mon Sep 17 00:00:00 2001 From: Guilhem Lemouel Date: Fri, 30 Oct 2026 06:27:22 -0700 Subject: [PATCH] fixup! feat: grant and remove access from the access view Co-Authored-By: Claude Opus 5.5 --- ...4880fffdb7b2b91b678a6c315070d6fad7b41.json | 30 ----- backend/windmill-api-groups/src/access.rs | 108 ++++++++++++------ backend/windmill-api/openapi.yaml | 9 ++ .../src/lib/components/AccessDrawer.svelte | 24 +++- .../components/access/GrantNavigator.svelte | 28 +++-- 5 files changed, 111 insertions(+), 88 deletions(-) delete mode 100644 backend/.sqlx/query-81ed7bc2a02a9a535ce901e33984880fffdb7b2b91b678a6c315070d6fad7b41.json diff --git a/backend/.sqlx/query-81ed7bc2a02a9a535ce901e33984880fffdb7b2b91b678a6c315070d6fad7b41.json b/backend/.sqlx/query-81ed7bc2a02a9a535ce901e33984880fffdb7b2b91b678a6c315070d6fad7b41.json deleted file mode 100644 index b517bb1790..0000000000 --- a/backend/.sqlx/query-81ed7bc2a02a9a535ce901e33984880fffdb7b2b91b678a6c315070d6fad7b41.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - "db_name": "PostgreSQL", - "query": "SELECT name, ARRAY(SELECT k FROM jsonb_object_keys(extra_perms) k WHERE k = ANY($3) ORDER BY k) AS \"via!\"\n FROM folder WHERE workspace_id = $1 AND name = ANY($2)", - "describe": { - "columns": [ - { - "ordinal": 0, - "name": "name", - "type_info": "Varchar" - }, - { - "ordinal": 1, - "name": "via!", - "type_info": "TextArray" - } - ], - "parameters": { - "Left": [ - "Text", - "TextArray", - "TextArray" - ] - }, - "nullable": [ - false, - null - ] - }, - "hash": "81ed7bc2a02a9a535ce901e33984880fffdb7b2b91b678a6c315070d6fad7b41" -} diff --git a/backend/windmill-api-groups/src/access.rs b/backend/windmill-api-groups/src/access.rs index 9b2af2d335..0054187c84 100644 --- a/backend/windmill-api-groups/src/access.rs +++ b/backend/windmill-api-groups/src/access.rs @@ -83,6 +83,12 @@ struct FolderAccess { owner: bool, /// The `extra_perms` keys (`u/` or `g/`) that grant the folder. via: Vec, + /// The principal's own entry in `extra_perms`: absent when none, false for read, true for write. + #[serde(skip_serializing_if = "Option::is_none")] + grant: Option, + /// Why the caller cannot change the principal's own entry. Set only where there is one. + #[serde(skip_serializing_if = "Option::is_none")] + blocked: Option, } #[derive(Serialize)] @@ -91,6 +97,9 @@ struct ItemAccess { path: String, write: bool, reasons: Vec, + /// Why the caller cannot change the principal's own grant. Set only where there is one. + #[serde(skip_serializing_if = "Option::is_none")] + blocked: Option, } #[derive(Serialize)] @@ -184,7 +193,7 @@ impl From for ItemAccess { *write } }); - ItemAccess { kind: r.kind, path: r.path, write, reasons } + ItemAccess { kind: r.kind, path: r.path, write, reasons, blocked: None } } } @@ -282,39 +291,54 @@ async fn get_access( .iter() .map(|f| f.0.clone()) .collect::>(); - let mut via = sqlx::query!( - "SELECT name, ARRAY(SELECT k FROM jsonb_object_keys(extra_perms) k WHERE k = ANY($3) ORDER BY k) AS \"via!\" + let mut via = sqlx::query_as::<_, (String, Vec, Option)>( + "SELECT name, ARRAY(SELECT k FROM jsonb_object_keys(extra_perms) k WHERE k = ANY($3) ORDER BY k), + (extra_perms ->> $4)::boolean FROM folder WHERE workspace_id = $1 AND name = ANY($2)", - &w_id, - &folder_names, - &perms ) + .bind(&w_id) + .bind(&folder_names) + .bind(&perms) + .bind(&principal) .fetch_all(&db) .await? .into_iter() - .map(|r| (r.name, r.via)) + .map(|(name, via, grant)| (name, (via, grant))) .collect::>(); let mut folders = target .folders .iter() - .map(|(name, write, owner)| FolderAccess { - name: name.clone(), - write: *write, - owner: *owner, - via: via.remove(name).unwrap_or_default(), + .map(|(name, write, owner)| { + let (via, grant) = via.remove(name).unwrap_or_default(); + // An owner keeps write whatever its entry says, so there is nothing to change. + let blocked = (grant.is_some() && !*owner && !windmill_api_auth::is_owner(&authed, name)) + .then(|| folder_blocked(name)); + FolderAccess { name: name.clone(), write: *write, owner: *owner, via, grant, blocked } }) .collect::>(); folders.sort_by(|a, b| a.name.cmp(&b.name)); let mut tx = user_db.begin(&target).await?; - let items = sqlx::query_as::<_, ItemRow>(&items_query()) + let rows = sqlx::query_as::<_, ItemRow>(&items_query()) .bind(&w_id) .fetch_all(&mut *tx) - .await? - .into_iter() - .map(ItemAccess::from) - .collect(); + .await?; tx.commit().await?; + let denied = manage_denied(&db, &w_id, &authed).await; + let items = rows + .into_iter() + .map(|r| { + let direct = match prefix { + "u" => r.user_grant.is_some(), + _ => r.group_read.iter().any(|g| g == name), + }; + let mut item = ItemAccess::from(r); + if direct { + item.blocked = item_blocked(&authed, &denied, &item.kind, &item.path); + } + item + }) + .collect(); let mut groups = target.groups; groups.sort(); @@ -457,31 +481,41 @@ async fn browse_items( let has_more = items.len() as i64 > BROWSE_PAGE; items.truncate(BROWSE_PAGE as usize); - // Operators may be barred from managing schedules and triggers, `/acls` included. - let mut manage_denied = vec![]; + let denied = manage_denied(&db, &w_id, &authed).await; + for item in &mut items { + item.blocked = item_blocked(&authed, &denied, &item.kind, &item.path); + } + Ok(Json(BrowseItems { items, has_more })) +} + +/// Operators may be barred from managing schedules and triggers, `/acls` included. +async fn manage_denied(db: &DB, w_id: &str, authed: &ApiAuthed) -> Vec<(ManageKind, String)> { + let mut denied = vec![]; if !authed.is_admin { for kind in [ManageKind::Schedules, ManageKind::Triggers] { - if let Err(e) = check_operator_can_manage(&db, &w_id, &authed, kind.clone()).await { - manage_denied.push((kind, e.to_string())); + if let Err(e) = check_operator_can_manage(db, w_id, authed, kind).await { + denied.push((kind, e.to_string())); } } } - for item in &mut items { - let manage_kind = match item.kind.as_str() { - "schedule" => Some(ManageKind::Schedules), - k if k.ends_with("_trigger") => Some(ManageKind::Triggers), - _ => None, - }; - item.blocked = manage_kind - .and_then(|mk| { - manage_denied - .iter() - .find(|(k, _)| *k == mk) - .map(|(_, e)| e.clone()) - }) - .or_else(|| grant_blocked(&authed, &item.path)); - } - Ok(Json(BrowseItems { items, has_more })) + denied +} + +/// Why the caller cannot change grants on this item: the operator gate first, then the path. +fn item_blocked( + authed: &ApiAuthed, + denied: &[(ManageKind, String)], + kind: &str, + path: &str, +) -> Option { + let manage_kind = match kind { + "schedule" => Some(ManageKind::Schedules), + k if k.ends_with("_trigger") => Some(ManageKind::Triggers), + _ => None, + }; + manage_kind + .and_then(|mk| denied.iter().find(|(k, _)| *k == mk).map(|(_, e)| e.clone())) + .or_else(|| grant_blocked(authed, path)) } #[derive(Serialize)] diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index c2695b9f3e..2519354976 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -37123,6 +37123,12 @@ components: type: array items: type: string + grant: + description: the principal's own entry on the folder; absent when none, false for read, true for write + type: boolean + blocked: + description: why the caller cannot change the principal's own entry; set only where there is one + type: string required: [name, write, owner, via] items: type: array @@ -37139,6 +37145,9 @@ components: type: array items: $ref: "#/components/schemas/AccessReason" + blocked: + description: why the caller cannot change the principal's own grant; set only where there is one + type: string required: [kind, path, write, reasons] required: [principal, is_admin, groups, folders, items] diff --git a/frontend/src/lib/components/AccessDrawer.svelte b/frontend/src/lib/components/AccessDrawer.svelte index 675512f698..5615714fda 100644 --- a/frontend/src/lib/components/AccessDrawer.svelte +++ b/frontend/src/lib/components/AccessDrawer.svelte @@ -10,7 +10,8 @@ import { sendUserToast } from '$lib/toast' import type { Item } from '$lib/utils' import { logFeatureUsage } from '$lib/utils/featureUsage' - import { Plus } from 'lucide-svelte' + import { Lock, Plus } from 'lucide-svelte' + import Tooltip from './meltComponents/Tooltip.svelte' import Popover from './meltComponents/Popover.svelte' import DropdownV2 from './DropdownV2.svelte' import AddNeededAccess from './access/AddNeededAccess.svelte' @@ -92,8 +93,8 @@ await reload() } - /** `current` is the grant's level when known; a folder's entry is only known to exist. */ - function grantActions(kind: string, path: string, current?: boolean): Item[] { + /** `current` is the grant's level: `true` for write, `false` for read. */ + function grantActions(kind: string, path: string, current: boolean): Item[] { return [ { displayName: 'Read', @@ -156,6 +157,17 @@ } +{#snippet grantMenu(kind: string, path: string, current: boolean, blocked: string | undefined)} + {#if blocked} + + + {#snippet text()}{blocked}{/snippet} + + {:else} + + {/if} +{/snippet} + drawer?.closeDrawer()}>
@@ -224,8 +236,8 @@
- {#if !folder.owner && folder.via.includes(principal)} - + {#if !folder.owner && folder.grant != undefined} + {@render grantMenu('folder', folder.name, folder.grant, folder.blocked)} {/if} @@ -332,7 +344,7 @@ {#if own !== undefined} - + {@render grantMenu(item.kind, item.path, own, item.blocked)} {/if} diff --git a/frontend/src/lib/components/access/GrantNavigator.svelte b/frontend/src/lib/components/access/GrantNavigator.svelte index 18b5caf210..3044bb3b98 100644 --- a/frontend/src/lib/components/access/GrantNavigator.svelte +++ b/frontend/src/lib/components/access/GrantNavigator.svelte @@ -16,7 +16,7 @@ Picking a row grants the principal read on it; the "All of f/x" row grants the w import { userStore, workspaceStore } from '$lib/stores' import { sendUserToast } from '$lib/toast' import { debounce } from '$lib/utils' - import { Folder, ScanSearch, User } from 'lucide-svelte' + import { Folder, ScanSearch, User, Users } from 'lucide-svelte' import { setGrant } from './access' import type { RunnableKind } from './detectNeeds' @@ -129,7 +129,7 @@ Picking a row grants the principal read on it; the "All of f/x" row grants the w const tree = $derived.by[]>(() => { const folderNames = new Set((folders ?? []).map((f) => `f/${f.name}`)) const others = Object.keys(rows) - .filter((b) => b.startsWith('u/') && b !== own) + .filter((b) => b !== own && !folderNames.has(b)) .sort() return [ { @@ -168,19 +168,17 @@ Picking a row grants the principal read on it; the "All of f/x" row grants the w ] }) ), - // Other users' items appear once a search finds them. - ...others - .filter((b) => !folderNames.has(b)) - .map( - (b): DrillNode => ({ - type: 'branch', - key: b, - label: b, - icon: User, - searchGroup: true, - children: itemLeaves(b) - }) - ) + // Other users' and groups' items appear once a search finds them. + ...others.map( + (b): DrillNode => ({ + type: 'branch', + key: b, + label: b, + icon: b.startsWith('g/') ? Users : User, + searchGroup: true, + children: itemLeaves(b) + }) + ) ] })