@rebasepro/studio 0.35.1 → 0.37.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/{ApiExplorer-C61w8gCc.js → ApiExplorer-BVQHAjHB.js} +3 -2
- package/dist/{ApiExplorer-C61w8gCc.js.map → ApiExplorer-BVQHAjHB.js.map} +1 -1
- package/dist/{useRunAsUsers-CF7tkGzj.js → AuthSimulationSelector-XogpkeXC.js} +4 -68
- package/dist/AuthSimulationSelector-XogpkeXC.js.map +1 -0
- package/dist/{CronJobsView-DUuyzkJK.js → CronJobsView-DyX9MINh.js} +2 -2
- package/dist/{CronJobsView-DUuyzkJK.js.map → CronJobsView-DyX9MINh.js.map} +1 -1
- package/dist/DatabaseView-Bi0NZS90.js +1897 -0
- package/dist/DatabaseView-Bi0NZS90.js.map +1 -0
- package/dist/{JSEditor-Bi2Aol-Y.js → JSEditor-B4Lazm-z.js} +4 -3
- package/dist/{JSEditor-Bi2Aol-Y.js.map → JSEditor-B4Lazm-z.js.map} +1 -1
- package/dist/RLSEditor-CcHkWFNg.js +973 -0
- package/dist/RLSEditor-CcHkWFNg.js.map +1 -0
- package/dist/{SQLEditor-DRo5kW1h.js → SQLEditorRoute-f5j_ZKdj.js} +1472 -320
- package/dist/SQLEditorRoute-f5j_ZKdj.js.map +1 -0
- package/dist/SchemaDiagramView-CckQobIZ.js +98 -0
- package/dist/SchemaDiagramView-CckQobIZ.js.map +1 -0
- package/dist/{StorageView-D0D8z4lI.js → StorageView-DQEIDFvO.js} +592 -447
- package/dist/StorageView-DQEIDFvO.js.map +1 -0
- package/dist/TableEditorView-BRZNvD6U.js +3615 -0
- package/dist/TableEditorView-BRZNvD6U.js.map +1 -0
- package/dist/TablePolicies-C1-tbC8x.js +1740 -0
- package/dist/TablePolicies-C1-tbC8x.js.map +1 -0
- package/dist/{SchemaVisualizer-Dy4kWKT5.js → TablesDiagram-RTJfwVEN.js} +651 -489
- package/dist/TablesDiagram-RTJfwVEN.js.map +1 -0
- package/dist/{UsersAndRolesView-CKw4SJPj.js → UsersAndRolesView-Cx6xtMVH.js} +2 -2
- package/dist/{UsersAndRolesView-CKw4SJPj.js.map → UsersAndRolesView-Cx6xtMVH.js.map} +1 -1
- package/dist/{WebhooksView-CFPY-N9h.js → WebhooksView-Bn9KB7xG.js} +2 -2
- package/dist/{WebhooksView-CFPY-N9h.js.map → WebhooksView-Bn9KB7xG.js.map} +1 -1
- package/dist/{adminFetch-BfOnfRgU.js → adminFetch-C7Ra8rpP.js} +26 -1
- package/dist/{adminFetch-BfOnfRgU.js.map → adminFetch-C7Ra8rpP.js.map} +1 -1
- package/dist/components/Database/ActivitySection.d.ts +9 -0
- package/dist/components/Database/AdvisorsSection.d.ts +17 -0
- package/dist/components/Database/DatabaseView.d.ts +13 -0
- package/dist/components/Database/ExtensionsSection.d.ts +2 -0
- package/dist/components/Database/FunctionsSection.d.ts +2 -0
- package/dist/components/Database/QueriesSection.d.ts +3 -0
- package/dist/components/Database/TriggersSection.d.ts +2 -0
- package/dist/components/Database/TypesSection.d.ts +2 -0
- package/dist/components/Database/format.d.ts +6 -0
- package/dist/components/Database/links.d.ts +11 -0
- package/dist/components/Database/shared.d.ts +35 -0
- package/dist/components/Database/source.d.ts +12 -0
- package/dist/components/Database/useCatalogList.d.ts +13 -0
- package/dist/components/RLSEditor/RLSEditor.d.ts +2 -0
- package/dist/components/RLSEditor/TablePolicies.d.ts +54 -0
- package/dist/components/SQLEditor/ExplainVisualizer.d.ts +8 -20
- package/dist/components/SQLEditor/ResultChart.d.ts +18 -0
- package/dist/components/SQLEditor/SQLEditor.d.ts +13 -1
- package/dist/components/SQLEditor/SQLEditorRoute.d.ts +13 -0
- package/dist/components/SQLEditor/SQLEditorSidebar.d.ts +7 -1
- package/dist/components/SQLEditor/SqlParamsPanel.d.ts +16 -0
- package/dist/components/SQLEditor/useSqlSnippets.d.ts +47 -0
- package/dist/components/SchemaVisualizer/SchemaDiagramView.d.ts +12 -0
- package/dist/components/SchemaVisualizer/TablesDiagram.d.ts +23 -0
- package/dist/components/SchemaVisualizer/useSchemaGraph.d.ts +32 -1
- package/dist/components/StorageView/StorageView.d.ts +5 -0
- package/dist/components/TableEditor/CreateTableDialog.d.ts +8 -0
- package/dist/components/TableEditor/DdlConfirm.d.ts +23 -0
- package/dist/components/TableEditor/SqlBlock.d.ts +7 -0
- package/dist/components/TableEditor/TableDdlSheet.d.ts +9 -0
- package/dist/components/TableEditor/TableEditorView.d.ts +7 -0
- package/dist/components/TableEditor/tableModel.d.ts +210 -0
- package/dist/components/TableEditor/tabs/DataTab.d.ts +12 -0
- package/dist/components/TableEditor/tabs/PoliciesTab.d.ts +8 -0
- package/dist/components/TableEditor/tabs/RelationsTab.d.ts +8 -0
- package/dist/components/TableEditor/tabs/SettingsTab.d.ts +11 -0
- package/dist/components/TableEditor/tabs/StatsTab.d.ts +8 -0
- package/dist/components/TableEditor/tabs/StructureTab.d.ts +12 -0
- package/dist/index.es.js +29 -15
- package/dist/index.es.js.map +1 -1
- package/dist/{sql_utils-CYcgpgam.js → sql_utils-DkMYltfq.js} +27 -3
- package/dist/sql_utils-DkMYltfq.js.map +1 -0
- package/dist/useRunAsUsers-GPFCxcBC.js +69 -0
- package/dist/useRunAsUsers-GPFCxcBC.js.map +1 -0
- package/dist/utils/adminFetch.d.ts +1 -0
- package/dist/utils/csv_parse.d.ts +11 -0
- package/dist/utils/explain_plan.d.ts +70 -0
- package/dist/utils/sql_params.d.ts +51 -0
- package/dist/utils/sql_utils.d.ts +16 -0
- package/package.json +11 -9
- package/dist/RLSEditor-CTG3jLE7.js +0 -2572
- package/dist/RLSEditor-CTG3jLE7.js.map +0 -1
- package/dist/SQLEditor-DRo5kW1h.js.map +0 -1
- package/dist/SchemaVisualizer-Dy4kWKT5.js.map +0 -1
- package/dist/StorageView-D0D8z4lI.js.map +0 -1
- package/dist/TableEditorView-BKhSBG-K.js +0 -1694
- package/dist/TableEditorView-BKhSBG-K.js.map +0 -1
- package/dist/components/SchemaVisualizer/SchemaVisualizer.d.ts +0 -3
- package/dist/policy-presets-CJKzAvQp.js +0 -167
- package/dist/policy-presets-CJKzAvQp.js.map +0 -1
- package/dist/sql_utils-CYcgpgam.js.map +0 -1
- package/dist/useRunAsUsers-CF7tkGzj.js.map +0 -1
|
@@ -0,0 +1,1740 @@
|
|
|
1
|
+
import { s as quoteIdentifier } from "./sql_utils-DkMYltfq.js";
|
|
2
|
+
import { t as MonacoEditor } from "./MonacoEditor-7IlHFZU5.js";
|
|
3
|
+
import { r as schemaSourceProblem, t as SchemaSourceProblemAlert } from "./schema-source-C4ekB7WU.js";
|
|
4
|
+
import { ConfirmationDialog, useApiBase, useApiConfig, useRebaseContext, useSnackbarController, useStudioCapabilities, useStudioSchemaEditing, useTableDescriptors, useTranslation } from "@rebasepro/app";
|
|
5
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
6
|
+
import { Alert, AlertTriangleIcon, Button, Dialog, DialogActions, DialogContent, DialogTitle, HelpCircleIcon, IconButton, KeyIcon, MultiSelect, MultiSelectItem, Paper, PlusIcon, Select, SelectItem, ShieldIcon, StatusDot, TextField, Tooltip, Trash2Icon, Typography, cls, defaultBorderMixin, iconSize } from "@rebasepro/ui";
|
|
7
|
+
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
|
|
8
|
+
import { REBASE_ADMIN_REQUEST_ROLE, REBASE_ADMIN_ROLE, RLS_JWT_SQL, RLS_ROLES_SQL, RLS_UID_SQL, isRedactedSecurityRule, policy, rulePatches } from "@rebasepro/types";
|
|
9
|
+
import { adminRequestPolicyName, getPolicyNamesForRule, getPolicyOperations } from "@rebasepro/utils";
|
|
10
|
+
import { REBASE_INTERNAL_PREFIXES, REBASE_INTERNAL_SCHEMAS, REBASE_USER_ROLE, compilePolicyExpression, policyToSecurityRule } from "@rebasepro/common";
|
|
11
|
+
//#region src/components/RLSEditor/policyListing.ts
|
|
12
|
+
/**
|
|
13
|
+
* The roles of a `pg_policies` row, from its `roles` column.
|
|
14
|
+
*
|
|
15
|
+
* A driver hands a `name[]` back as an array or as Postgres's text for one:
|
|
16
|
+
* `{public}`, `{anon,authenticated}`, and with an element quoted — a `"` or
|
|
17
|
+
* `\` in it escaped by a backslash — when it holds a comma, a brace, a quote,
|
|
18
|
+
* a backslash or a space: `{"my role",anon}`. Split on commas, that last one
|
|
19
|
+
* reads as the role `"my role"`, quotes and all: the policy shows drift that
|
|
20
|
+
* is not there, and an edit of it names a role that does not exist.
|
|
21
|
+
*/
|
|
22
|
+
function readPolicyRoles(value) {
|
|
23
|
+
if (Array.isArray(value)) return value.filter((role) => typeof role === "string");
|
|
24
|
+
if (typeof value !== "string") return [];
|
|
25
|
+
const body = value.trim().replace(/^\{/, "").replace(/\}$/, "");
|
|
26
|
+
const roles = [];
|
|
27
|
+
let i = 0;
|
|
28
|
+
while (i < body.length) {
|
|
29
|
+
if (body[i] === "\"") {
|
|
30
|
+
let role = "";
|
|
31
|
+
i++;
|
|
32
|
+
while (i < body.length && body[i] !== "\"") {
|
|
33
|
+
if (body[i] === "\\") i++;
|
|
34
|
+
role += body[i] ?? "";
|
|
35
|
+
i++;
|
|
36
|
+
}
|
|
37
|
+
roles.push(role);
|
|
38
|
+
i++;
|
|
39
|
+
} else {
|
|
40
|
+
const comma = body.indexOf(",", i);
|
|
41
|
+
const end = comma === -1 ? body.length : comma;
|
|
42
|
+
const role = body.slice(i, end).trim();
|
|
43
|
+
if (role) roles.push(role);
|
|
44
|
+
i = end;
|
|
45
|
+
}
|
|
46
|
+
while (i < body.length && body[i] !== ",") i++;
|
|
47
|
+
i++;
|
|
48
|
+
}
|
|
49
|
+
return roles;
|
|
50
|
+
}
|
|
51
|
+
var COMMANDS = {
|
|
52
|
+
select: "SELECT",
|
|
53
|
+
insert: "INSERT",
|
|
54
|
+
update: "UPDATE",
|
|
55
|
+
delete: "DELETE",
|
|
56
|
+
all: "ALL"
|
|
57
|
+
};
|
|
58
|
+
function commandOf(operation) {
|
|
59
|
+
return operation in COMMANDS ? COMMANDS[operation] : "ALL";
|
|
60
|
+
}
|
|
61
|
+
/** A policy the server compiled, in the shape `pg_policies` describes one. */
|
|
62
|
+
function servedAsPostgresPolicy(served, tableName) {
|
|
63
|
+
return {
|
|
64
|
+
policyname: served.name,
|
|
65
|
+
tablename: tableName,
|
|
66
|
+
permissive: served.mode === "restrictive" ? "RESTRICTIVE" : "PERMISSIVE",
|
|
67
|
+
cmd: commandOf(served.operation),
|
|
68
|
+
roles: [...served.roles],
|
|
69
|
+
qual: served.using,
|
|
70
|
+
with_check: served.withCheck
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The rule's own clauses, for a declared rule the server compiled no policy
|
|
75
|
+
* from — an `existsIn` naming a table the backend does not hold, say. Only
|
|
76
|
+
* ever a `code_only` row's content: nothing is compared against it.
|
|
77
|
+
*/
|
|
78
|
+
function uncompiledPolicy(rule, operation, name, tableName) {
|
|
79
|
+
return {
|
|
80
|
+
policyname: name,
|
|
81
|
+
tablename: tableName,
|
|
82
|
+
permissive: rule.mode === "restrictive" ? "RESTRICTIVE" : "PERMISSIVE",
|
|
83
|
+
cmd: COMMANDS[operation],
|
|
84
|
+
roles: [...rule.pgRoles ?? ["public"]],
|
|
85
|
+
qual: rule.using || null,
|
|
86
|
+
with_check: rule.withCheck || null
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
function sameRoles$1(a, b) {
|
|
90
|
+
const left = [...a].sort();
|
|
91
|
+
const right = [...b].sort();
|
|
92
|
+
return left.length === right.length && left.every((role, i) => role === right[i]);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Where a live policy differs from the one the code compiles to.
|
|
96
|
+
*
|
|
97
|
+
* The same comparison `rebase doctor` makes: the `TO` list, the command, the
|
|
98
|
+
* mode, and whether each clause is there at all. Never the clause text —
|
|
99
|
+
* Postgres rewrites an expression when it stores it, so the text of a policy
|
|
100
|
+
* that matches its declaration exactly still differs from the declaration's.
|
|
101
|
+
* A clause that is missing is not a rewrite, though: a `USING` that is gone
|
|
102
|
+
* changes who the policy admits.
|
|
103
|
+
*/
|
|
104
|
+
function policyDrift(declared, live) {
|
|
105
|
+
const drift = [];
|
|
106
|
+
if (!sameRoles$1(declared.roles, live.roles)) drift.push("roles");
|
|
107
|
+
if (declared.cmd !== live.cmd) drift.push("command");
|
|
108
|
+
if (declared.permissive !== live.permissive) drift.push("mode");
|
|
109
|
+
if (declared.qual != null !== (live.qual != null)) drift.push("USING");
|
|
110
|
+
if (declared.with_check != null !== (live.with_check != null)) drift.push("WITH CHECK");
|
|
111
|
+
return drift;
|
|
112
|
+
}
|
|
113
|
+
/** The {@link DeclaredPolicies} of a served table, when the server included its compiled policies. */
|
|
114
|
+
function declaredPoliciesOf(rls) {
|
|
115
|
+
return rls.policies ? {
|
|
116
|
+
rules: rls.rules,
|
|
117
|
+
policies: rls.policies
|
|
118
|
+
} : void 0;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* The policies of one table: every policy the database has, as the database
|
|
122
|
+
* has it, and every policy the server compiles for the table that it does not.
|
|
123
|
+
*
|
|
124
|
+
* The server is the one compiler: what it serves in `rls.policies` (and
|
|
125
|
+
* `rls.junctions`) is exactly what `db push` and every server start write.
|
|
126
|
+
* This only compares it with `pg_policies`. A policy in both keeps the
|
|
127
|
+
* database's fields — this list is what Postgres enforces — and carries the
|
|
128
|
+
* declaration beside them, with where the two differ.
|
|
129
|
+
*
|
|
130
|
+
* @param live The table's `pg_policies` rows.
|
|
131
|
+
* @param declared What the server serves for the table, if anything.
|
|
132
|
+
*/
|
|
133
|
+
function listPolicies(live, declared, tableName) {
|
|
134
|
+
const listed = /* @__PURE__ */ new Map();
|
|
135
|
+
for (const policy of live) listed.set(policy.policyname, {
|
|
136
|
+
...policy,
|
|
137
|
+
status: "live"
|
|
138
|
+
});
|
|
139
|
+
if (!declared) return [...listed.values()];
|
|
140
|
+
const livePolicies = new Map(live.map((policy) => [policy.policyname, policy]));
|
|
141
|
+
const compiledRules = /* @__PURE__ */ new Set();
|
|
142
|
+
for (const served of declared.policies) {
|
|
143
|
+
if (served.rule !== void 0) compiledRules.add(served.rule);
|
|
144
|
+
const existing = livePolicies.get(served.name);
|
|
145
|
+
if (served.roles.includes(REBASE_ADMIN_REQUEST_ROLE) && !existing) continue;
|
|
146
|
+
const compiled = servedAsPostgresPolicy(served, tableName);
|
|
147
|
+
const declaredPolicy = existing !== void 0 && sameRoles$1(compiled.roles, [REBASE_ADMIN_ROLE]) && sameRoles$1(existing.roles, ["public"]) ? {
|
|
148
|
+
...compiled,
|
|
149
|
+
roles: ["public"]
|
|
150
|
+
} : compiled;
|
|
151
|
+
const rule = served.rule !== void 0 ? { rule: served.rule } : {};
|
|
152
|
+
if (existing) listed.set(served.name, {
|
|
153
|
+
...existing,
|
|
154
|
+
status: "both",
|
|
155
|
+
declared: declaredPolicy,
|
|
156
|
+
drift: policyDrift(declaredPolicy, existing),
|
|
157
|
+
generated: served.injected,
|
|
158
|
+
...rule
|
|
159
|
+
});
|
|
160
|
+
else listed.set(served.name, {
|
|
161
|
+
...declaredPolicy,
|
|
162
|
+
status: "code_only",
|
|
163
|
+
generated: served.injected,
|
|
164
|
+
...rule
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
declared.rules.forEach((rule, index) => {
|
|
168
|
+
if (compiledRules.has(index) || isRedactedSecurityRule(rule)) return;
|
|
169
|
+
const operations = getPolicyOperations(rule);
|
|
170
|
+
getPolicyNamesForRule(rule, tableName).forEach((name, position) => {
|
|
171
|
+
const existing = listed.get(name);
|
|
172
|
+
if (existing) {
|
|
173
|
+
if (existing.status === "live") listed.set(name, {
|
|
174
|
+
...existing,
|
|
175
|
+
status: "both",
|
|
176
|
+
generated: false,
|
|
177
|
+
rule: index
|
|
178
|
+
});
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
listed.set(name, {
|
|
182
|
+
...uncompiledPolicy(rule, operations[position] ?? "all", name, tableName),
|
|
183
|
+
status: "code_only",
|
|
184
|
+
generated: false,
|
|
185
|
+
rule: index
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
});
|
|
189
|
+
return [...listed.values()];
|
|
190
|
+
}
|
|
191
|
+
//#endregion
|
|
192
|
+
//#region src/components/RLSEditor/policy-presets.ts
|
|
193
|
+
/**
|
|
194
|
+
* What the RLS editor offers before the user types anything: the roles a `TO`
|
|
195
|
+
* list may name, and the ready-made policies.
|
|
196
|
+
*
|
|
197
|
+
* Separated from the component because these are the parts that have to agree
|
|
198
|
+
* with the *server*, not with the UI — and the ways they can silently disagree
|
|
199
|
+
* are the reason this file has tests.
|
|
200
|
+
*/
|
|
201
|
+
var COMMAND_OPTIONS = [
|
|
202
|
+
"ALL",
|
|
203
|
+
"SELECT",
|
|
204
|
+
"INSERT",
|
|
205
|
+
"UPDATE",
|
|
206
|
+
"DELETE"
|
|
207
|
+
];
|
|
208
|
+
/**
|
|
209
|
+
* The roles a policy's `TO` list can name when nothing better is known.
|
|
210
|
+
*
|
|
211
|
+
* Not `authenticated` / `anon` / `admin`, which is what this list used to be.
|
|
212
|
+
* The first two are Supabase's role names and Rebase never creates them, so
|
|
213
|
+
* `CREATE POLICY ... TO authenticated` — which is literally what the editor
|
|
214
|
+
* builds and executes — fails with `role "authenticated" does not exist`. The
|
|
215
|
+
* driver's `validatePolicyPgRoles` rejects the same three names in
|
|
216
|
+
* `SecurityRule.pgRoles` and says why: they are another platform's convention,
|
|
217
|
+
* and application roles belong in a condition, not in the `TO` list.
|
|
218
|
+
*
|
|
219
|
+
* `admin` was the more dangerous entry, because it is a plausible *application*
|
|
220
|
+
* role. Had one existed as a database role too, the policy would have been
|
|
221
|
+
* created successfully and then matched nothing at all: requests run as
|
|
222
|
+
* {@link REBASE_USER_ROLE} after a `SET LOCAL ROLE`, and a `TO` list naming a
|
|
223
|
+
* role the request never assumes filters every row without erroring.
|
|
224
|
+
*
|
|
225
|
+
* So: `public` (what the framework's own generator emits) and the role requests
|
|
226
|
+
* actually arrive as. "Signed-in users" and "admins" are not roles here — they
|
|
227
|
+
* are conditions over `rebase.uid()` and `rebase.roles()`, which is what the
|
|
228
|
+
* presets below now compile to.
|
|
229
|
+
*/
|
|
230
|
+
var FALLBACK_ROLE_OPTIONS = ["public", REBASE_USER_ROLE];
|
|
231
|
+
/**
|
|
232
|
+
* The `TO` list the editor offers, given what the database reported and what
|
|
233
|
+
* the policy being edited already targets.
|
|
234
|
+
*
|
|
235
|
+
* Three sources, and each is there for a reason the others do not cover:
|
|
236
|
+
*
|
|
237
|
+
* - {@link FALLBACK_ROLE_OPTIONS} always, because `public` is a **keyword, not
|
|
238
|
+
* a row in `pg_roles`** — `fetchAvailableRoles` cannot return it, so seeding
|
|
239
|
+
* from the live fetch alone would drop the one role every generated policy
|
|
240
|
+
* targets.
|
|
241
|
+
* - the live roles, so an operator's own `app_read` is reachable without
|
|
242
|
+
* hand-editing SQL.
|
|
243
|
+
* - the edited policy's own roles, because a `MultiSelect` silently drops a
|
|
244
|
+
* value with no matching item. That value is the `TO` list of a policy
|
|
245
|
+
* already enforcing something, so opening an unrelated policy for a one-word
|
|
246
|
+
* rename would have quietly rewritten who it applies to.
|
|
247
|
+
*/
|
|
248
|
+
function roleOptionsFor(fetched, policyRoles) {
|
|
249
|
+
return [.../* @__PURE__ */ new Set([
|
|
250
|
+
...FALLBACK_ROLE_OPTIONS,
|
|
251
|
+
...fetched ?? [],
|
|
252
|
+
...policyRoles ?? []
|
|
253
|
+
])];
|
|
254
|
+
}
|
|
255
|
+
/**
|
|
256
|
+
* Preset conditions, compiled by the framework's own policy compiler rather
|
|
257
|
+
* than written out here.
|
|
258
|
+
*
|
|
259
|
+
* `compilePolicyExpression` is the function `db push` generates policies with, so a
|
|
260
|
+
* preset cannot drift from what the framework emits — and the one that matters
|
|
261
|
+
* has drifted before: `authenticated()` used to compile to a bare
|
|
262
|
+
* `IS NOT NULL`, which is true for anonymous visitors, and any copy of that
|
|
263
|
+
* string sitting in a preset would still be handing out the old grant today.
|
|
264
|
+
*/
|
|
265
|
+
var SIGNED_IN_SQL = compilePolicyExpression(policy.authenticated());
|
|
266
|
+
var IS_ADMIN_SQL = compilePolicyExpression(policy.rolesOverlap(["admin"]));
|
|
267
|
+
var OWNS_ROW_SQL = compilePolicyExpression(policy.compare(policy.authUid(), "eq", policy.field("uid")));
|
|
268
|
+
/**
|
|
269
|
+
* Every preset targets `TO public`, which is what the framework's generator
|
|
270
|
+
* emits and what actually reaches a request running as `rebase_user`. The part
|
|
271
|
+
* that used to be expressed as a role — "authenticated", "admin" — is a
|
|
272
|
+
* condition now, so it is enforced where Postgres will actually evaluate it.
|
|
273
|
+
*/
|
|
274
|
+
var POLICY_PRESETS = [
|
|
275
|
+
{
|
|
276
|
+
id: "public_read",
|
|
277
|
+
label: "Enable read access to everyone",
|
|
278
|
+
description: "Anyone can read data, regardless of authentication status.",
|
|
279
|
+
policyname: "Enable read access for all users",
|
|
280
|
+
cmd: "SELECT",
|
|
281
|
+
permissive: "PERMISSIVE",
|
|
282
|
+
roles: ["public"],
|
|
283
|
+
qual: "true",
|
|
284
|
+
with_check: ""
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
id: "auth_read",
|
|
288
|
+
label: "Enable read access for signed-in users only",
|
|
289
|
+
description: "Only signed-in users are allowed to read data. Anonymous requests carry a sentinel uid, so the condition excludes them explicitly.",
|
|
290
|
+
policyname: "Enable read access for signed-in users",
|
|
291
|
+
cmd: "SELECT",
|
|
292
|
+
permissive: "PERMISSIVE",
|
|
293
|
+
roles: ["public"],
|
|
294
|
+
qual: SIGNED_IN_SQL,
|
|
295
|
+
with_check: ""
|
|
296
|
+
},
|
|
297
|
+
{
|
|
298
|
+
id: "auth_insert",
|
|
299
|
+
label: "Enable insert for signed-in users only",
|
|
300
|
+
description: "Only signed-in users are allowed to insert new data.",
|
|
301
|
+
policyname: "Enable insert for signed-in users only",
|
|
302
|
+
cmd: "INSERT",
|
|
303
|
+
permissive: "PERMISSIVE",
|
|
304
|
+
roles: ["public"],
|
|
305
|
+
qual: "",
|
|
306
|
+
with_check: SIGNED_IN_SQL
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
id: "admin_all",
|
|
310
|
+
label: "Admins can do anything",
|
|
311
|
+
description: "Restricted to users holding the `admin` application role, matched inside the policy via rebase.roles().",
|
|
312
|
+
policyname: "Admins have full access",
|
|
313
|
+
cmd: "ALL",
|
|
314
|
+
permissive: "PERMISSIVE",
|
|
315
|
+
roles: ["public"],
|
|
316
|
+
qual: IS_ADMIN_SQL,
|
|
317
|
+
with_check: IS_ADMIN_SQL
|
|
318
|
+
},
|
|
319
|
+
{
|
|
320
|
+
id: "user_select_own",
|
|
321
|
+
label: "Users can read their own rows",
|
|
322
|
+
description: "Users can only read rows whose uid column matches their rebase.uid()",
|
|
323
|
+
policyname: "Users can select their own data",
|
|
324
|
+
cmd: "SELECT",
|
|
325
|
+
permissive: "PERMISSIVE",
|
|
326
|
+
roles: ["public"],
|
|
327
|
+
qual: OWNS_ROW_SQL,
|
|
328
|
+
with_check: ""
|
|
329
|
+
},
|
|
330
|
+
{
|
|
331
|
+
id: "user_update_own",
|
|
332
|
+
label: "Users can update their own rows",
|
|
333
|
+
description: "Users can only update rows whose uid column matches their rebase.uid()",
|
|
334
|
+
policyname: "Users can update their own data",
|
|
335
|
+
cmd: "UPDATE",
|
|
336
|
+
permissive: "PERMISSIVE",
|
|
337
|
+
roles: ["public"],
|
|
338
|
+
qual: OWNS_ROW_SQL,
|
|
339
|
+
with_check: OWNS_ROW_SQL
|
|
340
|
+
},
|
|
341
|
+
{
|
|
342
|
+
id: "user_delete_own",
|
|
343
|
+
label: "Users can delete their own rows",
|
|
344
|
+
description: "Users can only delete rows whose uid column matches their rebase.uid()",
|
|
345
|
+
policyname: "Users can delete their own data",
|
|
346
|
+
cmd: "DELETE",
|
|
347
|
+
permissive: "PERMISSIVE",
|
|
348
|
+
roles: ["public"],
|
|
349
|
+
qual: OWNS_ROW_SQL,
|
|
350
|
+
with_check: ""
|
|
351
|
+
}
|
|
352
|
+
];
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region src/components/RLSEditor/PolicyEditor.tsx
|
|
355
|
+
var PolicyEditor = ({ policy, schema, table, roleOptions, onSave, onCancel }) => {
|
|
356
|
+
const { t } = useTranslation();
|
|
357
|
+
const [name, setName] = useState("");
|
|
358
|
+
const [helpOpen, setHelpOpen] = useState(false);
|
|
359
|
+
const [behavior, setBehavior] = useState("PERMISSIVE");
|
|
360
|
+
const [command, setCommand] = useState("ALL");
|
|
361
|
+
const [roles, setRoles] = useState(["public"]);
|
|
362
|
+
const [usingExpr, setUsingExpr] = useState("");
|
|
363
|
+
const [checkExpr, setCheckExpr] = useState("");
|
|
364
|
+
const [selectedPreset, setSelectedPreset] = useState("");
|
|
365
|
+
const availableRoles = useMemo(() => roleOptionsFor(roleOptions, policy?.roles), [roleOptions, policy]);
|
|
366
|
+
useEffect(() => {
|
|
367
|
+
if (policy) {
|
|
368
|
+
setName(policy.policyname || "");
|
|
369
|
+
setBehavior(policy.permissive || "PERMISSIVE");
|
|
370
|
+
setCommand(policy.cmd || "ALL");
|
|
371
|
+
const initialRoles = policy.roles ? Array.isArray(policy.roles) ? policy.roles : [policy.roles] : ["public"];
|
|
372
|
+
setRoles(initialRoles);
|
|
373
|
+
setUsingExpr(policy.qual || "");
|
|
374
|
+
setCheckExpr(policy.with_check || "");
|
|
375
|
+
} else {
|
|
376
|
+
setName("");
|
|
377
|
+
setBehavior("PERMISSIVE");
|
|
378
|
+
setCommand("ALL");
|
|
379
|
+
setRoles(["public"]);
|
|
380
|
+
setUsingExpr("");
|
|
381
|
+
setCheckExpr("");
|
|
382
|
+
setSelectedPreset("");
|
|
383
|
+
}
|
|
384
|
+
}, [policy]);
|
|
385
|
+
const handlePresetChange = (presetId) => {
|
|
386
|
+
const preset = POLICY_PRESETS.find((p) => p.id === presetId);
|
|
387
|
+
if (!preset) return;
|
|
388
|
+
setSelectedPreset(presetId);
|
|
389
|
+
setName(preset.policyname);
|
|
390
|
+
setBehavior(preset.permissive);
|
|
391
|
+
setCommand(preset.cmd);
|
|
392
|
+
setRoles(preset.roles);
|
|
393
|
+
setUsingExpr(preset.qual);
|
|
394
|
+
setCheckExpr(preset.with_check);
|
|
395
|
+
};
|
|
396
|
+
const showCheck = command === "ALL" || command === "INSERT" || command === "UPDATE";
|
|
397
|
+
/**
|
|
398
|
+
* The two expression editors. Save reads what they hold now: an edit
|
|
399
|
+
* reaches `usingExpr` and `checkExpr` only when this re-renders, so a Save
|
|
400
|
+
* right after a paste wrote the expression from before it.
|
|
401
|
+
*/
|
|
402
|
+
const usingEditor = useRef(null);
|
|
403
|
+
const checkEditor = useRef(null);
|
|
404
|
+
const handleSave = () => {
|
|
405
|
+
onSave({
|
|
406
|
+
policyname: name,
|
|
407
|
+
permissive: behavior,
|
|
408
|
+
cmd: command,
|
|
409
|
+
roles,
|
|
410
|
+
qual: usingEditor.current?.getValue() ?? usingExpr,
|
|
411
|
+
with_check: showCheck ? checkEditor.current?.getValue() ?? checkExpr : null
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
415
|
+
/* @__PURE__ */ jsxs(Typography, {
|
|
416
|
+
variant: "h6",
|
|
417
|
+
gutterBottom: true,
|
|
418
|
+
className: "mt-8 mx-8 flex justify-between items-center w-full",
|
|
419
|
+
children: [/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx("div", { children: policy ? t("studio_policy_edit") : t("studio_policy_create") }), /* @__PURE__ */ jsxs("div", {
|
|
420
|
+
className: "text-sm font-normal text-text-secondary dark:text-text-secondary-dark tracking-wide mt-1",
|
|
421
|
+
children: [
|
|
422
|
+
t("studio_policy_defining_rules"),
|
|
423
|
+
" ",
|
|
424
|
+
/* @__PURE__ */ jsxs("span", {
|
|
425
|
+
className: "font-mono text-primary bg-primary-bg px-1 py-0.5 rounded",
|
|
426
|
+
children: [
|
|
427
|
+
schema,
|
|
428
|
+
".",
|
|
429
|
+
table
|
|
430
|
+
]
|
|
431
|
+
})
|
|
432
|
+
]
|
|
433
|
+
})] }), /* @__PURE__ */ jsx(IconButton, {
|
|
434
|
+
"aria-label": t("help"),
|
|
435
|
+
size: "small",
|
|
436
|
+
onClick: () => setHelpOpen(true),
|
|
437
|
+
children: /* @__PURE__ */ jsx(HelpCircleIcon, { size: iconSize.smallest })
|
|
438
|
+
})]
|
|
439
|
+
}),
|
|
440
|
+
/* @__PURE__ */ jsx(DialogContent, {
|
|
441
|
+
className: "p-4 md:p-6 border-t border-hairline bg-surface-well",
|
|
442
|
+
includeMargin: false,
|
|
443
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
444
|
+
className: "max-w-4xl mx-auto",
|
|
445
|
+
children: [/* @__PURE__ */ jsxs(Paper, {
|
|
446
|
+
className: cls("p-4 md:p-6 flex flex-col gap-6 bg-surface-card border-none sm:border-solid rounded-none sm:rounded-xl", defaultBorderMixin),
|
|
447
|
+
children: [
|
|
448
|
+
!policy && /* @__PURE__ */ jsxs("div", {
|
|
449
|
+
className: "flex flex-col gap-1.5 bg-primary/5 p-3 sm:p-4 rounded-lg border border-primary/10 dark:border-primary/20",
|
|
450
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
451
|
+
variant: "caption",
|
|
452
|
+
className: "text-primary dark:text-primary-light uppercase tracking-wider",
|
|
453
|
+
children: t("studio_policy_template")
|
|
454
|
+
}), /* @__PURE__ */ jsx(Select, {
|
|
455
|
+
size: "small",
|
|
456
|
+
value: selectedPreset,
|
|
457
|
+
onValueChange: handlePresetChange,
|
|
458
|
+
position: "item-aligned",
|
|
459
|
+
placeholder: t("studio_policy_select_template"),
|
|
460
|
+
className: "bg-surface-card",
|
|
461
|
+
children: POLICY_PRESETS.map((preset) => /* @__PURE__ */ jsx(SelectItem, {
|
|
462
|
+
value: preset.id,
|
|
463
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
464
|
+
className: "flex flex-col text-left",
|
|
465
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
466
|
+
className: "text-sm",
|
|
467
|
+
children: preset.label
|
|
468
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
469
|
+
className: "text-xs text-text-secondary dark:text-text-secondary-dark",
|
|
470
|
+
children: preset.description
|
|
471
|
+
})]
|
|
472
|
+
})
|
|
473
|
+
}, preset.id))
|
|
474
|
+
})]
|
|
475
|
+
}),
|
|
476
|
+
/* @__PURE__ */ jsxs("div", {
|
|
477
|
+
className: "grid grid-cols-1 md:grid-cols-2 gap-4 sm:gap-6",
|
|
478
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
479
|
+
className: "flex flex-col gap-1.5",
|
|
480
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
481
|
+
variant: "caption",
|
|
482
|
+
className: "uppercase tracking-wider text-text-secondary",
|
|
483
|
+
children: t("studio_policy_name")
|
|
484
|
+
}), /* @__PURE__ */ jsx(TextField, {
|
|
485
|
+
value: name,
|
|
486
|
+
"aria-label": t("studio_policy_name"),
|
|
487
|
+
onChange: (e) => setName(e.target.value),
|
|
488
|
+
placeholder: t("studio_policy_name_placeholder"),
|
|
489
|
+
className: "w-full"
|
|
490
|
+
})]
|
|
491
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
492
|
+
className: "flex flex-col gap-1.5",
|
|
493
|
+
children: [/* @__PURE__ */ jsxs(Typography, {
|
|
494
|
+
variant: "caption",
|
|
495
|
+
className: "uppercase tracking-wider text-text-secondary",
|
|
496
|
+
children: [
|
|
497
|
+
t("studio_policy_behavior"),
|
|
498
|
+
" ",
|
|
499
|
+
/* @__PURE__ */ jsx("code", {
|
|
500
|
+
className: "text-xs bg-hairline text-text-secondary dark:text-text-secondary-dark px-1 py-0.5 rounded ml-1",
|
|
501
|
+
children: "AS"
|
|
502
|
+
})
|
|
503
|
+
]
|
|
504
|
+
}), /* @__PURE__ */ jsxs(Select, {
|
|
505
|
+
value: behavior,
|
|
506
|
+
onValueChange: (val) => setBehavior(val),
|
|
507
|
+
position: "item-aligned",
|
|
508
|
+
children: [/* @__PURE__ */ jsx(SelectItem, {
|
|
509
|
+
value: "PERMISSIVE",
|
|
510
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
511
|
+
className: "flex flex-col text-left",
|
|
512
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
513
|
+
className: "",
|
|
514
|
+
children: t("studio_policy_permissive")
|
|
515
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
516
|
+
className: "text-xs text-text-secondary dark:text-text-secondary-dark",
|
|
517
|
+
children: t("studio_policy_permissive_desc")
|
|
518
|
+
})]
|
|
519
|
+
})
|
|
520
|
+
}), /* @__PURE__ */ jsx(SelectItem, {
|
|
521
|
+
value: "RESTRICTIVE",
|
|
522
|
+
children: /* @__PURE__ */ jsxs("div", {
|
|
523
|
+
className: "flex flex-col text-left",
|
|
524
|
+
children: [/* @__PURE__ */ jsx("span", {
|
|
525
|
+
className: "",
|
|
526
|
+
children: t("studio_policy_restrictive")
|
|
527
|
+
}), /* @__PURE__ */ jsx("span", {
|
|
528
|
+
className: "text-xs text-text-secondary dark:text-text-secondary-dark",
|
|
529
|
+
children: t("studio_policy_restrictive_desc")
|
|
530
|
+
})]
|
|
531
|
+
})
|
|
532
|
+
})]
|
|
533
|
+
})]
|
|
534
|
+
})]
|
|
535
|
+
}),
|
|
536
|
+
/* @__PURE__ */ jsxs("div", {
|
|
537
|
+
className: "flex flex-col gap-1.5",
|
|
538
|
+
children: [/* @__PURE__ */ jsxs(Typography, {
|
|
539
|
+
variant: "caption",
|
|
540
|
+
className: "uppercase tracking-wider text-text-secondary",
|
|
541
|
+
children: [
|
|
542
|
+
t("studio_policy_command"),
|
|
543
|
+
" ",
|
|
544
|
+
/* @__PURE__ */ jsx("code", {
|
|
545
|
+
className: "text-xs bg-hairline text-text-secondary dark:text-text-secondary-dark px-1 py-0.5 rounded ml-1",
|
|
546
|
+
children: "FOR"
|
|
547
|
+
})
|
|
548
|
+
]
|
|
549
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
550
|
+
className: "flex flex-wrap gap-1.5",
|
|
551
|
+
children: COMMAND_OPTIONS.map((cmd) => /* @__PURE__ */ jsx(Button, {
|
|
552
|
+
size: "small",
|
|
553
|
+
variant: command === cmd ? "filled" : "text",
|
|
554
|
+
color: "neutral",
|
|
555
|
+
onClick: () => setCommand(cmd),
|
|
556
|
+
className: "min-w-[80px]",
|
|
557
|
+
children: cmd
|
|
558
|
+
}, cmd))
|
|
559
|
+
})]
|
|
560
|
+
}),
|
|
561
|
+
/* @__PURE__ */ jsxs("div", {
|
|
562
|
+
className: "flex flex-col gap-1.5",
|
|
563
|
+
children: [/* @__PURE__ */ jsxs(Typography, {
|
|
564
|
+
variant: "caption",
|
|
565
|
+
className: "uppercase tracking-wider text-text-secondary",
|
|
566
|
+
children: [
|
|
567
|
+
t("studio_policy_target_roles"),
|
|
568
|
+
" ",
|
|
569
|
+
/* @__PURE__ */ jsx("code", {
|
|
570
|
+
className: "text-xs bg-hairline text-text-secondary dark:text-text-secondary-dark px-1 py-0.5 rounded ml-1",
|
|
571
|
+
children: "TO"
|
|
572
|
+
})
|
|
573
|
+
]
|
|
574
|
+
}), /* @__PURE__ */ jsx(MultiSelect, {
|
|
575
|
+
size: "small",
|
|
576
|
+
value: roles,
|
|
577
|
+
onValueChange: setRoles,
|
|
578
|
+
placeholder: t("studio_policy_roles_placeholder"),
|
|
579
|
+
children: availableRoles.map((role) => /* @__PURE__ */ jsx(MultiSelectItem, {
|
|
580
|
+
value: role,
|
|
581
|
+
children: role
|
|
582
|
+
}, role))
|
|
583
|
+
})]
|
|
584
|
+
})
|
|
585
|
+
]
|
|
586
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
587
|
+
className: "mt-8 flex flex-col gap-4",
|
|
588
|
+
children: [command !== "INSERT" && /* @__PURE__ */ jsxs("div", {
|
|
589
|
+
className: "flex flex-col gap-1.5",
|
|
590
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
591
|
+
className: "flex flex-col gap-0.5",
|
|
592
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
593
|
+
variant: "caption",
|
|
594
|
+
className: "uppercase tracking-wider text-text-secondary",
|
|
595
|
+
children: t("studio_policy_using_expr")
|
|
596
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
597
|
+
variant: "caption",
|
|
598
|
+
className: "text-text-secondary opacity-70",
|
|
599
|
+
children: t("studio_policy_using_expr_desc")
|
|
600
|
+
})]
|
|
601
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
602
|
+
className: cls("h-32 border rounded-md overflow-hidden bg-surface-card", defaultBorderMixin),
|
|
603
|
+
children: /* @__PURE__ */ jsx(MonacoEditor, {
|
|
604
|
+
ref: usingEditor,
|
|
605
|
+
value: usingExpr,
|
|
606
|
+
onChange: (v) => setUsingExpr(v || ""),
|
|
607
|
+
readOnly: false,
|
|
608
|
+
autoFocus: true
|
|
609
|
+
})
|
|
610
|
+
})]
|
|
611
|
+
}), showCheck && /* @__PURE__ */ jsxs("div", {
|
|
612
|
+
className: "flex flex-col gap-1.5 mt-2",
|
|
613
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
614
|
+
className: "flex flex-col gap-0.5",
|
|
615
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
616
|
+
variant: "caption",
|
|
617
|
+
className: "uppercase tracking-wider text-text-secondary",
|
|
618
|
+
children: t("studio_policy_check_expr")
|
|
619
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
620
|
+
variant: "caption",
|
|
621
|
+
className: "text-text-secondary opacity-70",
|
|
622
|
+
children: t("studio_policy_check_expr_desc")
|
|
623
|
+
})]
|
|
624
|
+
}), /* @__PURE__ */ jsx("div", {
|
|
625
|
+
className: cls("h-32 border rounded-md overflow-hidden bg-surface-card", defaultBorderMixin),
|
|
626
|
+
children: /* @__PURE__ */ jsx(MonacoEditor, {
|
|
627
|
+
ref: checkEditor,
|
|
628
|
+
value: checkExpr,
|
|
629
|
+
onChange: (v) => setCheckExpr(v || ""),
|
|
630
|
+
readOnly: false,
|
|
631
|
+
autoFocus: command === "INSERT"
|
|
632
|
+
})
|
|
633
|
+
})]
|
|
634
|
+
})]
|
|
635
|
+
})]
|
|
636
|
+
})
|
|
637
|
+
}),
|
|
638
|
+
/* @__PURE__ */ jsxs(DialogActions, { children: [/* @__PURE__ */ jsx(Button, {
|
|
639
|
+
size: "small",
|
|
640
|
+
variant: "text",
|
|
641
|
+
color: "neutral",
|
|
642
|
+
onClick: onCancel,
|
|
643
|
+
children: t("studio_policy_cancel")
|
|
644
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
645
|
+
size: "small",
|
|
646
|
+
variant: "filled",
|
|
647
|
+
color: "primary",
|
|
648
|
+
onClick: handleSave,
|
|
649
|
+
disabled: !name,
|
|
650
|
+
children: t("studio_policy_save")
|
|
651
|
+
})] }),
|
|
652
|
+
/* @__PURE__ */ jsxs(Dialog, {
|
|
653
|
+
open: helpOpen,
|
|
654
|
+
onOpenChange: setHelpOpen,
|
|
655
|
+
maxWidth: "3xl",
|
|
656
|
+
children: [
|
|
657
|
+
/* @__PURE__ */ jsx(DialogTitle, {
|
|
658
|
+
hidden: true,
|
|
659
|
+
children: t("studio_policy_help_title")
|
|
660
|
+
}),
|
|
661
|
+
/* @__PURE__ */ jsxs(DialogContent, {
|
|
662
|
+
className: "p-4 sm:p-6 lg:p-8 flex flex-col gap-6",
|
|
663
|
+
children: [
|
|
664
|
+
/* @__PURE__ */ jsxs("div", { children: [/* @__PURE__ */ jsx(Typography, {
|
|
665
|
+
variant: "h5",
|
|
666
|
+
className: "mb-2",
|
|
667
|
+
children: t("studio_policy_help_title")
|
|
668
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
669
|
+
className: "text-text-secondary dark:text-text-secondary-dark",
|
|
670
|
+
children: t("studio_policy_help_intro")
|
|
671
|
+
})] }),
|
|
672
|
+
/* @__PURE__ */ jsxs("div", {
|
|
673
|
+
className: "flex flex-col gap-4",
|
|
674
|
+
children: [
|
|
675
|
+
/* @__PURE__ */ jsxs(Paper, {
|
|
676
|
+
className: cls("p-4 sm:p-5 flex flex-col gap-1", defaultBorderMixin),
|
|
677
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
678
|
+
variant: "subtitle2",
|
|
679
|
+
className: "text-primary dark:text-primary-light font-medium",
|
|
680
|
+
children: t("studio_policy_help_step1_title")
|
|
681
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
682
|
+
variant: "body2",
|
|
683
|
+
className: "text-text-secondary dark:text-text-secondary-dark",
|
|
684
|
+
children: t("studio_policy_help_step1_desc")
|
|
685
|
+
})]
|
|
686
|
+
}),
|
|
687
|
+
/* @__PURE__ */ jsxs(Paper, {
|
|
688
|
+
className: cls("p-4 sm:p-5 flex flex-col gap-1", defaultBorderMixin),
|
|
689
|
+
children: [
|
|
690
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
691
|
+
variant: "subtitle2",
|
|
692
|
+
className: "text-primary dark:text-primary-light font-medium",
|
|
693
|
+
children: t("studio_policy_help_step2_title")
|
|
694
|
+
}),
|
|
695
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
696
|
+
variant: "body2",
|
|
697
|
+
className: "text-text-secondary dark:text-text-secondary-dark mb-1",
|
|
698
|
+
children: t("studio_policy_help_step2_desc")
|
|
699
|
+
}),
|
|
700
|
+
/* @__PURE__ */ jsxs("ul", {
|
|
701
|
+
className: "list-disc pl-5 space-y-1 text-sm text-text-secondary dark:text-text-secondary-dark",
|
|
702
|
+
children: [/* @__PURE__ */ jsxs("li", { children: [
|
|
703
|
+
/* @__PURE__ */ jsx("strong", { children: "public" }),
|
|
704
|
+
": ",
|
|
705
|
+
t("studio_policy_help_role_public")
|
|
706
|
+
] }), /* @__PURE__ */ jsxs("li", { children: [
|
|
707
|
+
/* @__PURE__ */ jsx("strong", { children: REBASE_USER_ROLE }),
|
|
708
|
+
": ",
|
|
709
|
+
t("studio_policy_help_role_rebase_user")
|
|
710
|
+
] })]
|
|
711
|
+
}),
|
|
712
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
713
|
+
variant: "body2",
|
|
714
|
+
className: "text-text-secondary dark:text-text-secondary-dark mt-1",
|
|
715
|
+
children: t("studio_policy_help_roles_not_users")
|
|
716
|
+
})
|
|
717
|
+
]
|
|
718
|
+
}),
|
|
719
|
+
/* @__PURE__ */ jsxs(Paper, {
|
|
720
|
+
className: cls("p-4 sm:p-5 flex flex-col gap-1", defaultBorderMixin),
|
|
721
|
+
children: [
|
|
722
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
723
|
+
variant: "subtitle2",
|
|
724
|
+
className: "text-primary dark:text-primary-light font-medium",
|
|
725
|
+
children: t("studio_policy_help_step3_title")
|
|
726
|
+
}),
|
|
727
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
728
|
+
variant: "body2",
|
|
729
|
+
className: "text-text-secondary dark:text-text-secondary-dark mb-1",
|
|
730
|
+
children: t("studio_policy_help_step3_desc")
|
|
731
|
+
}),
|
|
732
|
+
/* @__PURE__ */ jsxs("div", {
|
|
733
|
+
className: cls("bg-surface-well px-3 py-2 rounded-md font-mono text-sm my-2", defaultBorderMixin),
|
|
734
|
+
children: [
|
|
735
|
+
t("studio_policy_help_example"),
|
|
736
|
+
" ",
|
|
737
|
+
RLS_UID_SQL,
|
|
738
|
+
" = uid"
|
|
739
|
+
]
|
|
740
|
+
}),
|
|
741
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
742
|
+
variant: "caption",
|
|
743
|
+
className: "text-text-secondary dark:text-text-secondary-dark",
|
|
744
|
+
children: t("studio_policy_help_step3_example")
|
|
745
|
+
})
|
|
746
|
+
]
|
|
747
|
+
}),
|
|
748
|
+
/* @__PURE__ */ jsxs(Paper, {
|
|
749
|
+
className: cls("p-4 sm:p-5 flex flex-col gap-1", defaultBorderMixin),
|
|
750
|
+
children: [
|
|
751
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
752
|
+
variant: "subtitle2",
|
|
753
|
+
className: "text-primary dark:text-primary-light font-medium",
|
|
754
|
+
children: t("studio_policy_help_step4_title")
|
|
755
|
+
}),
|
|
756
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
757
|
+
variant: "body2",
|
|
758
|
+
className: "text-text-secondary dark:text-text-secondary-dark mb-1",
|
|
759
|
+
children: t("studio_policy_help_step4_desc")
|
|
760
|
+
}),
|
|
761
|
+
/* @__PURE__ */ jsxs("div", {
|
|
762
|
+
className: cls("bg-surface-well px-3 py-2 rounded-md font-mono text-sm my-2", defaultBorderMixin),
|
|
763
|
+
children: [
|
|
764
|
+
t("studio_policy_help_example"),
|
|
765
|
+
" ",
|
|
766
|
+
RLS_UID_SQL,
|
|
767
|
+
" = uid"
|
|
768
|
+
]
|
|
769
|
+
}),
|
|
770
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
771
|
+
variant: "caption",
|
|
772
|
+
className: "text-text-secondary dark:text-text-secondary-dark",
|
|
773
|
+
children: t("studio_policy_help_step4_example")
|
|
774
|
+
})
|
|
775
|
+
]
|
|
776
|
+
}),
|
|
777
|
+
/* @__PURE__ */ jsxs(Paper, {
|
|
778
|
+
className: cls("p-4 sm:p-5 flex flex-col gap-2 bg-primary/5", defaultBorderMixin),
|
|
779
|
+
children: [
|
|
780
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
781
|
+
variant: "subtitle2",
|
|
782
|
+
className: "text-primary dark:text-primary-light font-medium",
|
|
783
|
+
children: t("studio_policy_help_auth_vars_title")
|
|
784
|
+
}),
|
|
785
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
786
|
+
variant: "body2",
|
|
787
|
+
className: "text-text-secondary dark:text-text-secondary-dark",
|
|
788
|
+
children: t("studio_policy_help_auth_vars_desc")
|
|
789
|
+
}),
|
|
790
|
+
/* @__PURE__ */ jsxs("ul", {
|
|
791
|
+
className: "list-disc pl-5 space-y-2 text-sm text-text-secondary dark:text-text-secondary-dark font-normal",
|
|
792
|
+
children: [
|
|
793
|
+
/* @__PURE__ */ jsxs("li", { children: [/* @__PURE__ */ jsx("code", {
|
|
794
|
+
className: "bg-surface-well px-1.5 py-0.5 rounded mr-1 whitespace-nowrap",
|
|
795
|
+
children: RLS_UID_SQL
|
|
796
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
797
|
+
className: "block mt-0.5",
|
|
798
|
+
children: [
|
|
799
|
+
t("studio_policy_help_auth_uid"),
|
|
800
|
+
" ",
|
|
801
|
+
/* @__PURE__ */ jsxs("code", {
|
|
802
|
+
className: "bg-surface-well px-1 py-0.5 rounded text-xs",
|
|
803
|
+
children: [RLS_UID_SQL, " = uid"]
|
|
804
|
+
})
|
|
805
|
+
]
|
|
806
|
+
})] }),
|
|
807
|
+
/* @__PURE__ */ jsxs("li", { children: [/* @__PURE__ */ jsx("code", {
|
|
808
|
+
className: "bg-surface-well px-1.5 py-0.5 rounded mr-1 whitespace-nowrap",
|
|
809
|
+
children: RLS_JWT_SQL
|
|
810
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
811
|
+
className: "block mt-0.5",
|
|
812
|
+
children: [
|
|
813
|
+
t("studio_policy_help_auth_jwt"),
|
|
814
|
+
" ",
|
|
815
|
+
/* @__PURE__ */ jsxs("code", {
|
|
816
|
+
className: "bg-surface-well px-1 py-0.5 rounded text-xs",
|
|
817
|
+
children: [RLS_JWT_SQL, " ->> 'email' = 'admin@example.com'"]
|
|
818
|
+
})
|
|
819
|
+
]
|
|
820
|
+
})] }),
|
|
821
|
+
/* @__PURE__ */ jsxs("li", { children: [/* @__PURE__ */ jsx("code", {
|
|
822
|
+
className: "bg-surface-well px-1.5 py-0.5 rounded mr-1 whitespace-nowrap",
|
|
823
|
+
children: RLS_ROLES_SQL
|
|
824
|
+
}), /* @__PURE__ */ jsxs("span", {
|
|
825
|
+
className: "block mt-0.5",
|
|
826
|
+
children: [
|
|
827
|
+
t("studio_policy_help_auth_roles"),
|
|
828
|
+
" ",
|
|
829
|
+
/* @__PURE__ */ jsxs("code", {
|
|
830
|
+
className: "bg-surface-well px-1 py-0.5 rounded text-xs",
|
|
831
|
+
children: [
|
|
832
|
+
"string_to_array(",
|
|
833
|
+
RLS_ROLES_SQL,
|
|
834
|
+
", ',') @> ARRAY['admin']"
|
|
835
|
+
]
|
|
836
|
+
})
|
|
837
|
+
]
|
|
838
|
+
})] })
|
|
839
|
+
]
|
|
840
|
+
})
|
|
841
|
+
]
|
|
842
|
+
})
|
|
843
|
+
]
|
|
844
|
+
}),
|
|
845
|
+
/* @__PURE__ */ jsxs("div", {
|
|
846
|
+
className: cls("mt-2 flex flex-col sm:flex-row justify-between items-start sm:items-center bg-primary/5 p-4 rounded-xl border border-primary/10 dark:border-primary/20", defaultBorderMixin),
|
|
847
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
848
|
+
variant: "body2",
|
|
849
|
+
className: "text-primary dark:text-primary-light mb-4 sm:mb-0 max-w-md",
|
|
850
|
+
children: t("studio_policy_help_docs_cta")
|
|
851
|
+
}), /* @__PURE__ */ jsx(Button, {
|
|
852
|
+
component: "a",
|
|
853
|
+
href: "https://www.postgresql.org/docs/current/sql-createpolicy.html",
|
|
854
|
+
target: "_blank",
|
|
855
|
+
variant: "outlined",
|
|
856
|
+
color: "primary",
|
|
857
|
+
size: "small",
|
|
858
|
+
className: "whitespace-nowrap flex-shrink-0",
|
|
859
|
+
children: t("studio_policy_help_read_docs")
|
|
860
|
+
})]
|
|
861
|
+
})
|
|
862
|
+
]
|
|
863
|
+
}),
|
|
864
|
+
/* @__PURE__ */ jsx(DialogActions, {
|
|
865
|
+
className: "p-4 sm:px-6 sm:pb-6 pt-0 border-t-0",
|
|
866
|
+
children: /* @__PURE__ */ jsx(Button, {
|
|
867
|
+
onClick: () => setHelpOpen(false),
|
|
868
|
+
variant: "filled",
|
|
869
|
+
color: "primary",
|
|
870
|
+
children: t("studio_policy_help_got_it")
|
|
871
|
+
})
|
|
872
|
+
})
|
|
873
|
+
]
|
|
874
|
+
})
|
|
875
|
+
] });
|
|
876
|
+
};
|
|
877
|
+
//#endregion
|
|
878
|
+
//#region src/components/RLSEditor/saveSecurityRules.ts
|
|
879
|
+
/**
|
|
880
|
+
* Write a table's rule edits to its source file: `{ patches }` to the
|
|
881
|
+
* source-only schema editor (`/admin/schema-editor/apply`).
|
|
882
|
+
*
|
|
883
|
+
* The schema-editor routes sit behind an admin gate that accepts a bearer token
|
|
884
|
+
* and nothing else — there is no cookie fallback — so a request without an
|
|
885
|
+
* `Authorization` header is a 401 every time. The RLS editor's two write paths
|
|
886
|
+
* ("Create policy" on a mapped table, and "Import to codebase") both sent none,
|
|
887
|
+
* and both reported the failure as a constant "Failed to save policy", which is
|
|
888
|
+
* also what a 501 and a parse error looked like.
|
|
889
|
+
*
|
|
890
|
+
* It lives in its own module rather than inline in the component so that the
|
|
891
|
+
* header can be asserted without rendering the whole RLS editor.
|
|
892
|
+
*/
|
|
893
|
+
async function postTablePatches(options) {
|
|
894
|
+
const { apiBase, patches, getAuthToken } = options;
|
|
895
|
+
const headers = { "Content-Type": "application/json" };
|
|
896
|
+
const token = getAuthToken ? await getAuthToken() : null;
|
|
897
|
+
if (token) headers["Authorization"] = `Bearer ${token}`;
|
|
898
|
+
const response = await fetch(`${apiBase}/admin/schema-editor/apply`, {
|
|
899
|
+
method: "POST",
|
|
900
|
+
headers,
|
|
901
|
+
body: JSON.stringify({ patches })
|
|
902
|
+
});
|
|
903
|
+
if (response.ok) return;
|
|
904
|
+
const body = await response.text().catch(() => "");
|
|
905
|
+
let message;
|
|
906
|
+
try {
|
|
907
|
+
const parsed = JSON.parse(body);
|
|
908
|
+
const error = typeof parsed === "object" && parsed !== null && "error" in parsed ? parsed.error : void 0;
|
|
909
|
+
message = typeof error === "object" && error !== null && "message" in error && typeof error.message === "string" ? error.message : void 0;
|
|
910
|
+
} catch {
|
|
911
|
+
message = body.trim() || void 0;
|
|
912
|
+
}
|
|
913
|
+
throw new Error(message ?? `Failed to save policy (HTTP ${response.status}).`);
|
|
914
|
+
}
|
|
915
|
+
//#endregion
|
|
916
|
+
//#region src/components/RLSEditor/saveRules.ts
|
|
917
|
+
/**
|
|
918
|
+
* Where a table's security rules go when the RLS editor saves them — the one
|
|
919
|
+
* function every RLS-editor write to the source goes through.
|
|
920
|
+
*
|
|
921
|
+
* The edit is sent as the rule operations it is (`TablePatch`): the rules the
|
|
922
|
+
* table declares, compared with the rules it should declare, by
|
|
923
|
+
* {@link rulePatches} — one `replaceRule` for an edited rule, one `addRule`
|
|
924
|
+
* for an imported one. A policy's name is a hash of its rule as written, so a
|
|
925
|
+
* rule the edit did not touch is never rewritten. A posture is `setRls`:
|
|
926
|
+
* removing the last rule is `"admin-only"`, never an empty list.
|
|
927
|
+
*
|
|
928
|
+
* A table whose rules are inherited from `defaultRls` keeps `"inherit"`
|
|
929
|
+
* unless the person deliberately detaches it: `confirmDetach` asks (naming
|
|
930
|
+
* what happens), and saying no writes nothing.
|
|
931
|
+
*
|
|
932
|
+
* There are two doors onto one file. With a collection editor mounted, the
|
|
933
|
+
* write goes through its plan/apply flow — planned, shown as SQL, confirmed,
|
|
934
|
+
* applied to the database — arriving over the Studio bridge as
|
|
935
|
+
* {@link StudioSchemaEditing} (`@rebasepro/cms` is a *peer* of
|
|
936
|
+
* `@rebasepro/studio`, which has to run without it). Without one, it goes to
|
|
937
|
+
* the source-only schema editor directly.
|
|
938
|
+
*
|
|
939
|
+
* A separate module from the component so the decision can be asserted
|
|
940
|
+
* without a database and a rendered editor.
|
|
941
|
+
*/
|
|
942
|
+
async function saveRules(schemaEditing, transport, table, rules, options) {
|
|
943
|
+
const patches = await rlsEdit(table, rules.map(asSecurityRule), options.confirmDetach);
|
|
944
|
+
if (patches.length === 0) return;
|
|
945
|
+
if (schemaEditing.available) {
|
|
946
|
+
await schemaEditing.applyTablePatches(table.slug, patches);
|
|
947
|
+
return;
|
|
948
|
+
}
|
|
949
|
+
await postTablePatches({
|
|
950
|
+
apiBase: transport.apiBase,
|
|
951
|
+
patches,
|
|
952
|
+
getAuthToken: transport.getAuthToken
|
|
953
|
+
});
|
|
954
|
+
}
|
|
955
|
+
/**
|
|
956
|
+
* A rule the policy editor built — its fields assigned one at a time, so
|
|
957
|
+
* wider than any one variant — as the `SecurityRule` variant its fields make
|
|
958
|
+
* it. Which fields go together is the variants' rule; the editor never sets
|
|
959
|
+
* two that exclude each other ({@link PolicyRule}).
|
|
960
|
+
*/
|
|
961
|
+
function asSecurityRule(rule) {
|
|
962
|
+
const { using, withCheck, ownerField, access, condition, check, redacted, ...rest } = rule;
|
|
963
|
+
const base = rest;
|
|
964
|
+
if (redacted) return {
|
|
965
|
+
...base,
|
|
966
|
+
redacted,
|
|
967
|
+
...access !== void 0 ? { access } : {}
|
|
968
|
+
};
|
|
969
|
+
if (using !== void 0) return {
|
|
970
|
+
...base,
|
|
971
|
+
using,
|
|
972
|
+
...withCheck !== void 0 ? { withCheck } : {}
|
|
973
|
+
};
|
|
974
|
+
if (withCheck !== void 0) return {
|
|
975
|
+
...base,
|
|
976
|
+
withCheck
|
|
977
|
+
};
|
|
978
|
+
if (ownerField !== void 0) return {
|
|
979
|
+
...base,
|
|
980
|
+
ownerField
|
|
981
|
+
};
|
|
982
|
+
if (access !== void 0) {
|
|
983
|
+
if (condition !== void 0) return {
|
|
984
|
+
...base,
|
|
985
|
+
access,
|
|
986
|
+
condition,
|
|
987
|
+
...check !== void 0 ? { check } : {}
|
|
988
|
+
};
|
|
989
|
+
if (check !== void 0) return {
|
|
990
|
+
...base,
|
|
991
|
+
access,
|
|
992
|
+
check
|
|
993
|
+
};
|
|
994
|
+
switch (access) {
|
|
995
|
+
case "public": return {
|
|
996
|
+
...base,
|
|
997
|
+
access: "public"
|
|
998
|
+
};
|
|
999
|
+
case "authenticated": return {
|
|
1000
|
+
...base,
|
|
1001
|
+
access: "authenticated"
|
|
1002
|
+
};
|
|
1003
|
+
case "registered": return {
|
|
1004
|
+
...base,
|
|
1005
|
+
access: "registered"
|
|
1006
|
+
};
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
if (condition !== void 0) return {
|
|
1010
|
+
...base,
|
|
1011
|
+
condition,
|
|
1012
|
+
...check !== void 0 ? { check } : {}
|
|
1013
|
+
};
|
|
1014
|
+
return base;
|
|
1015
|
+
}
|
|
1016
|
+
/** The person kept `"inherit"`: an answer, matched like the plan dialog's cancellation. */
|
|
1017
|
+
var DetachDeclined = class extends Error {
|
|
1018
|
+
constructor() {
|
|
1019
|
+
super("The table keeps its inherited rules.");
|
|
1020
|
+
this.name = "SchemaChangeCancelled";
|
|
1021
|
+
}
|
|
1022
|
+
};
|
|
1023
|
+
/**
|
|
1024
|
+
* The operations that make `table` declare `rules`.
|
|
1025
|
+
*
|
|
1026
|
+
* Exported for the tests: the decision is the whole of what this module adds.
|
|
1027
|
+
*/
|
|
1028
|
+
async function rlsEdit(table, rules, confirmDetach) {
|
|
1029
|
+
const declared = table.rls.posture === "rules" ? table.rls.rules : [];
|
|
1030
|
+
if (table.rls.inherited) {
|
|
1031
|
+
if (rules.length === 0 || JSON.stringify(rules) === JSON.stringify(declared)) return [];
|
|
1032
|
+
if (!await confirmDetach(table.slug)) throw new DetachDeclined();
|
|
1033
|
+
return [{
|
|
1034
|
+
op: "setRls",
|
|
1035
|
+
table: table.slug,
|
|
1036
|
+
rls: [rules[0], ...rules.slice(1)]
|
|
1037
|
+
}];
|
|
1038
|
+
}
|
|
1039
|
+
if (rules.length === 0) return declared.length === 0 ? [] : [{
|
|
1040
|
+
op: "setRls",
|
|
1041
|
+
table: table.slug,
|
|
1042
|
+
rls: "admin-only"
|
|
1043
|
+
}];
|
|
1044
|
+
if (declared.length === 0) return [{
|
|
1045
|
+
op: "setRls",
|
|
1046
|
+
table: table.slug,
|
|
1047
|
+
rls: [rules[0], ...rules.slice(1)]
|
|
1048
|
+
}];
|
|
1049
|
+
return rulePatches(table.slug, declared, rules);
|
|
1050
|
+
}
|
|
1051
|
+
/**
|
|
1052
|
+
* Was this rejection the person closing the plan dialog, or keeping a table's
|
|
1053
|
+
* inherited rules?
|
|
1054
|
+
*
|
|
1055
|
+
* Either is an answer, not a failure, and a caller that treats every
|
|
1056
|
+
* rejection as an error reports someone's own choice back to them in red — as
|
|
1057
|
+
* the collection editor once did, with "Error persisting collection: The schema
|
|
1058
|
+
* change was not applied."
|
|
1059
|
+
*
|
|
1060
|
+
* Matched by name rather than `instanceof`: `SchemaChangeCancelled` is declared
|
|
1061
|
+
* in `@rebasepro/cms`, which this package cannot import, and two copies of a
|
|
1062
|
+
* class in one bundle make `instanceof` silently false anyway.
|
|
1063
|
+
*/
|
|
1064
|
+
function isCancellation(err) {
|
|
1065
|
+
return err instanceof Error && err.name === "SchemaChangeCancelled";
|
|
1066
|
+
}
|
|
1067
|
+
//#endregion
|
|
1068
|
+
//#region src/components/RLSEditor/policyRules.ts
|
|
1069
|
+
/** The {@link PolicyRuleForm} of a declared rule. */
|
|
1070
|
+
function ruleForm(rule) {
|
|
1071
|
+
if (isRedactedSecurityRule(rule)) return "redacted";
|
|
1072
|
+
if (rule.using != null || rule.withCheck != null) return "raw";
|
|
1073
|
+
if (rule.ownerField != null) return "owner";
|
|
1074
|
+
if (rule.access !== void 0) return rule.condition != null || rule.check != null ? "conditional_access" : "access";
|
|
1075
|
+
if (rule.condition != null || rule.check != null) return "structured";
|
|
1076
|
+
return "roles_only";
|
|
1077
|
+
}
|
|
1078
|
+
/**
|
|
1079
|
+
* Write an edit of one policy back to the declared rule it compiles from.
|
|
1080
|
+
*
|
|
1081
|
+
* The rule is the one the server says the policy was compiled from (the
|
|
1082
|
+
* served policy's `rule` index, carried on the listed row) — the server is the
|
|
1083
|
+
* one compiler, so the editor never guesses it from names. A policy with no
|
|
1084
|
+
* rule is Rebase's own (`generated`: the admin baseline, tenancy, a junction's
|
|
1085
|
+
* derived policies) or exists only in the database.
|
|
1086
|
+
*
|
|
1087
|
+
* A rule over several operations is refused rather than split: taking one
|
|
1088
|
+
* operation out renames every policy the rule compiles to.
|
|
1089
|
+
*
|
|
1090
|
+
* @param declared The table's declared rules (`rls.rules`), as written.
|
|
1091
|
+
* @param original The listed policy the editor was opened with.
|
|
1092
|
+
*/
|
|
1093
|
+
function applyPolicyEdit(declared, original, edited) {
|
|
1094
|
+
const index = original.rule;
|
|
1095
|
+
if (index === void 0 || declared[index] === void 0) return {
|
|
1096
|
+
ok: false,
|
|
1097
|
+
reason: original.generated ? "generated" : "not_declared"
|
|
1098
|
+
};
|
|
1099
|
+
const rule = declared[index];
|
|
1100
|
+
if (ruleForm(rule) === "redacted") return {
|
|
1101
|
+
ok: false,
|
|
1102
|
+
reason: "redacted"
|
|
1103
|
+
};
|
|
1104
|
+
if (getPolicyOperations(rule).length > 1) return {
|
|
1105
|
+
ok: false,
|
|
1106
|
+
reason: "several_operations"
|
|
1107
|
+
};
|
|
1108
|
+
return {
|
|
1109
|
+
ok: true,
|
|
1110
|
+
rules: declared.map((r, i) => i === index ? editedRule(r, original, edited) : r)
|
|
1111
|
+
};
|
|
1112
|
+
}
|
|
1113
|
+
/**
|
|
1114
|
+
* The rule with what the editor changed, and nothing else.
|
|
1115
|
+
*
|
|
1116
|
+
* The editor shows a rule as a policy — name, command, mode, `TO` list and raw
|
|
1117
|
+
* USING / WITH CHECK — and an `ownerField` or `access` rule has no raw clause
|
|
1118
|
+
* to show. So a field is only written when it differs from what the editor was
|
|
1119
|
+
* opened with: renaming an `access: "public"` rule keeps it public, and an
|
|
1120
|
+
* unnamed rule stays unnamed unless the name was changed. A condition typed in
|
|
1121
|
+
* replaces the rule's condition whole — the forms are exclusive.
|
|
1122
|
+
*/
|
|
1123
|
+
function editedRule(rule, original, edited) {
|
|
1124
|
+
const next = policyToSecurityRule(edited);
|
|
1125
|
+
const result = { ...rule };
|
|
1126
|
+
if ((edited.policyname ?? "") !== original.policyname) if (next.name) result.name = next.name;
|
|
1127
|
+
else delete result.name;
|
|
1128
|
+
if (edited.cmd !== original.cmd) {
|
|
1129
|
+
delete result.operations;
|
|
1130
|
+
result.operation = next.operation;
|
|
1131
|
+
}
|
|
1132
|
+
if (edited.permissive !== original.permissive) result.mode = next.mode;
|
|
1133
|
+
if (!sameRoles(edited.roles, original.roles)) if (next.pgRoles) result.pgRoles = next.pgRoles;
|
|
1134
|
+
else delete result.pgRoles;
|
|
1135
|
+
if ((edited.qual || null) !== (original.qual || null) || (edited.with_check || null) !== (original.with_check || null)) {
|
|
1136
|
+
delete result.ownerField;
|
|
1137
|
+
delete result.access;
|
|
1138
|
+
delete result.condition;
|
|
1139
|
+
delete result.check;
|
|
1140
|
+
delete result.using;
|
|
1141
|
+
delete result.withCheck;
|
|
1142
|
+
if (next.using) result.using = next.using;
|
|
1143
|
+
if (next.withCheck) result.withCheck = next.withCheck;
|
|
1144
|
+
}
|
|
1145
|
+
return result;
|
|
1146
|
+
}
|
|
1147
|
+
function sameRoles(a, b) {
|
|
1148
|
+
const left = [...a ?? []].sort();
|
|
1149
|
+
const right = [...b ?? []].sort();
|
|
1150
|
+
return left.length === right.length && left.every((role, i) => role === right[i]);
|
|
1151
|
+
}
|
|
1152
|
+
//#endregion
|
|
1153
|
+
//#region src/components/RLSEditor/TablePolicies.tsx
|
|
1154
|
+
/**
|
|
1155
|
+
* Whether `descriptor` is the table the server declares for a live table: the
|
|
1156
|
+
* same SQL table in the same schema, as the server resolved both.
|
|
1157
|
+
*
|
|
1158
|
+
* Matched once by name, a table was taken for any collection whose `id`,
|
|
1159
|
+
* `path`, `table`, `slug` or `collectionId` equalled its name, in any schema.
|
|
1160
|
+
* So an unmapped table named like another table's slug, or a same-named table
|
|
1161
|
+
* in another schema, was taken for that table: it listed the declared
|
|
1162
|
+
* policies as missing from the table, and offered to import the table's own
|
|
1163
|
+
* policies into the declaration. A view is never a `pg_tables` row.
|
|
1164
|
+
*/
|
|
1165
|
+
function mapsTable(descriptor, table) {
|
|
1166
|
+
return descriptor.kind === "table" && descriptor.table === table.tableName && descriptor.schema === table.schemaName;
|
|
1167
|
+
}
|
|
1168
|
+
/**
|
|
1169
|
+
* A table the project's own schema maps is the CUSTOMER'S, whatever schema
|
|
1170
|
+
* Postgres put it in: Rebase creates a tenant's collection tables in the
|
|
1171
|
+
* `rebase` schema, so the schema can never decide this on its own. `users`
|
|
1172
|
+
* and `branches` are both platform tables AND, on a project that defines
|
|
1173
|
+
* them, collections the customer writes policies for. The mapping is the only
|
|
1174
|
+
* thing that knows, so it is asked first.
|
|
1175
|
+
*/
|
|
1176
|
+
function classifyTableClient(tableName, schemaName, isJunction, isMappedToCollection) {
|
|
1177
|
+
if (isMappedToCollection) return "collection";
|
|
1178
|
+
if (REBASE_INTERNAL_SCHEMAS.includes(schemaName) || REBASE_INTERNAL_PREFIXES.some((prefix) => tableName.startsWith(prefix))) return "internal";
|
|
1179
|
+
if (isJunction) return "junction";
|
|
1180
|
+
return "other";
|
|
1181
|
+
}
|
|
1182
|
+
/**
|
|
1183
|
+
* Every junction the server derives, with its policies, by `schema.table`. A
|
|
1184
|
+
* junction has no descriptor of its own: it is listed under each table that
|
|
1185
|
+
* declares a relation through it.
|
|
1186
|
+
*/
|
|
1187
|
+
function servedJunctionsOf(declaredTables) {
|
|
1188
|
+
const junctions = /* @__PURE__ */ new Map();
|
|
1189
|
+
for (const table of declaredTables) {
|
|
1190
|
+
for (const junction of table.rls.junctions ?? []) junctions.set(`${junction.schema}.${junction.table}`, junction);
|
|
1191
|
+
for (const relation of table.relations) {
|
|
1192
|
+
if (relation.kind !== "manyToMany") continue;
|
|
1193
|
+
const key = `${table.schema}.${relation.through.table}`;
|
|
1194
|
+
if (!junctions.has(key)) junctions.set(key, {
|
|
1195
|
+
table: relation.through.table,
|
|
1196
|
+
schema: table.schema,
|
|
1197
|
+
policies: []
|
|
1198
|
+
});
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
return junctions;
|
|
1202
|
+
}
|
|
1203
|
+
/** What the editor says when an edit has no declared rule to go back to. */
|
|
1204
|
+
var POLICY_EDIT_REFUSALS = {
|
|
1205
|
+
generated: "studio_rls_edit_generated",
|
|
1206
|
+
not_declared: "studio_rls_edit_not_declared",
|
|
1207
|
+
several_operations: "studio_rls_edit_several_operations",
|
|
1208
|
+
redacted: "studio_rls_edit_redacted"
|
|
1209
|
+
};
|
|
1210
|
+
/**
|
|
1211
|
+
* One table's row level security: the RLS switch, the policies Postgres
|
|
1212
|
+
* enforces merged with what the code declares (drift, unapplied), and the
|
|
1213
|
+
* editor that writes a policy where it belongs — the declaration when there
|
|
1214
|
+
* is a codebase, the database otherwise.
|
|
1215
|
+
*
|
|
1216
|
+
* The RLS view and the table inspector's Policies tab both render this, so
|
|
1217
|
+
* there is one place a policy is listed, edited and dropped.
|
|
1218
|
+
*/
|
|
1219
|
+
function TablePolicies({ schemaName, tableName, rlsEnabled, policies, isJunction = false, onChanged }) {
|
|
1220
|
+
const { databaseAdmin } = useRebaseContext();
|
|
1221
|
+
const snackbarController = useSnackbarController();
|
|
1222
|
+
const { t } = useTranslation();
|
|
1223
|
+
const descriptors = useTableDescriptors();
|
|
1224
|
+
const schemaProblem = schemaSourceProblem(descriptors);
|
|
1225
|
+
const declaredTables = descriptors.tables;
|
|
1226
|
+
const { codebase: hasCodebase } = useStudioCapabilities();
|
|
1227
|
+
const apiConfig = useApiConfig();
|
|
1228
|
+
const apiBase = useApiBase() ?? "/api";
|
|
1229
|
+
const schemaEditing = useStudioSchemaEditing();
|
|
1230
|
+
const reloadDeclared = descriptors.reload;
|
|
1231
|
+
const activeTableData = useMemo(() => ({
|
|
1232
|
+
schemaName,
|
|
1233
|
+
tableName
|
|
1234
|
+
}), [schemaName, tableName]);
|
|
1235
|
+
/**
|
|
1236
|
+
* Saving rules on a table that inherits them from `defaultRls` detaches it
|
|
1237
|
+
* into rules of its own: asked here, never done silently. Settled by the
|
|
1238
|
+
* dialog below — "OK" detaches, "Cancel" keeps `"inherit"`.
|
|
1239
|
+
*/
|
|
1240
|
+
const [detaching, setDetaching] = useState(null);
|
|
1241
|
+
const settleDetach = useRef(null);
|
|
1242
|
+
const confirmDetach = useCallback((slug) => new Promise((resolve) => {
|
|
1243
|
+
settleDetach.current?.(false);
|
|
1244
|
+
settleDetach.current = resolve;
|
|
1245
|
+
setDetaching(slug);
|
|
1246
|
+
}), []);
|
|
1247
|
+
const answerDetach = useCallback((detach) => {
|
|
1248
|
+
settleDetach.current?.(detach);
|
|
1249
|
+
settleDetach.current = null;
|
|
1250
|
+
setDetaching(null);
|
|
1251
|
+
}, []);
|
|
1252
|
+
/** Through the plan/apply dialog when there is one — see `saveRules`. */
|
|
1253
|
+
const saveSecurityRules = useCallback(async (table, securityRules) => {
|
|
1254
|
+
await saveRules(schemaEditing, {
|
|
1255
|
+
apiBase,
|
|
1256
|
+
getAuthToken: apiConfig?.getAuthToken
|
|
1257
|
+
}, table, securityRules, { confirmDetach });
|
|
1258
|
+
reloadDeclared();
|
|
1259
|
+
}, [
|
|
1260
|
+
apiBase,
|
|
1261
|
+
apiConfig,
|
|
1262
|
+
schemaEditing,
|
|
1263
|
+
reloadDeclared,
|
|
1264
|
+
confirmDetach
|
|
1265
|
+
]);
|
|
1266
|
+
/** Closing the plan dialog is an answer, not a failure — see `saveRules`. */
|
|
1267
|
+
const reportSaveFailure = useCallback((e) => {
|
|
1268
|
+
if (isCancellation(e)) return;
|
|
1269
|
+
snackbarController.open({
|
|
1270
|
+
type: "error",
|
|
1271
|
+
message: e instanceof Error ? e.message : String(e)
|
|
1272
|
+
});
|
|
1273
|
+
}, [snackbarController]);
|
|
1274
|
+
const [editingPolicy, setEditingPolicy] = useState(null);
|
|
1275
|
+
const [confirmToggleRls, setConfirmToggleRls] = useState(null);
|
|
1276
|
+
const [confirmDropPolicy, setConfirmDropPolicy] = useState(null);
|
|
1277
|
+
const [confirming, setConfirming] = useState(false);
|
|
1278
|
+
/**
|
|
1279
|
+
* Native PostgreSQL roles, for the policy editor's `TO` list.
|
|
1280
|
+
*
|
|
1281
|
+
* `fetchAvailableRoles` and not `fetchApplicationRoles`: a policy's `TO`
|
|
1282
|
+
* clause names database roles, and an application role put there produces
|
|
1283
|
+
* a policy that either fails to create or matches nothing.
|
|
1284
|
+
*/
|
|
1285
|
+
const [pgRoleOptions, setPgRoleOptions] = useState([]);
|
|
1286
|
+
useEffect(() => {
|
|
1287
|
+
let mounted = true;
|
|
1288
|
+
if (!databaseAdmin?.fetchAvailableRoles) return;
|
|
1289
|
+
databaseAdmin.fetchAvailableRoles().then((roles) => {
|
|
1290
|
+
if (mounted) setPgRoleOptions(roles);
|
|
1291
|
+
}).catch((e) => console.error("Failed to fetch database roles", e));
|
|
1292
|
+
return () => {
|
|
1293
|
+
mounted = false;
|
|
1294
|
+
};
|
|
1295
|
+
}, [databaseAdmin]);
|
|
1296
|
+
const qualifiedTable = `${quoteIdentifier(schemaName)}.${quoteIdentifier(tableName)}`;
|
|
1297
|
+
const applyToggleRls = useCallback(async () => {
|
|
1298
|
+
if (!confirmToggleRls || !databaseAdmin?.executeSql) return;
|
|
1299
|
+
const action = confirmToggleRls.enabled ? "DISABLE" : "ENABLE";
|
|
1300
|
+
setConfirming(true);
|
|
1301
|
+
try {
|
|
1302
|
+
await databaseAdmin.executeSql(`ALTER TABLE ${qualifiedTable} ${action} ROW LEVEL SECURITY`);
|
|
1303
|
+
snackbarController.open({
|
|
1304
|
+
type: "success",
|
|
1305
|
+
message: t(confirmToggleRls.enabled ? "studio_tables_rls_disabled_on" : "studio_tables_rls_enabled_on", { name: tableName })
|
|
1306
|
+
});
|
|
1307
|
+
setConfirmToggleRls(null);
|
|
1308
|
+
onChanged();
|
|
1309
|
+
} catch (e) {
|
|
1310
|
+
snackbarController.open({
|
|
1311
|
+
type: "error",
|
|
1312
|
+
message: e instanceof Error ? e.message : String(e)
|
|
1313
|
+
});
|
|
1314
|
+
} finally {
|
|
1315
|
+
setConfirming(false);
|
|
1316
|
+
}
|
|
1317
|
+
}, [
|
|
1318
|
+
confirmToggleRls,
|
|
1319
|
+
databaseAdmin,
|
|
1320
|
+
qualifiedTable,
|
|
1321
|
+
snackbarController,
|
|
1322
|
+
t,
|
|
1323
|
+
tableName,
|
|
1324
|
+
onChanged
|
|
1325
|
+
]);
|
|
1326
|
+
const applyDropPolicy = useCallback(async () => {
|
|
1327
|
+
if (!confirmDropPolicy || !databaseAdmin?.executeSql) return;
|
|
1328
|
+
setConfirming(true);
|
|
1329
|
+
try {
|
|
1330
|
+
await databaseAdmin.executeSql(`DROP POLICY ${quoteIdentifier(confirmDropPolicy)} ON ${qualifiedTable}; DROP POLICY IF EXISTS ${quoteIdentifier(adminRequestPolicyName(confirmDropPolicy))} ON ${qualifiedTable}`);
|
|
1331
|
+
snackbarController.open({
|
|
1332
|
+
type: "success",
|
|
1333
|
+
message: t("studio_tables_policy_dropped", { name: confirmDropPolicy })
|
|
1334
|
+
});
|
|
1335
|
+
setConfirmDropPolicy(null);
|
|
1336
|
+
onChanged();
|
|
1337
|
+
} catch (e) {
|
|
1338
|
+
snackbarController.open({
|
|
1339
|
+
type: "error",
|
|
1340
|
+
message: e instanceof Error ? e.message : String(e)
|
|
1341
|
+
});
|
|
1342
|
+
} finally {
|
|
1343
|
+
setConfirming(false);
|
|
1344
|
+
}
|
|
1345
|
+
}, [
|
|
1346
|
+
confirmDropPolicy,
|
|
1347
|
+
databaseAdmin,
|
|
1348
|
+
qualifiedTable,
|
|
1349
|
+
snackbarController,
|
|
1350
|
+
t,
|
|
1351
|
+
onChanged
|
|
1352
|
+
]);
|
|
1353
|
+
const servedJunctions = useMemo(() => servedJunctionsOf(declaredTables), [declaredTables]);
|
|
1354
|
+
/** The table the server declares for this one, if any. */
|
|
1355
|
+
const activeDescriptor = useMemo(() => declaredTables.find((descriptor) => mapsTable(descriptor, activeTableData)) ?? null, [activeTableData, declaredTables]);
|
|
1356
|
+
/**
|
|
1357
|
+
* The junction a relation derives this table as, when it is one. Its
|
|
1358
|
+
* policies are Rebase's — derived from the endpoints' relations — and the
|
|
1359
|
+
* server re-creates them on every start, like a declared table's.
|
|
1360
|
+
*/
|
|
1361
|
+
const activeJunction = useMemo(() => {
|
|
1362
|
+
if (activeDescriptor) return null;
|
|
1363
|
+
return servedJunctions.get(`${schemaName}.${tableName}`) ?? null;
|
|
1364
|
+
}, [
|
|
1365
|
+
activeDescriptor,
|
|
1366
|
+
servedJunctions,
|
|
1367
|
+
schemaName,
|
|
1368
|
+
tableName
|
|
1369
|
+
]);
|
|
1370
|
+
const activeTableCategory = classifyTableClient(tableName, schemaName, Boolean(activeJunction) || isJunction, Boolean(activeDescriptor));
|
|
1371
|
+
/** Whether the server re-asserts this table's RLS on every start. */
|
|
1372
|
+
const activeTableManaged = Boolean(activeDescriptor || activeJunction);
|
|
1373
|
+
/**
|
|
1374
|
+
* What the server says the table's policies are — `undefined` when it
|
|
1375
|
+
* declares none, or withheld them from this caller.
|
|
1376
|
+
*/
|
|
1377
|
+
const activeDeclared = useMemo(() => {
|
|
1378
|
+
if (activeDescriptor) return declaredPoliciesOf(activeDescriptor.rls);
|
|
1379
|
+
if (activeJunction && activeJunction.policies.length > 0) return {
|
|
1380
|
+
rules: [],
|
|
1381
|
+
policies: activeJunction.policies
|
|
1382
|
+
};
|
|
1383
|
+
}, [activeDescriptor, activeJunction]);
|
|
1384
|
+
/**
|
|
1385
|
+
* The table is declared, but this caller is not shown what it compiles to
|
|
1386
|
+
* (no `schema:read`): the list is what the database holds, and nothing is
|
|
1387
|
+
* written to the declaration — its raw rules may be redacted.
|
|
1388
|
+
*/
|
|
1389
|
+
const declaredWithheld = activeDescriptor !== null && activeDeclared === void 0;
|
|
1390
|
+
/** A save to the declaration needs the declaration, and a codebase to write it into. */
|
|
1391
|
+
const savesToCodebase = activeDescriptor !== null && hasCodebase;
|
|
1392
|
+
/**
|
|
1393
|
+
* What Postgres enforces on the table, and what the code declares that it
|
|
1394
|
+
* does not have yet. A policy in both is listed with the *database's*
|
|
1395
|
+
* fields, and the declaration rides beside it with where the two differ.
|
|
1396
|
+
*/
|
|
1397
|
+
const mergedPolicies = useMemo(() => listPolicies([...policies], activeDeclared, tableName).sort((a, b) => a.policyname.localeCompare(b.policyname)), [
|
|
1398
|
+
policies,
|
|
1399
|
+
activeDeclared,
|
|
1400
|
+
tableName
|
|
1401
|
+
]);
|
|
1402
|
+
const handleSave = async (newPolicy) => {
|
|
1403
|
+
if (!editingPolicy) return;
|
|
1404
|
+
if (savesToCodebase && activeDescriptor) {
|
|
1405
|
+
if (declaredWithheld) {
|
|
1406
|
+
snackbarController.open({
|
|
1407
|
+
type: "error",
|
|
1408
|
+
message: t("studio_rls_save_needs_schema_read", { table: tableName })
|
|
1409
|
+
});
|
|
1410
|
+
return;
|
|
1411
|
+
}
|
|
1412
|
+
const existingRules = activeDescriptor.rls.rules;
|
|
1413
|
+
let newRules;
|
|
1414
|
+
if (editingPolicy === "new") newRules = [...existingRules, policyToSecurityRule(newPolicy)];
|
|
1415
|
+
else {
|
|
1416
|
+
const edit = applyPolicyEdit(existingRules, editingPolicy, newPolicy);
|
|
1417
|
+
if (!edit.ok) {
|
|
1418
|
+
snackbarController.open({
|
|
1419
|
+
type: "error",
|
|
1420
|
+
message: t(POLICY_EDIT_REFUSALS[edit.reason], { policy: editingPolicy.policyname })
|
|
1421
|
+
});
|
|
1422
|
+
return;
|
|
1423
|
+
}
|
|
1424
|
+
newRules = edit.rules;
|
|
1425
|
+
if (JSON.stringify(newRules) === JSON.stringify(existingRules)) {
|
|
1426
|
+
snackbarController.open({
|
|
1427
|
+
type: "info",
|
|
1428
|
+
message: t("studio_rls_nothing_changed")
|
|
1429
|
+
});
|
|
1430
|
+
return;
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
try {
|
|
1434
|
+
await saveSecurityRules(activeDescriptor, newRules);
|
|
1435
|
+
snackbarController.open({
|
|
1436
|
+
type: "success",
|
|
1437
|
+
message: t("studio_tables_policy_saved")
|
|
1438
|
+
});
|
|
1439
|
+
setEditingPolicy(null);
|
|
1440
|
+
onChanged();
|
|
1441
|
+
} catch (e) {
|
|
1442
|
+
reportSaveFailure(e);
|
|
1443
|
+
}
|
|
1444
|
+
return;
|
|
1445
|
+
}
|
|
1446
|
+
if (editingPolicy !== "new" && editingPolicy.generated) {
|
|
1447
|
+
snackbarController.open({
|
|
1448
|
+
type: "error",
|
|
1449
|
+
message: t(POLICY_EDIT_REFUSALS.generated, { policy: editingPolicy.policyname })
|
|
1450
|
+
});
|
|
1451
|
+
return;
|
|
1452
|
+
}
|
|
1453
|
+
if (!databaseAdmin?.executeSql) return;
|
|
1454
|
+
try {
|
|
1455
|
+
const policyName = quoteIdentifier(newPolicy.policyname || "unnamed_policy");
|
|
1456
|
+
const cmd = newPolicy.cmd || "ALL";
|
|
1457
|
+
const permissive = (newPolicy.permissive || "PERMISSIVE") === "PERMISSIVE" ? "PERMISSIVE" : "RESTRICTIVE";
|
|
1458
|
+
const roles = newPolicy.roles && newPolicy.roles.length > 0 ? newPolicy.roles.map((r) => quoteIdentifier(r)).join(", ") : "public";
|
|
1459
|
+
let sql = `CREATE POLICY ${policyName} ON ${qualifiedTable} AS ${permissive} FOR ${cmd} TO ${roles}`;
|
|
1460
|
+
if (newPolicy.qual) sql += ` USING (${newPolicy.qual})`;
|
|
1461
|
+
if (newPolicy.with_check) sql += ` WITH CHECK (${newPolicy.with_check})`;
|
|
1462
|
+
if (editingPolicy !== "new") {
|
|
1463
|
+
const previousName = quoteIdentifier(editingPolicy.policyname);
|
|
1464
|
+
const companion = quoteIdentifier(adminRequestPolicyName(editingPolicy.policyname));
|
|
1465
|
+
sql = `DROP POLICY IF EXISTS ${previousName} ON ${qualifiedTable}; DROP POLICY IF EXISTS ${companion} ON ${qualifiedTable}; ${sql}`;
|
|
1466
|
+
}
|
|
1467
|
+
await databaseAdmin.executeSql(sql);
|
|
1468
|
+
snackbarController.open({
|
|
1469
|
+
type: "success",
|
|
1470
|
+
message: t("studio_tables_policy_applied", { name: newPolicy.policyname ?? "" })
|
|
1471
|
+
});
|
|
1472
|
+
setEditingPolicy(null);
|
|
1473
|
+
onChanged();
|
|
1474
|
+
} catch (e) {
|
|
1475
|
+
snackbarController.open({
|
|
1476
|
+
type: "error",
|
|
1477
|
+
message: e instanceof Error ? e.message : String(e)
|
|
1478
|
+
});
|
|
1479
|
+
}
|
|
1480
|
+
};
|
|
1481
|
+
const importToCodebase = async (policy) => {
|
|
1482
|
+
if (!activeDescriptor) return;
|
|
1483
|
+
const rule = policyToSecurityRule(policy);
|
|
1484
|
+
try {
|
|
1485
|
+
await saveSecurityRules(activeDescriptor, [...activeDescriptor.rls.rules, rule]);
|
|
1486
|
+
snackbarController.open({
|
|
1487
|
+
type: "success",
|
|
1488
|
+
message: t("studio_tables_policy_imported")
|
|
1489
|
+
});
|
|
1490
|
+
onChanged();
|
|
1491
|
+
} catch (e) {
|
|
1492
|
+
reportSaveFailure(e);
|
|
1493
|
+
}
|
|
1494
|
+
};
|
|
1495
|
+
const renderPolicyTag = (label, value) => /* @__PURE__ */ jsxs("div", {
|
|
1496
|
+
className: "flex items-center gap-1.5 px-2 py-1 rounded-md bg-surface-field",
|
|
1497
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
1498
|
+
variant: "caption",
|
|
1499
|
+
color: "secondary",
|
|
1500
|
+
className: "font-medium",
|
|
1501
|
+
children: label
|
|
1502
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
1503
|
+
variant: "mono",
|
|
1504
|
+
className: "text-xs break-all",
|
|
1505
|
+
children: value
|
|
1506
|
+
})]
|
|
1507
|
+
});
|
|
1508
|
+
if (editingPolicy) return /* @__PURE__ */ jsx(PolicyEditor, {
|
|
1509
|
+
policy: editingPolicy === "new" ? void 0 : editingPolicy,
|
|
1510
|
+
schema: schemaName,
|
|
1511
|
+
table: tableName,
|
|
1512
|
+
roleOptions: pgRoleOptions,
|
|
1513
|
+
onSave: (policy) => void handleSave(policy),
|
|
1514
|
+
onCancel: () => setEditingPolicy(null)
|
|
1515
|
+
});
|
|
1516
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
1517
|
+
className: "flex flex-col gap-5",
|
|
1518
|
+
children: [
|
|
1519
|
+
/* @__PURE__ */ jsxs("div", {
|
|
1520
|
+
className: "flex items-center justify-between gap-3 flex-wrap",
|
|
1521
|
+
children: [/* @__PURE__ */ jsx(StatusDot, {
|
|
1522
|
+
tone: rlsEnabled ? "ok" : "busy",
|
|
1523
|
+
children: rlsEnabled ? t("studio_rls_enabled") : t("studio_rls_disabled")
|
|
1524
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
1525
|
+
className: "flex items-center gap-2",
|
|
1526
|
+
children: [/* @__PURE__ */ jsx(Button, {
|
|
1527
|
+
size: "small",
|
|
1528
|
+
variant: "outlined",
|
|
1529
|
+
onClick: () => setConfirmToggleRls({
|
|
1530
|
+
enabled: rlsEnabled,
|
|
1531
|
+
managed: activeTableManaged
|
|
1532
|
+
}),
|
|
1533
|
+
children: rlsEnabled ? t("studio_rls_disable_rls") : t("studio_rls_enable_rls")
|
|
1534
|
+
}), /* @__PURE__ */ jsxs(Button, {
|
|
1535
|
+
size: "small",
|
|
1536
|
+
color: "primary",
|
|
1537
|
+
variant: "filled",
|
|
1538
|
+
onClick: () => setEditingPolicy("new"),
|
|
1539
|
+
children: [/* @__PURE__ */ jsx(PlusIcon, { size: iconSize.smallest }), t("studio_rls_create_policy")]
|
|
1540
|
+
})]
|
|
1541
|
+
})]
|
|
1542
|
+
}),
|
|
1543
|
+
schemaProblem && /* @__PURE__ */ jsx(SchemaSourceProblemAlert, { problem: schemaProblem }),
|
|
1544
|
+
declaredWithheld && /* @__PURE__ */ jsx(Alert, {
|
|
1545
|
+
color: "info",
|
|
1546
|
+
children: /* @__PURE__ */ jsx(Typography, {
|
|
1547
|
+
variant: "caption",
|
|
1548
|
+
children: t("studio_rls_declared_withheld", { table: tableName })
|
|
1549
|
+
})
|
|
1550
|
+
}),
|
|
1551
|
+
activeTableManaged && !savesToCodebase && /* @__PURE__ */ jsx(Alert, {
|
|
1552
|
+
color: "info",
|
|
1553
|
+
children: /* @__PURE__ */ jsx(Typography, {
|
|
1554
|
+
variant: "caption",
|
|
1555
|
+
children: t("studio_rls_reapplied_on_start", { table: tableName })
|
|
1556
|
+
})
|
|
1557
|
+
}),
|
|
1558
|
+
activeTableCategory === "internal" && /* @__PURE__ */ jsxs(Alert, {
|
|
1559
|
+
color: "info",
|
|
1560
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
1561
|
+
variant: "body2",
|
|
1562
|
+
className: "mb-1 font-semibold",
|
|
1563
|
+
children: "Rebase System Table"
|
|
1564
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
1565
|
+
variant: "caption",
|
|
1566
|
+
className: "opacity-80",
|
|
1567
|
+
children: "This table is managed internally by Rebase. Its security policies are configured automatically. Editing policies on system tables is an advanced operation."
|
|
1568
|
+
})]
|
|
1569
|
+
}),
|
|
1570
|
+
activeTableCategory === "junction" && /* @__PURE__ */ jsxs(Alert, {
|
|
1571
|
+
color: "info",
|
|
1572
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
1573
|
+
variant: "body2",
|
|
1574
|
+
className: "mb-1 font-semibold",
|
|
1575
|
+
children: "Junction Table"
|
|
1576
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
1577
|
+
variant: "caption",
|
|
1578
|
+
className: "opacity-80",
|
|
1579
|
+
children: "This is an auto-generated junction table for a many-to-many relation. Rebase derives its policies: rows are readable when both related rows are, and writable following the declaring table's update rules (plus the server/admin baseline). You can still add RLS policies directly to broaden access."
|
|
1580
|
+
})]
|
|
1581
|
+
}),
|
|
1582
|
+
!rlsEnabled && /* @__PURE__ */ jsxs(Alert, {
|
|
1583
|
+
color: "warning",
|
|
1584
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
1585
|
+
variant: "subtitle2",
|
|
1586
|
+
children: "Row Level Security (RLS) is disabled"
|
|
1587
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
1588
|
+
variant: "body2",
|
|
1589
|
+
className: "mt-1",
|
|
1590
|
+
children: "Your table is completely readable and writable by anyone with access privileges. Enable RLS to create policies that restrict access to specific rows."
|
|
1591
|
+
})]
|
|
1592
|
+
}),
|
|
1593
|
+
mergedPolicies.length > 0 && /* @__PURE__ */ jsxs("div", {
|
|
1594
|
+
className: "flex flex-col gap-3",
|
|
1595
|
+
children: [/* @__PURE__ */ jsx(Typography, {
|
|
1596
|
+
variant: "subtitle2",
|
|
1597
|
+
children: t("studio_rls_policies")
|
|
1598
|
+
}), mergedPolicies.map((policy) => /* @__PURE__ */ jsxs(Paper, {
|
|
1599
|
+
className: cls("p-3 sm:px-4 sm:py-3 flex flex-col sm:flex-row sm:items-center justify-between gap-4 border rounded-lg", defaultBorderMixin),
|
|
1600
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
1601
|
+
className: "flex flex-col gap-2 min-w-0",
|
|
1602
|
+
children: [/* @__PURE__ */ jsxs("div", {
|
|
1603
|
+
className: "flex items-center gap-2 flex-wrap",
|
|
1604
|
+
children: [
|
|
1605
|
+
/* @__PURE__ */ jsx(KeyIcon, {
|
|
1606
|
+
size: iconSize.smallest,
|
|
1607
|
+
className: "text-text-secondary dark:text-text-secondary-dark shrink-0"
|
|
1608
|
+
}),
|
|
1609
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
1610
|
+
variant: "body2",
|
|
1611
|
+
className: "truncate",
|
|
1612
|
+
children: policy.policyname
|
|
1613
|
+
}),
|
|
1614
|
+
policy.status === "code_only" && /* @__PURE__ */ jsx(Tooltip, {
|
|
1615
|
+
title: t("studio_rls_unapplied_tooltip"),
|
|
1616
|
+
children: /* @__PURE__ */ jsx("span", { children: /* @__PURE__ */ jsx(StatusDot, {
|
|
1617
|
+
tone: "idle",
|
|
1618
|
+
size: "small",
|
|
1619
|
+
children: t("studio_rls_unapplied")
|
|
1620
|
+
}) })
|
|
1621
|
+
}),
|
|
1622
|
+
policy.drift && policy.drift.length > 0 && /* @__PURE__ */ jsx(Tooltip, {
|
|
1623
|
+
title: t("studio_rls_drift_tooltip", { fields: policy.drift.join(", ") }),
|
|
1624
|
+
children: /* @__PURE__ */ jsx("span", { children: /* @__PURE__ */ jsx(StatusDot, {
|
|
1625
|
+
tone: "busy",
|
|
1626
|
+
size: "small",
|
|
1627
|
+
children: `${t("studio_rls_drift")}: ${policy.drift.join(", ")}`
|
|
1628
|
+
}) })
|
|
1629
|
+
}),
|
|
1630
|
+
policy.status === "live" && hasCodebase && /* @__PURE__ */ jsx(Tooltip, {
|
|
1631
|
+
title: t("studio_tables_policy_db_only_tooltip"),
|
|
1632
|
+
children: /* @__PURE__ */ jsx("span", { children: /* @__PURE__ */ jsx(StatusDot, {
|
|
1633
|
+
tone: "busy",
|
|
1634
|
+
size: "small",
|
|
1635
|
+
children: t("studio_tables_policy_db_only")
|
|
1636
|
+
}) })
|
|
1637
|
+
})
|
|
1638
|
+
]
|
|
1639
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
1640
|
+
className: "flex flex-wrap gap-1.5",
|
|
1641
|
+
children: [
|
|
1642
|
+
renderPolicyTag(t("studio_tables_policy_action"), policy.cmd),
|
|
1643
|
+
renderPolicyTag(t("studio_tables_policy_roles"), Array.isArray(policy.roles) ? policy.roles.join(", ") : String(policy.roles)),
|
|
1644
|
+
policy.qual && renderPolicyTag("USING", policy.qual),
|
|
1645
|
+
policy.with_check && renderPolicyTag("WITH CHECK", policy.with_check)
|
|
1646
|
+
]
|
|
1647
|
+
})]
|
|
1648
|
+
}), /* @__PURE__ */ jsxs("div", {
|
|
1649
|
+
className: "flex gap-2 shrink-0 items-center",
|
|
1650
|
+
children: [
|
|
1651
|
+
policy.status === "live" && savesToCodebase && activeDescriptor && !declaredWithheld && /* @__PURE__ */ jsx(Button, {
|
|
1652
|
+
size: "small",
|
|
1653
|
+
variant: "outlined",
|
|
1654
|
+
onClick: () => void importToCodebase(policy),
|
|
1655
|
+
children: t("studio_tables_policy_import")
|
|
1656
|
+
}),
|
|
1657
|
+
/* @__PURE__ */ jsx(Button, {
|
|
1658
|
+
size: "small",
|
|
1659
|
+
variant: "text",
|
|
1660
|
+
onClick: () => setEditingPolicy(policy),
|
|
1661
|
+
children: t("studio_rls_edit")
|
|
1662
|
+
}),
|
|
1663
|
+
policy.status !== "code_only" && /* @__PURE__ */ jsx(Tooltip, {
|
|
1664
|
+
title: t("studio_rls_delete"),
|
|
1665
|
+
asChild: true,
|
|
1666
|
+
children: /* @__PURE__ */ jsx(IconButton, {
|
|
1667
|
+
"aria-label": t("studio_rls_delete"),
|
|
1668
|
+
size: "small",
|
|
1669
|
+
onClick: () => setConfirmDropPolicy(policy.policyname),
|
|
1670
|
+
children: /* @__PURE__ */ jsx(Trash2Icon, { size: iconSize.smallest })
|
|
1671
|
+
})
|
|
1672
|
+
})
|
|
1673
|
+
]
|
|
1674
|
+
})]
|
|
1675
|
+
}, policy.policyname))]
|
|
1676
|
+
}),
|
|
1677
|
+
mergedPolicies.length === 0 && rlsEnabled && /* @__PURE__ */ jsxs("div", {
|
|
1678
|
+
className: "flex flex-col items-center justify-center py-12 text-center gap-2",
|
|
1679
|
+
children: [
|
|
1680
|
+
/* @__PURE__ */ jsx(ShieldIcon, {
|
|
1681
|
+
size: 36,
|
|
1682
|
+
className: "text-text-disabled dark:text-text-disabled-dark"
|
|
1683
|
+
}),
|
|
1684
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
1685
|
+
variant: "subtitle2",
|
|
1686
|
+
color: "secondary",
|
|
1687
|
+
children: t("studio_tables_policies_none_title")
|
|
1688
|
+
}),
|
|
1689
|
+
/* @__PURE__ */ jsx(Typography, {
|
|
1690
|
+
variant: "caption",
|
|
1691
|
+
color: "secondary",
|
|
1692
|
+
className: "max-w-sm",
|
|
1693
|
+
children: t("studio_tables_policies_none_body")
|
|
1694
|
+
})
|
|
1695
|
+
]
|
|
1696
|
+
}),
|
|
1697
|
+
mergedPolicies.length === 0 && !rlsEnabled && activeDescriptor && /* @__PURE__ */ jsxs("div", {
|
|
1698
|
+
className: "flex flex-col items-center justify-center py-12 text-center gap-2",
|
|
1699
|
+
children: [/* @__PURE__ */ jsx(AlertTriangleIcon, {
|
|
1700
|
+
size: 36,
|
|
1701
|
+
className: "text-text-disabled dark:text-text-disabled-dark"
|
|
1702
|
+
}), /* @__PURE__ */ jsx(Typography, {
|
|
1703
|
+
variant: "subtitle2",
|
|
1704
|
+
color: "secondary",
|
|
1705
|
+
children: t("studio_tables_policies_no_access_control")
|
|
1706
|
+
})]
|
|
1707
|
+
}),
|
|
1708
|
+
/* @__PURE__ */ jsx(ConfirmationDialog, {
|
|
1709
|
+
open: confirmToggleRls !== null,
|
|
1710
|
+
loading: confirming,
|
|
1711
|
+
onAccept: applyToggleRls,
|
|
1712
|
+
onCancel: () => setConfirmToggleRls(null),
|
|
1713
|
+
title: confirmToggleRls?.enabled ? t("studio_rls_disable_confirm_title") : t("studio_rls_enable_confirm_title"),
|
|
1714
|
+
body: confirmToggleRls?.enabled ? [t("studio_rls_disable_confirm_body", { table: tableName }), confirmToggleRls.managed ? t("studio_rls_disable_reapplied", { table: tableName }) : ""].filter(Boolean).join(" ") : t("studio_rls_enable_confirm_body", { table: tableName })
|
|
1715
|
+
}),
|
|
1716
|
+
/* @__PURE__ */ jsx(ConfirmationDialog, {
|
|
1717
|
+
open: detaching !== null,
|
|
1718
|
+
onAccept: () => answerDetach(true),
|
|
1719
|
+
onCancel: () => answerDetach(false),
|
|
1720
|
+
title: t("schema_rules_detach_title"),
|
|
1721
|
+
body: t("schema_rules_detach_body", { table: detaching ?? "" })
|
|
1722
|
+
}),
|
|
1723
|
+
/* @__PURE__ */ jsx(ConfirmationDialog, {
|
|
1724
|
+
open: confirmDropPolicy !== null,
|
|
1725
|
+
loading: confirming,
|
|
1726
|
+
onAccept: applyDropPolicy,
|
|
1727
|
+
onCancel: () => setConfirmDropPolicy(null),
|
|
1728
|
+
title: t("studio_rls_drop_policy_title"),
|
|
1729
|
+
body: t("studio_rls_drop_policy_body", {
|
|
1730
|
+
policy: confirmDropPolicy ?? "",
|
|
1731
|
+
table: tableName
|
|
1732
|
+
})
|
|
1733
|
+
})
|
|
1734
|
+
]
|
|
1735
|
+
});
|
|
1736
|
+
}
|
|
1737
|
+
//#endregion
|
|
1738
|
+
export { SIGNED_IN_SQL as a, servedJunctionsOf as i, classifyTableClient as n, readPolicyRoles as o, mapsTable as r, TablePolicies as t };
|
|
1739
|
+
|
|
1740
|
+
//# sourceMappingURL=TablePolicies-C1-tbC8x.js.map
|