|
| 1 | +// Channel access editor. Personal admin status and gateway-wide settings are never writable here. |
| 2 | +import { canManage, isAuthorized, PROFILE_FLAGS, channelProfile } from "../gateway/modes.js"; |
| 3 | + |
| 4 | +export const ACCESS_EDIT_ACTION_ID = "cg_channel_settings_access_edit"; |
| 5 | +export const ACCESS_CALLBACK_ID = "cg_channel_settings_access_form"; |
| 6 | +export const ACCESS_MODE_BLOCK_ID = "settings_access_mode"; |
| 7 | +const text = (value) => ({ type: "plain_text", text: value }); |
| 8 | +const option = (label, value) => ({ text: text(label), value }); |
| 9 | +const selects = { |
| 10 | + mode: [["Read-only", "read"], ["Worker", "worker"], ["Autonomous", "auto"]], |
| 11 | + access: [["Approved members", "approved"], ["Admins only", "admins"], ["Locked — named users only", "none"]], |
| 12 | + manageAccess: [["Org admins only", "admins"], ["Approved channel members", "members"], ["Named managers", "custom"]], |
| 13 | +}; |
| 14 | +const labels = { mode: "Mode", access: "Who can use it here", manageAccess: "Who can manage this channel" }; |
| 15 | +const flags = [["Full access — permission bypass for admin authors", "adminMode"], ["Lean — bare model without skills or connectors", "cleanMode"], ["Network — tell the engine network use is allowed", "allowNetwork"]]; |
| 16 | + |
| 17 | +export function accessSettingsSnapshot(meta = {}) { |
| 18 | + return { |
| 19 | + mode: meta.autoMode ? "auto" : meta.allowBash ? "worker" : "read", |
| 20 | + adminMode: Boolean(meta.adminMode), cleanMode: Boolean(meta.cleanMode), allowNetwork: Boolean(meta.allowNetwork), |
| 21 | + access: meta.access || "approved", manageAccess: meta.manageAccess || "admins", |
| 22 | + allowedUsers: meta.allowedUsers || [], managers: meta.managers || [], |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +export function assertAccessManager(meta, actor) { |
| 27 | + if (meta.isDM || !isAuthorized(meta, actor.authorId, false, actor) || !canManage(meta, actor)) { |
| 28 | + throw new Error("Only admins and current channel managers can change access settings."); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +export function buildAccessEditorView(meta, privateMetadata) { |
| 33 | + const current = accessSettingsSnapshot(meta); |
| 34 | + const flagOptions = flags.map(([label, value]) => option(label, value)); |
| 35 | + const initialFlags = flagOptions.filter((entry) => current[entry.value]); |
| 36 | + return { |
| 37 | + type: "modal", callback_id: ACCESS_CALLBACK_ID, private_metadata: privateMetadata, |
| 38 | + title: text("Channel access"), submit: text("Save"), close: text("Cancel"), |
| 39 | + blocks: [ |
| 40 | + { type: "section", text: { type: "mrkdwn", text: "Changes apply to this channel's next runs. Full access bypasses permissions only for admin authors. If the operator enabled whole-home access, Full access also exposes the gateway home to this channel. Network is advisory; the container stays on its bridge network." } }, |
| 41 | + ...Object.entries(selects).map(([key, choices]) => { |
| 42 | + const options = choices.map(([label, value]) => option(label, value)); |
| 43 | + return { type: "input", block_id: `settings_access_${key}`, label: text(labels[key]), element: { |
| 44 | + type: "static_select", action_id: key, options, |
| 45 | + initial_option: options.find((entry) => entry.value === current[key]) || options[0], |
| 46 | + } }; |
| 47 | + }), |
| 48 | + { type: "input", block_id: "settings_access_flags", optional: true, label: text("Special modes & network"), element: { |
| 49 | + type: "checkboxes", action_id: "flags", options: flagOptions, |
| 50 | + ...(initialFlags.length ? { initial_options: initialFlags } : {}), |
| 51 | + } }, |
| 52 | + ...[["allowedUsers", "Guest access — named users"], ["managers", "Named managers (when selected above)"]].map(([key, label]) => ({ |
| 53 | + type: "input", block_id: `settings_access_${key}`, optional: true, label: text(label), |
| 54 | + hint: text("Select current human members of this channel. Membership is checked when you save."), |
| 55 | + element: { type: "multi_users_select", action_id: key, max_selected_items: 100, |
| 56 | + ...(current[key].length ? { initial_users: current[key] } : {}), |
| 57 | + }, |
| 58 | + })), |
| 59 | + ], |
| 60 | + }; |
| 61 | +} |
| 62 | + |
| 63 | +export function readAccessForm(view = {}) { |
| 64 | + const values = view.state?.values || {}; |
| 65 | + const form = {}; |
| 66 | + for (const key of Object.keys(selects)) form[key] = values[`settings_access_${key}`]?.[key]?.selected_option?.value; |
| 67 | + const chosen = values.settings_access_flags?.flags?.selected_options; |
| 68 | + if (!Array.isArray(chosen)) throw new Error("Access form is incomplete. Reopen Settings and try again."); |
| 69 | + if (chosen.some((entry) => !flags.some(([, key]) => key === entry.value))) throw new Error("Unknown access option."); |
| 70 | + for (const [, key] of flags) form[key] = chosen.some((entry) => entry.value === key); |
| 71 | + for (const key of ["allowedUsers", "managers"]) { |
| 72 | + const selected = values[`settings_access_${key}`]?.[key]; |
| 73 | + if (!selected || !Array.isArray(selected.selected_users)) throw new Error("Access form is incomplete. Reopen Settings and try again."); |
| 74 | + form[key] = selected.selected_users; |
| 75 | + } |
| 76 | + return form; |
| 77 | +} |
| 78 | + |
| 79 | +// Validate both UI values and the latest policy inside the metadata transaction. A stale editor |
| 80 | +// cannot restore its own revoked management grant or overwrite unrelated channel settings. |
| 81 | +export function accessSettingsPatch(meta, form, actor) { |
| 82 | + assertAccessManager(meta, actor); |
| 83 | + for (const [key, choices] of Object.entries(selects)) { |
| 84 | + if (!choices.some(([, value]) => form[key] === value)) throw new Error(`Invalid ${labels[key]}.`); |
| 85 | + } |
| 86 | + for (const [, key] of flags) if (typeof form[key] !== "boolean") throw new Error("Invalid access option."); |
| 87 | + for (const key of ["allowedUsers", "managers"]) { |
| 88 | + if (!Array.isArray(form[key]) || form[key].length > 100 || form[key].some((id) => typeof id !== "string" || !/^[UW][A-Z0-9]+$/.test(id))) { |
| 89 | + throw new Error("Choose up to 100 valid users per list."); |
| 90 | + } |
| 91 | + } |
| 92 | + const patch = { |
| 93 | + ...PROFILE_FLAGS[form.mode], adminMode: form.adminMode, cleanMode: form.cleanMode, |
| 94 | + allowNetwork: form.allowNetwork, access: form.access, manageAccess: form.manageAccess, |
| 95 | + allowedUsers: [...new Set(form.allowedUsers)], managers: [...new Set(form.managers)], |
| 96 | + }; |
| 97 | + patch.profile = Object.entries(PROFILE_FLAGS).find(([, preset]) => Object.keys(preset).every((key) => patch[key] === preset[key]))?.[0] || "custom"; |
| 98 | + return patch; |
| 99 | +} |
| 100 | + |
| 101 | +export function accessSummary(meta) { |
| 102 | + const current = accessSettingsSnapshot(meta); |
| 103 | + return `*Mode:* ${channelProfile(meta)}\n*Full access:* ${current.adminMode ? "on" : "off"} · *Lean:* ${current.cleanMode ? "on" : "off"} · *Network:* ${current.allowNetwork ? "on" : "off"}\n*Who can use:* ${current.access} · *Who can manage:* ${current.manageAccess}\n*Named users:* ${current.allowedUsers.length} · *Named managers:* ${current.managers.length}`; |
| 104 | +} |
0 commit comments