@opengeni/db 3.2.0-canary.0 → 3.4.0-canary.1
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/dist/canonical-human-identities.js +3 -3
- package/dist/{chunk-SIBYBA2D.js → chunk-6DPJA36C.js} +30 -11
- package/dist/chunk-6DPJA36C.js.map +1 -0
- package/dist/{chunk-KRLPEMLH.js → chunk-C2AFP7SB.js} +184 -6
- package/dist/chunk-C2AFP7SB.js.map +1 -0
- package/dist/{chunk-AZHW7R5V.js → chunk-H7MXYG7R.js} +12 -8
- package/dist/chunk-H7MXYG7R.js.map +1 -0
- package/dist/{chunk-2PRS4J5Q.js → chunk-HY3F5Q4E.js} +3 -3
- package/dist/{chunk-NAB6FKFN.js → chunk-O6FTELKH.js} +3 -3
- package/dist/{chunk-UZNXG44K.js → chunk-PYMY3AJW.js} +324 -8
- package/dist/chunk-PYMY3AJW.js.map +1 -0
- package/dist/{chunk-6HMM2R5T.js → chunk-TRBOWUG3.js} +98 -4
- package/dist/chunk-TRBOWUG3.js.map +1 -0
- package/dist/{chunk-E6CLMKEE.js → chunk-V3TRUXFJ.js} +2 -2
- package/dist/{chunk-2EH4WRHL.js → chunk-ZWRIPCTR.js} +2 -2
- package/dist/editable-artifact-durable-export.js +2 -2
- package/dist/editable-artifacts.js +3 -3
- package/dist/governed-learning-activation.d.ts +16 -0
- package/dist/index.d.ts +108 -7
- package/dist/index.js +3605 -1417
- package/dist/index.js.map +1 -1
- package/dist/insights-model-bundle.d.ts +30 -0
- package/dist/insights-usage-bundle.d.ts +32 -0
- package/dist/insights.d.ts +10 -2
- package/dist/lossless-json.d.ts +1 -1
- package/dist/managed-user-setup.d.ts +82 -0
- package/dist/memory-domain.d.ts +2 -2
- package/dist/migrate.d.ts +3 -1
- package/dist/migrate.js +1 -1
- package/dist/new-session-drafts.d.ts +4 -4
- package/dist/organization-membership-lifecycle.d.ts +60 -1
- package/dist/pr-review.d.ts +44 -3
- package/dist/provision-roles.js +1 -1
- package/dist/runtime-posture.d.ts +12 -7
- package/dist/schema.d.ts +839 -101
- package/dist/schema.js +9 -1
- package/dist/session-background-commands.js +3 -3
- package/dist/session-tenancy.d.ts +12 -1
- package/dist/session-tenancy.js +3 -3
- package/dist/video-generation.js +3 -3
- package/drizzle/0348_named_signup_and_user_setup.sql +838 -0
- package/drizzle/0349_greenfield_session_tenancy_activation.sql +792 -0
- package/drizzle/0350_organization_shared_workspace_administration.sql +889 -0
- package/drizzle/0351_organization_user_setup_delivery.sql +978 -0
- package/drizzle/0352_session_variable_set_attachments.sql +1661 -0
- package/drizzle/0353_automatic_session_title_policy_fence.sql +476 -0
- package/drizzle/0354_automatic_session_title_quarantine_index.sql +9 -0
- package/drizzle/0355_automatic_session_title_quarantine.sql +92 -0
- package/drizzle/0356_set_based_insights_session_visibility.sql +237 -0
- package/drizzle/0357_rig_platform_base_only.sql +44 -0
- package/drizzle/0358_pr_review_managed_github_app.sql +108 -0
- package/drizzle/0359_insights_force_rls_read_capability.sql +936 -0
- package/drizzle/0360_organization_identity_confirmation_prompt.sql +103 -0
- package/drizzle/0361_remember_knowledge_memory_materialization.sql +249 -0
- package/package.json +6 -6
- package/src/database.ts +12 -8
- package/src/governed-learning-activation.ts +74 -2
- package/src/index.ts +879 -92
- package/src/insights-model-bundle.ts +681 -0
- package/src/insights-usage-bundle.ts +240 -0
- package/src/insights.ts +265 -152
- package/src/lossless-json.ts +2 -1
- package/src/managed-user-setup.ts +316 -0
- package/src/memory-domain.ts +3 -3
- package/src/migrate.ts +38 -14
- package/src/new-session-drafts.ts +22 -15
- package/src/organization-membership-lifecycle.ts +253 -1
- package/src/pr-review.ts +449 -6
- package/src/provision-roles.ts +92 -0
- package/src/runtime-posture.ts +247 -4
- package/src/schema.ts +210 -4
- package/src/session-tenancy.ts +120 -2
- package/dist/chunk-6HMM2R5T.js.map +0 -1
- package/dist/chunk-AZHW7R5V.js.map +0 -1
- package/dist/chunk-KRLPEMLH.js.map +0 -1
- package/dist/chunk-SIBYBA2D.js.map +0 -1
- package/dist/chunk-UZNXG44K.js.map +0 -1
- /package/dist/{chunk-2PRS4J5Q.js.map → chunk-HY3F5Q4E.js.map} +0 -0
- /package/dist/{chunk-NAB6FKFN.js.map → chunk-O6FTELKH.js.map} +0 -0
- /package/dist/{chunk-E6CLMKEE.js.map → chunk-V3TRUXFJ.js.map} +0 -0
- /package/dist/{chunk-2EH4WRHL.js.map → chunk-ZWRIPCTR.js.map} +0 -0
|
@@ -0,0 +1,889 @@
|
|
|
1
|
+
-- deployment-mode: rolling
|
|
2
|
+
-- Organization-owned shared-workspace administration. Personal workspaces are
|
|
3
|
+
-- identified only by organization_memberships.personal_workspace_id; names,
|
|
4
|
+
-- slugs, and external ids are never authority.
|
|
5
|
+
|
|
6
|
+
SET LOCAL lock_timeout = '5s';
|
|
7
|
+
SET LOCAL statement_timeout = '10min';
|
|
8
|
+
|
|
9
|
+
CREATE TABLE organization_workspace_operation_receipts (
|
|
10
|
+
account_id uuid NOT NULL REFERENCES managed_accounts(id) ON DELETE CASCADE,
|
|
11
|
+
operation_id uuid NOT NULL,
|
|
12
|
+
action text NOT NULL,
|
|
13
|
+
input_hash text NOT NULL,
|
|
14
|
+
result jsonb NOT NULL,
|
|
15
|
+
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
16
|
+
PRIMARY KEY (account_id, operation_id),
|
|
17
|
+
CONSTRAINT organization_workspace_operation_receipts_action_check CHECK (
|
|
18
|
+
action IN ('create', 'rename', 'grant', 'revoke')
|
|
19
|
+
),
|
|
20
|
+
CONSTRAINT organization_workspace_operation_receipts_hash_check CHECK (
|
|
21
|
+
input_hash ~ '^[0-9a-f]{64}$'
|
|
22
|
+
)
|
|
23
|
+
);
|
|
24
|
+
ALTER TABLE organization_workspace_operation_receipts ENABLE ROW LEVEL SECURITY;
|
|
25
|
+
ALTER TABLE organization_workspace_operation_receipts FORCE ROW LEVEL SECURITY;
|
|
26
|
+
CREATE POLICY organization_tenancy_lifecycle ON organization_workspace_operation_receipts
|
|
27
|
+
USING (current_setting('opengeni.organization_tenancy_lifecycle', true)
|
|
28
|
+
= 'organization_membership_lifecycle')
|
|
29
|
+
WITH CHECK (current_setting('opengeni.organization_tenancy_lifecycle', true)
|
|
30
|
+
= 'organization_membership_lifecycle');
|
|
31
|
+
|
|
32
|
+
CREATE TABLE organization_workspace_lifecycle_events (
|
|
33
|
+
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
34
|
+
account_id uuid NOT NULL REFERENCES managed_accounts(id) ON DELETE CASCADE,
|
|
35
|
+
operation_id uuid NOT NULL,
|
|
36
|
+
actor_membership_id uuid NOT NULL,
|
|
37
|
+
workspace_id uuid NOT NULL,
|
|
38
|
+
target_organization_membership_id uuid,
|
|
39
|
+
target_workspace_membership_id uuid,
|
|
40
|
+
kind text NOT NULL,
|
|
41
|
+
role text,
|
|
42
|
+
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
|
43
|
+
CONSTRAINT organization_workspace_lifecycle_events_operation_uq
|
|
44
|
+
UNIQUE (account_id, operation_id),
|
|
45
|
+
CONSTRAINT organization_workspace_lifecycle_events_actor_fk
|
|
46
|
+
FOREIGN KEY (actor_membership_id, account_id)
|
|
47
|
+
REFERENCES organization_memberships(id, account_id) ON DELETE RESTRICT,
|
|
48
|
+
CONSTRAINT organization_workspace_lifecycle_events_target_fk
|
|
49
|
+
FOREIGN KEY (target_organization_membership_id, account_id)
|
|
50
|
+
REFERENCES organization_memberships(id, account_id) ON DELETE RESTRICT,
|
|
51
|
+
CONSTRAINT organization_workspace_lifecycle_events_kind_check CHECK (
|
|
52
|
+
kind IN ('create', 'rename', 'grant', 'revoke')
|
|
53
|
+
),
|
|
54
|
+
CONSTRAINT organization_workspace_lifecycle_events_role_check CHECK (
|
|
55
|
+
(kind = 'grant' AND role IN ('viewer', 'member', 'admin', 'custom'))
|
|
56
|
+
OR (kind <> 'grant' AND role IS NULL)
|
|
57
|
+
)
|
|
58
|
+
);
|
|
59
|
+
ALTER TABLE organization_workspace_lifecycle_events ENABLE ROW LEVEL SECURITY;
|
|
60
|
+
ALTER TABLE organization_workspace_lifecycle_events FORCE ROW LEVEL SECURITY;
|
|
61
|
+
CREATE POLICY organization_tenancy_lifecycle ON organization_workspace_lifecycle_events
|
|
62
|
+
USING (current_setting('opengeni.organization_tenancy_lifecycle', true)
|
|
63
|
+
= 'organization_membership_lifecycle')
|
|
64
|
+
WITH CHECK (current_setting('opengeni.organization_tenancy_lifecycle', true)
|
|
65
|
+
= 'organization_membership_lifecycle');
|
|
66
|
+
|
|
67
|
+
CREATE TRIGGER organization_workspace_operation_receipts_immutable
|
|
68
|
+
BEFORE UPDATE OR DELETE ON organization_workspace_operation_receipts
|
|
69
|
+
FOR EACH ROW EXECUTE FUNCTION opengeni_private.organization_membership_history_immutable();
|
|
70
|
+
CREATE TRIGGER organization_workspace_lifecycle_events_immutable
|
|
71
|
+
BEFORE UPDATE OR DELETE ON organization_workspace_lifecycle_events
|
|
72
|
+
FOR EACH ROW EXECUTE FUNCTION opengeni_private.organization_membership_history_immutable();
|
|
73
|
+
|
|
74
|
+
CREATE FUNCTION opengeni_private.workspace_member_role_permissions(p_role text)
|
|
75
|
+
RETURNS jsonb
|
|
76
|
+
LANGUAGE sql IMMUTABLE
|
|
77
|
+
SET search_path = pg_catalog
|
|
78
|
+
AS $body$
|
|
79
|
+
SELECT CASE p_role
|
|
80
|
+
WHEN 'viewer' THEN '[
|
|
81
|
+
"workspace:read", "sessions:read", "stream:view", "files:read",
|
|
82
|
+
"documents:search", "variable-sets:list", "connections:read",
|
|
83
|
+
"rigs:use", "artifacts:read"
|
|
84
|
+
]'::jsonb
|
|
85
|
+
WHEN 'member' THEN '[
|
|
86
|
+
"workspace:read", "sessions:create", "sessions:read", "sessions:control",
|
|
87
|
+
"files:upload", "files:read", "documents:manage", "documents:search",
|
|
88
|
+
"scheduled_tasks:manage", "scheduled_tasks:run", "github:use",
|
|
89
|
+
"variable-sets:list", "variable-sets:read", "variable-sets:write",
|
|
90
|
+
"variable-sets:attach", "variable-sets:use", "secrets:list",
|
|
91
|
+
"secrets:write", "goals:manage"
|
|
92
|
+
]'::jsonb
|
|
93
|
+
WHEN 'admin' THEN '[
|
|
94
|
+
"workspace:read", "workspace:admin", "members:manage", "sessions:create",
|
|
95
|
+
"sessions:read", "sessions:control", "stream:view", "stream:control",
|
|
96
|
+
"stream:acknowledge", "terminal:attach", "codemode:call", "files:upload",
|
|
97
|
+
"files:read", "files:write", "documents:manage", "documents:search",
|
|
98
|
+
"scheduled_tasks:manage", "scheduled_tasks:run", "github:manage",
|
|
99
|
+
"github:use", "api_keys:manage", "connections:read", "connections:write",
|
|
100
|
+
"variable-sets:list", "variable-sets:read", "variable-sets:write",
|
|
101
|
+
"variable-sets:manage", "variable-sets:attach", "variable-sets:use",
|
|
102
|
+
"secrets:list", "secrets:write", "mcp_servers:attach", "goals:manage",
|
|
103
|
+
"rigs:use", "rigs:manage", "enrollments:read", "enrollments:manage",
|
|
104
|
+
"artifacts:read", "artifacts:publish"
|
|
105
|
+
]'::jsonb
|
|
106
|
+
ELSE NULL
|
|
107
|
+
END
|
|
108
|
+
$body$;
|
|
109
|
+
|
|
110
|
+
CREATE FUNCTION opengeni_private.workspace_member_role(
|
|
111
|
+
p_role text,
|
|
112
|
+
p_permissions jsonb
|
|
113
|
+
) RETURNS text
|
|
114
|
+
LANGUAGE sql IMMUTABLE
|
|
115
|
+
SET search_path = pg_catalog
|
|
116
|
+
AS $body$
|
|
117
|
+
SELECT CASE
|
|
118
|
+
WHEN p_role IN ('viewer', 'member', 'admin')
|
|
119
|
+
AND p_permissions @> opengeni_private.workspace_member_role_permissions(p_role)
|
|
120
|
+
AND opengeni_private.workspace_member_role_permissions(p_role) @> p_permissions
|
|
121
|
+
THEN p_role
|
|
122
|
+
ELSE 'custom'
|
|
123
|
+
END
|
|
124
|
+
$body$;
|
|
125
|
+
|
|
126
|
+
CREATE FUNCTION opengeni_private.workspace_member_custom_permissions_valid(p_permissions jsonb)
|
|
127
|
+
RETURNS boolean
|
|
128
|
+
LANGUAGE sql IMMUTABLE
|
|
129
|
+
SET search_path = pg_catalog
|
|
130
|
+
AS $body$
|
|
131
|
+
SELECT CASE
|
|
132
|
+
WHEN pg_catalog.jsonb_typeof(p_permissions) IS DISTINCT FROM 'array' THEN false
|
|
133
|
+
ELSE pg_catalog.jsonb_array_length(p_permissions) <= 128
|
|
134
|
+
AND NOT EXISTS (
|
|
135
|
+
SELECT 1
|
|
136
|
+
FROM pg_catalog.jsonb_array_elements_text(p_permissions) permission(value)
|
|
137
|
+
WHERE permission.value NOT IN (
|
|
138
|
+
'members:manage', 'workspace:read', 'workspace:admin',
|
|
139
|
+
'sessions:create', 'sessions:read', 'sessions:control', 'stream:view',
|
|
140
|
+
'stream:control', 'stream:acknowledge', 'files:upload', 'files:read',
|
|
141
|
+
'files:write', 'terminal:attach', 'documents:manage', 'documents:search',
|
|
142
|
+
'scheduled_tasks:manage', 'scheduled_tasks:run', 'github:manage',
|
|
143
|
+
'github:use', 'api_keys:manage', 'connections:read', 'connections:write',
|
|
144
|
+
'environments:manage', 'environments:use', 'variable-sets:list',
|
|
145
|
+
'variable-sets:read', 'variable-sets:write', 'variable-sets:manage',
|
|
146
|
+
'variable-sets:attach', 'variable-sets:use', 'secrets:list',
|
|
147
|
+
'secrets:read', 'secrets:write', 'mcp_servers:attach', 'codemode:call',
|
|
148
|
+
'goals:manage', 'enrollments:read', 'enrollments:manage', 'rigs:use',
|
|
149
|
+
'rigs:manage', 'artifacts:read', 'artifacts:publish'
|
|
150
|
+
)
|
|
151
|
+
) END
|
|
152
|
+
$body$;
|
|
153
|
+
|
|
154
|
+
CREATE FUNCTION get_workspace_kind(p_account_id uuid, p_workspace_id uuid)
|
|
155
|
+
RETURNS text
|
|
156
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
157
|
+
SET search_path FROM CURRENT
|
|
158
|
+
AS $body$
|
|
159
|
+
DECLARE
|
|
160
|
+
previous_lifecycle text := current_setting('opengeni.organization_tenancy_lifecycle', true);
|
|
161
|
+
kind_value text;
|
|
162
|
+
BEGIN
|
|
163
|
+
IF p_account_id IS NULL OR p_workspace_id IS NULL
|
|
164
|
+
OR p_account_id IS DISTINCT FROM opengeni_private.current_account_id()
|
|
165
|
+
OR p_workspace_id IS DISTINCT FROM opengeni_private.current_workspace_id()
|
|
166
|
+
THEN
|
|
167
|
+
RAISE EXCEPTION 'workspace kind authority required' USING ERRCODE = '42501';
|
|
168
|
+
END IF;
|
|
169
|
+
PERFORM pg_catalog.set_config(
|
|
170
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
171
|
+
'organization_membership_lifecycle', true
|
|
172
|
+
);
|
|
173
|
+
IF NOT EXISTS (
|
|
174
|
+
SELECT 1 FROM workspaces workspace
|
|
175
|
+
WHERE workspace.account_id = p_account_id AND workspace.id = p_workspace_id
|
|
176
|
+
) THEN
|
|
177
|
+
RAISE EXCEPTION 'workspace not found' USING ERRCODE = 'P0002';
|
|
178
|
+
END IF;
|
|
179
|
+
kind_value := CASE WHEN EXISTS (
|
|
180
|
+
SELECT 1 FROM organization_memberships membership
|
|
181
|
+
WHERE membership.account_id = p_account_id
|
|
182
|
+
AND membership.personal_workspace_id = p_workspace_id
|
|
183
|
+
) THEN 'personal' ELSE 'shared' END;
|
|
184
|
+
PERFORM pg_catalog.set_config(
|
|
185
|
+
'opengeni.organization_tenancy_lifecycle', coalesce(previous_lifecycle, ''), true
|
|
186
|
+
);
|
|
187
|
+
RETURN kind_value;
|
|
188
|
+
EXCEPTION WHEN OTHERS THEN
|
|
189
|
+
PERFORM pg_catalog.set_config(
|
|
190
|
+
'opengeni.organization_tenancy_lifecycle', coalesce(previous_lifecycle, ''), true
|
|
191
|
+
);
|
|
192
|
+
RAISE;
|
|
193
|
+
END
|
|
194
|
+
$body$;
|
|
195
|
+
|
|
196
|
+
CREATE FUNCTION organization_workspace_command(p_command jsonb)
|
|
197
|
+
RETURNS jsonb
|
|
198
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
199
|
+
SET search_path FROM CURRENT
|
|
200
|
+
AS $body$
|
|
201
|
+
DECLARE
|
|
202
|
+
account_id_value uuid := nullif(p_command ->> 'organizationId', '')::uuid;
|
|
203
|
+
actor_subject text := p_command ->> 'actorSubjectId';
|
|
204
|
+
action_value text := p_command ->> 'action';
|
|
205
|
+
operation_id_value uuid := nullif(p_command ->> 'operationId', '')::uuid;
|
|
206
|
+
workspace_id_value uuid := nullif(p_command ->> 'workspaceId', '')::uuid;
|
|
207
|
+
target_membership_id_value uuid := nullif(
|
|
208
|
+
p_command ->> 'targetOrganizationMembershipId', ''
|
|
209
|
+
)::uuid;
|
|
210
|
+
role_value text := p_command ->> 'role';
|
|
211
|
+
permissions_value jsonb := p_command -> 'permissions';
|
|
212
|
+
resolved_permissions jsonb;
|
|
213
|
+
name_value text := nullif(btrim(p_command ->> 'name'), '');
|
|
214
|
+
expected_updated_at_value timestamptz := nullif(
|
|
215
|
+
p_command ->> 'expectedUpdatedAt', ''
|
|
216
|
+
)::timestamptz;
|
|
217
|
+
input_hash_value text;
|
|
218
|
+
actor organization_memberships%ROWTYPE;
|
|
219
|
+
target organization_memberships%ROWTYPE;
|
|
220
|
+
workspace workspaces%ROWTYPE;
|
|
221
|
+
access workspace_memberships%ROWTYPE;
|
|
222
|
+
prior organization_workspace_operation_receipts%ROWTYPE;
|
|
223
|
+
result jsonb;
|
|
224
|
+
BEGIN
|
|
225
|
+
IF p_command IS NULL OR account_id_value IS NULL OR actor_subject IS NULL
|
|
226
|
+
OR action_value NOT IN ('create', 'rename', 'grant')
|
|
227
|
+
OR operation_id_value IS NULL
|
|
228
|
+
OR actor_subject IS DISTINCT FROM opengeni_private.current_subject_id()
|
|
229
|
+
OR account_id_value IS DISTINCT FROM opengeni_private.current_account_id()
|
|
230
|
+
THEN
|
|
231
|
+
RAISE EXCEPTION 'organization workspace command is invalid' USING ERRCODE = '22023';
|
|
232
|
+
END IF;
|
|
233
|
+
input_hash_value := pg_catalog.encode(
|
|
234
|
+
pg_catalog.sha256(pg_catalog.convert_to(p_command::text, 'UTF8')), 'hex'
|
|
235
|
+
);
|
|
236
|
+
PERFORM pg_catalog.pg_advisory_xact_lock(
|
|
237
|
+
pg_catalog.hashtextextended('organization-membership:' || account_id_value::text, 0)
|
|
238
|
+
);
|
|
239
|
+
PERFORM pg_catalog.set_config(
|
|
240
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
241
|
+
'organization_membership_lifecycle', true
|
|
242
|
+
);
|
|
243
|
+
PERFORM 1 FROM managed_accounts account
|
|
244
|
+
WHERE account.id = account_id_value FOR KEY SHARE;
|
|
245
|
+
IF NOT FOUND THEN
|
|
246
|
+
RAISE EXCEPTION 'organization not found' USING ERRCODE = 'P0002';
|
|
247
|
+
END IF;
|
|
248
|
+
SELECT * INTO actor FROM organization_memberships membership
|
|
249
|
+
WHERE membership.account_id = account_id_value
|
|
250
|
+
AND membership.subject_id = actor_subject
|
|
251
|
+
FOR SHARE;
|
|
252
|
+
IF NOT FOUND OR actor.status <> 'active' OR actor.role NOT IN ('owner', 'admin') THEN
|
|
253
|
+
RAISE EXCEPTION 'organization administration required' USING ERRCODE = '42501';
|
|
254
|
+
END IF;
|
|
255
|
+
SELECT * INTO prior FROM organization_workspace_operation_receipts receipt
|
|
256
|
+
WHERE receipt.account_id = account_id_value
|
|
257
|
+
AND receipt.operation_id = operation_id_value;
|
|
258
|
+
IF FOUND THEN
|
|
259
|
+
IF prior.action IS DISTINCT FROM action_value
|
|
260
|
+
OR prior.input_hash IS DISTINCT FROM input_hash_value
|
|
261
|
+
THEN
|
|
262
|
+
RAISE EXCEPTION 'organization workspace operation key was reused'
|
|
263
|
+
USING ERRCODE = '23505';
|
|
264
|
+
END IF;
|
|
265
|
+
RETURN prior.result || pg_catalog.jsonb_build_object('replay', true);
|
|
266
|
+
END IF;
|
|
267
|
+
|
|
268
|
+
IF action_value = 'create' THEN
|
|
269
|
+
IF name_value IS NULL
|
|
270
|
+
OR char_length(name_value) > 120
|
|
271
|
+
OR octet_length(convert_to(name_value, 'UTF8')) > 480
|
|
272
|
+
OR workspace_id_value IS NOT NULL
|
|
273
|
+
OR target_membership_id_value IS NOT NULL
|
|
274
|
+
OR role_value IS NOT NULL
|
|
275
|
+
OR permissions_value IS NOT NULL
|
|
276
|
+
OR expected_updated_at_value IS NOT NULL
|
|
277
|
+
THEN
|
|
278
|
+
RAISE EXCEPTION 'shared workspace create input is invalid' USING ERRCODE = '22023';
|
|
279
|
+
END IF;
|
|
280
|
+
workspace_id_value := pg_catalog.gen_random_uuid();
|
|
281
|
+
PERFORM pg_catalog.set_config('opengeni.workspace_id', workspace_id_value::text, true);
|
|
282
|
+
INSERT INTO workspaces (id, account_id, name)
|
|
283
|
+
VALUES (workspace_id_value, account_id_value, name_value)
|
|
284
|
+
RETURNING * INTO workspace;
|
|
285
|
+
INSERT INTO workspace_inference_controls (workspace_id, account_id)
|
|
286
|
+
VALUES (workspace_id_value, account_id_value);
|
|
287
|
+
ELSE
|
|
288
|
+
IF workspace_id_value IS NULL THEN
|
|
289
|
+
RAISE EXCEPTION 'shared workspace id is required' USING ERRCODE = '22023';
|
|
290
|
+
END IF;
|
|
291
|
+
PERFORM pg_catalog.set_config('opengeni.workspace_id', workspace_id_value::text, true);
|
|
292
|
+
PERFORM 1 FROM workspace_inference_controls control
|
|
293
|
+
WHERE control.account_id = account_id_value
|
|
294
|
+
AND control.workspace_id = workspace_id_value
|
|
295
|
+
FOR SHARE;
|
|
296
|
+
SELECT * INTO workspace FROM workspaces candidate
|
|
297
|
+
WHERE candidate.account_id = account_id_value
|
|
298
|
+
AND candidate.id = workspace_id_value
|
|
299
|
+
FOR UPDATE;
|
|
300
|
+
IF NOT FOUND THEN
|
|
301
|
+
RAISE EXCEPTION 'workspace not found' USING ERRCODE = 'P0002';
|
|
302
|
+
END IF;
|
|
303
|
+
IF EXISTS (
|
|
304
|
+
SELECT 1 FROM organization_memberships personal_owner
|
|
305
|
+
WHERE personal_owner.account_id = account_id_value
|
|
306
|
+
AND personal_owner.personal_workspace_id = workspace_id_value
|
|
307
|
+
) THEN
|
|
308
|
+
RAISE EXCEPTION 'personal workspaces are not administrable'
|
|
309
|
+
USING ERRCODE = '42501';
|
|
310
|
+
END IF;
|
|
311
|
+
IF action_value = 'rename' THEN
|
|
312
|
+
IF name_value IS NULL
|
|
313
|
+
OR char_length(name_value) > 120
|
|
314
|
+
OR octet_length(convert_to(name_value, 'UTF8')) > 480
|
|
315
|
+
OR expected_updated_at_value IS NULL
|
|
316
|
+
OR target_membership_id_value IS NOT NULL
|
|
317
|
+
OR role_value IS NOT NULL
|
|
318
|
+
OR permissions_value IS NOT NULL
|
|
319
|
+
THEN
|
|
320
|
+
RAISE EXCEPTION 'shared workspace rename input is invalid' USING ERRCODE = '22023';
|
|
321
|
+
END IF;
|
|
322
|
+
IF workspace.updated_at IS DISTINCT FROM expected_updated_at_value THEN
|
|
323
|
+
RAISE EXCEPTION 'shared workspace changed before rename' USING ERRCODE = '40001';
|
|
324
|
+
END IF;
|
|
325
|
+
UPDATE workspaces SET name = name_value, updated_at = clock_timestamp()
|
|
326
|
+
WHERE id = workspace_id_value RETURNING * INTO workspace;
|
|
327
|
+
ELSE
|
|
328
|
+
IF target_membership_id_value IS NULL
|
|
329
|
+
OR role_value NOT IN ('viewer', 'member', 'admin', 'custom')
|
|
330
|
+
OR name_value IS NOT NULL
|
|
331
|
+
OR (role_value = 'custom'
|
|
332
|
+
AND opengeni_private.workspace_member_custom_permissions_valid(permissions_value)
|
|
333
|
+
IS DISTINCT FROM true)
|
|
334
|
+
OR (role_value <> 'custom' AND permissions_value IS NOT NULL)
|
|
335
|
+
THEN
|
|
336
|
+
RAISE EXCEPTION 'shared workspace grant input is invalid' USING ERRCODE = '22023';
|
|
337
|
+
END IF;
|
|
338
|
+
resolved_permissions := CASE
|
|
339
|
+
WHEN role_value = 'custom' THEN permissions_value
|
|
340
|
+
ELSE opengeni_private.workspace_member_role_permissions(role_value)
|
|
341
|
+
END;
|
|
342
|
+
SELECT * INTO target FROM organization_memberships membership
|
|
343
|
+
WHERE membership.account_id = account_id_value
|
|
344
|
+
AND membership.id = target_membership_id_value
|
|
345
|
+
FOR SHARE;
|
|
346
|
+
IF NOT FOUND OR target.status <> 'active' OR target.subject_id NOT LIKE 'user:%' THEN
|
|
347
|
+
RAISE EXCEPTION 'active organization member not found' USING ERRCODE = 'P0002';
|
|
348
|
+
END IF;
|
|
349
|
+
SELECT * INTO access FROM workspace_memberships membership
|
|
350
|
+
WHERE membership.account_id = account_id_value
|
|
351
|
+
AND membership.workspace_id = workspace_id_value
|
|
352
|
+
AND membership.subject_id = target.subject_id
|
|
353
|
+
FOR UPDATE;
|
|
354
|
+
IF FOUND THEN
|
|
355
|
+
IF expected_updated_at_value IS NULL
|
|
356
|
+
OR access.updated_at IS DISTINCT FROM expected_updated_at_value
|
|
357
|
+
THEN
|
|
358
|
+
RAISE EXCEPTION 'workspace access changed before grant' USING ERRCODE = '40001';
|
|
359
|
+
END IF;
|
|
360
|
+
UPDATE workspace_memberships SET
|
|
361
|
+
role = role_value,
|
|
362
|
+
permissions = resolved_permissions,
|
|
363
|
+
subject_label = NULL,
|
|
364
|
+
updated_at = clock_timestamp()
|
|
365
|
+
WHERE id = access.id RETURNING * INTO access;
|
|
366
|
+
ELSE
|
|
367
|
+
IF expected_updated_at_value IS NOT NULL THEN
|
|
368
|
+
RAISE EXCEPTION 'workspace access changed before grant' USING ERRCODE = '40001';
|
|
369
|
+
END IF;
|
|
370
|
+
INSERT INTO workspace_memberships (
|
|
371
|
+
account_id, workspace_id, subject_id, subject_label, role, permissions
|
|
372
|
+
) VALUES (
|
|
373
|
+
account_id_value, workspace_id_value, target.subject_id, NULL,
|
|
374
|
+
role_value, resolved_permissions
|
|
375
|
+
) RETURNING * INTO access;
|
|
376
|
+
END IF;
|
|
377
|
+
END IF;
|
|
378
|
+
END IF;
|
|
379
|
+
|
|
380
|
+
result := pg_catalog.jsonb_strip_nulls(pg_catalog.jsonb_build_object(
|
|
381
|
+
'workspaceId', workspace_id_value,
|
|
382
|
+
'organizationMembershipId', target_membership_id_value,
|
|
383
|
+
'workspaceMembershipId', access.id,
|
|
384
|
+
'updatedAt', coalesce(access.updated_at, workspace.updated_at),
|
|
385
|
+
'replay', false
|
|
386
|
+
));
|
|
387
|
+
INSERT INTO organization_workspace_operation_receipts (
|
|
388
|
+
account_id, operation_id, action, input_hash, result
|
|
389
|
+
) VALUES (
|
|
390
|
+
account_id_value, operation_id_value, action_value, input_hash_value, result
|
|
391
|
+
);
|
|
392
|
+
INSERT INTO organization_workspace_lifecycle_events (
|
|
393
|
+
account_id, operation_id, actor_membership_id, workspace_id,
|
|
394
|
+
target_organization_membership_id, target_workspace_membership_id, kind, role
|
|
395
|
+
) VALUES (
|
|
396
|
+
account_id_value, operation_id_value, actor.id, workspace_id_value,
|
|
397
|
+
target_membership_id_value, access.id, action_value,
|
|
398
|
+
CASE WHEN action_value = 'grant' THEN role_value ELSE NULL END
|
|
399
|
+
);
|
|
400
|
+
RETURN result;
|
|
401
|
+
END
|
|
402
|
+
$body$;
|
|
403
|
+
|
|
404
|
+
CREATE FUNCTION resolve_organization_workspace_removal_subject(
|
|
405
|
+
p_account_id uuid,
|
|
406
|
+
p_actor_subject_id text,
|
|
407
|
+
p_target_organization_membership_id uuid
|
|
408
|
+
) RETURNS text
|
|
409
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
410
|
+
SET search_path FROM CURRENT
|
|
411
|
+
AS $body$
|
|
412
|
+
DECLARE
|
|
413
|
+
actor organization_memberships%ROWTYPE;
|
|
414
|
+
target organization_memberships%ROWTYPE;
|
|
415
|
+
BEGIN
|
|
416
|
+
IF p_account_id IS NULL OR p_target_organization_membership_id IS NULL
|
|
417
|
+
OR p_actor_subject_id IS DISTINCT FROM opengeni_private.current_subject_id()
|
|
418
|
+
OR p_account_id IS DISTINCT FROM opengeni_private.current_account_id()
|
|
419
|
+
THEN
|
|
420
|
+
RAISE EXCEPTION 'organization workspace removal authority required'
|
|
421
|
+
USING ERRCODE = '42501';
|
|
422
|
+
END IF;
|
|
423
|
+
PERFORM pg_catalog.pg_advisory_xact_lock(
|
|
424
|
+
pg_catalog.hashtextextended('organization-membership:' || p_account_id::text, 0)
|
|
425
|
+
);
|
|
426
|
+
PERFORM pg_catalog.set_config(
|
|
427
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
428
|
+
'organization_membership_lifecycle', true
|
|
429
|
+
);
|
|
430
|
+
SELECT * INTO actor FROM organization_memberships membership
|
|
431
|
+
WHERE membership.account_id = p_account_id
|
|
432
|
+
AND membership.subject_id = p_actor_subject_id;
|
|
433
|
+
IF NOT FOUND OR actor.status <> 'active' OR actor.role NOT IN ('owner', 'admin') THEN
|
|
434
|
+
RAISE EXCEPTION 'organization administration required' USING ERRCODE = '42501';
|
|
435
|
+
END IF;
|
|
436
|
+
SELECT * INTO target FROM organization_memberships membership
|
|
437
|
+
WHERE membership.account_id = p_account_id
|
|
438
|
+
AND membership.id = p_target_organization_membership_id;
|
|
439
|
+
IF NOT FOUND OR target.subject_id NOT LIKE 'user:%' THEN
|
|
440
|
+
RAISE EXCEPTION 'organization member not found' USING ERRCODE = 'P0002';
|
|
441
|
+
END IF;
|
|
442
|
+
RETURN target.subject_id;
|
|
443
|
+
END
|
|
444
|
+
$body$;
|
|
445
|
+
|
|
446
|
+
CREATE FUNCTION prepare_organization_workspace_member_removal(p_command jsonb)
|
|
447
|
+
RETURNS jsonb
|
|
448
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
449
|
+
SET search_path FROM CURRENT
|
|
450
|
+
AS $body$
|
|
451
|
+
DECLARE
|
|
452
|
+
account_id_value uuid := nullif(p_command ->> 'organizationId', '')::uuid;
|
|
453
|
+
workspace_id_value uuid := nullif(p_command ->> 'workspaceId', '')::uuid;
|
|
454
|
+
actor_subject text := p_command ->> 'actorSubjectId';
|
|
455
|
+
target_membership_id_value uuid := nullif(
|
|
456
|
+
p_command ->> 'targetOrganizationMembershipId', ''
|
|
457
|
+
)::uuid;
|
|
458
|
+
target_subject text := p_command ->> 'targetSubjectId';
|
|
459
|
+
operation_id_value uuid := nullif(p_command ->> 'operationId', '')::uuid;
|
|
460
|
+
expected_updated_at_value timestamptz := nullif(
|
|
461
|
+
p_command ->> 'expectedUpdatedAt', ''
|
|
462
|
+
)::timestamptz;
|
|
463
|
+
input_hash_value text;
|
|
464
|
+
actor organization_memberships%ROWTYPE;
|
|
465
|
+
target organization_memberships%ROWTYPE;
|
|
466
|
+
access workspace_memberships%ROWTYPE;
|
|
467
|
+
prior organization_workspace_operation_receipts%ROWTYPE;
|
|
468
|
+
BEGIN
|
|
469
|
+
IF p_command IS NULL OR (p_command ->> 'action') IS DISTINCT FROM 'revoke'
|
|
470
|
+
OR account_id_value IS NULL OR workspace_id_value IS NULL
|
|
471
|
+
OR actor_subject IS NULL OR target_membership_id_value IS NULL
|
|
472
|
+
OR target_subject IS NULL OR operation_id_value IS NULL
|
|
473
|
+
OR expected_updated_at_value IS NULL
|
|
474
|
+
OR actor_subject IS DISTINCT FROM opengeni_private.current_subject_id()
|
|
475
|
+
OR account_id_value IS DISTINCT FROM opengeni_private.current_account_id()
|
|
476
|
+
THEN
|
|
477
|
+
RAISE EXCEPTION 'organization workspace removal request is invalid'
|
|
478
|
+
USING ERRCODE = '22023';
|
|
479
|
+
END IF;
|
|
480
|
+
input_hash_value := pg_catalog.encode(
|
|
481
|
+
pg_catalog.sha256(pg_catalog.convert_to(p_command::text, 'UTF8')), 'hex'
|
|
482
|
+
);
|
|
483
|
+
PERFORM pg_catalog.pg_advisory_xact_lock(
|
|
484
|
+
pg_catalog.hashtextextended('organization-membership:' || account_id_value::text, 0)
|
|
485
|
+
);
|
|
486
|
+
PERFORM pg_catalog.set_config(
|
|
487
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
488
|
+
'organization_membership_lifecycle', true
|
|
489
|
+
);
|
|
490
|
+
PERFORM 1 FROM managed_accounts account
|
|
491
|
+
WHERE account.id = account_id_value FOR KEY SHARE;
|
|
492
|
+
PERFORM pg_catalog.set_config('opengeni.workspace_id', workspace_id_value::text, true);
|
|
493
|
+
PERFORM 1 FROM workspace_inference_controls control
|
|
494
|
+
WHERE control.account_id = account_id_value AND control.workspace_id = workspace_id_value
|
|
495
|
+
FOR SHARE;
|
|
496
|
+
PERFORM 1 FROM workspaces workspace
|
|
497
|
+
WHERE workspace.account_id = account_id_value AND workspace.id = workspace_id_value
|
|
498
|
+
FOR KEY SHARE;
|
|
499
|
+
IF NOT FOUND THEN
|
|
500
|
+
RAISE EXCEPTION 'workspace not found' USING ERRCODE = 'P0002';
|
|
501
|
+
END IF;
|
|
502
|
+
IF EXISTS (
|
|
503
|
+
SELECT 1 FROM organization_memberships personal_owner
|
|
504
|
+
WHERE personal_owner.account_id = account_id_value
|
|
505
|
+
AND personal_owner.personal_workspace_id = workspace_id_value
|
|
506
|
+
) THEN
|
|
507
|
+
RAISE EXCEPTION 'personal workspaces are not administrable' USING ERRCODE = '42501';
|
|
508
|
+
END IF;
|
|
509
|
+
SELECT * INTO actor FROM organization_memberships membership
|
|
510
|
+
WHERE membership.account_id = account_id_value
|
|
511
|
+
AND membership.subject_id = actor_subject
|
|
512
|
+
FOR SHARE;
|
|
513
|
+
IF NOT FOUND OR actor.status <> 'active' OR actor.role NOT IN ('owner', 'admin') THEN
|
|
514
|
+
RAISE EXCEPTION 'organization administration required' USING ERRCODE = '42501';
|
|
515
|
+
END IF;
|
|
516
|
+
SELECT * INTO prior FROM organization_workspace_operation_receipts receipt
|
|
517
|
+
WHERE receipt.account_id = account_id_value
|
|
518
|
+
AND receipt.operation_id = operation_id_value;
|
|
519
|
+
IF FOUND THEN
|
|
520
|
+
IF prior.action <> 'revoke' OR prior.input_hash IS DISTINCT FROM input_hash_value THEN
|
|
521
|
+
RAISE EXCEPTION 'organization workspace operation key was reused'
|
|
522
|
+
USING ERRCODE = '23505';
|
|
523
|
+
END IF;
|
|
524
|
+
RETURN prior.result || pg_catalog.jsonb_build_object('replay', true);
|
|
525
|
+
END IF;
|
|
526
|
+
SELECT * INTO target FROM organization_memberships membership
|
|
527
|
+
WHERE membership.account_id = account_id_value
|
|
528
|
+
AND membership.id = target_membership_id_value
|
|
529
|
+
AND membership.subject_id = target_subject
|
|
530
|
+
FOR SHARE;
|
|
531
|
+
IF NOT FOUND THEN
|
|
532
|
+
RAISE EXCEPTION 'organization member not found' USING ERRCODE = 'P0002';
|
|
533
|
+
END IF;
|
|
534
|
+
SELECT * INTO access FROM workspace_memberships membership
|
|
535
|
+
WHERE membership.account_id = account_id_value
|
|
536
|
+
AND membership.workspace_id = workspace_id_value
|
|
537
|
+
AND membership.subject_id = target_subject
|
|
538
|
+
FOR UPDATE;
|
|
539
|
+
IF NOT FOUND THEN
|
|
540
|
+
RAISE EXCEPTION 'workspace access not found' USING ERRCODE = 'P0002';
|
|
541
|
+
END IF;
|
|
542
|
+
IF access.updated_at IS DISTINCT FROM expected_updated_at_value THEN
|
|
543
|
+
RAISE EXCEPTION 'workspace access changed before removal' USING ERRCODE = '40001';
|
|
544
|
+
END IF;
|
|
545
|
+
RETURN pg_catalog.jsonb_build_object(
|
|
546
|
+
'removed', false,
|
|
547
|
+
'replay', false,
|
|
548
|
+
'workspaceMembershipId', access.id,
|
|
549
|
+
'actorMembershipId', actor.id
|
|
550
|
+
);
|
|
551
|
+
END
|
|
552
|
+
$body$;
|
|
553
|
+
|
|
554
|
+
CREATE FUNCTION record_organization_workspace_member_removal(
|
|
555
|
+
p_command jsonb,
|
|
556
|
+
p_workspace_membership_id uuid,
|
|
557
|
+
p_actor_membership_id uuid
|
|
558
|
+
) RETURNS jsonb
|
|
559
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
560
|
+
SET search_path FROM CURRENT
|
|
561
|
+
AS $body$
|
|
562
|
+
DECLARE
|
|
563
|
+
account_id_value uuid := nullif(p_command ->> 'organizationId', '')::uuid;
|
|
564
|
+
workspace_id_value uuid := nullif(p_command ->> 'workspaceId', '')::uuid;
|
|
565
|
+
target_membership_id_value uuid := nullif(
|
|
566
|
+
p_command ->> 'targetOrganizationMembershipId', ''
|
|
567
|
+
)::uuid;
|
|
568
|
+
operation_id_value uuid := nullif(p_command ->> 'operationId', '')::uuid;
|
|
569
|
+
input_hash_value text;
|
|
570
|
+
result jsonb;
|
|
571
|
+
BEGIN
|
|
572
|
+
IF p_command IS NULL OR (p_command ->> 'action') IS DISTINCT FROM 'revoke'
|
|
573
|
+
OR account_id_value IS NULL OR workspace_id_value IS NULL
|
|
574
|
+
OR target_membership_id_value IS NULL OR operation_id_value IS NULL
|
|
575
|
+
OR p_workspace_membership_id IS NULL OR p_actor_membership_id IS NULL
|
|
576
|
+
OR (p_command ->> 'actorSubjectId') IS DISTINCT FROM opengeni_private.current_subject_id()
|
|
577
|
+
OR account_id_value IS DISTINCT FROM opengeni_private.current_account_id()
|
|
578
|
+
OR NOT EXISTS (
|
|
579
|
+
SELECT 1 FROM organization_memberships actor
|
|
580
|
+
WHERE actor.account_id = account_id_value
|
|
581
|
+
AND actor.id = p_actor_membership_id
|
|
582
|
+
AND actor.subject_id = p_command ->> 'actorSubjectId'
|
|
583
|
+
AND actor.status = 'active'
|
|
584
|
+
AND actor.role IN ('owner', 'admin')
|
|
585
|
+
)
|
|
586
|
+
OR NOT EXISTS (
|
|
587
|
+
SELECT 1 FROM organization_memberships target
|
|
588
|
+
WHERE target.account_id = account_id_value
|
|
589
|
+
AND target.id = target_membership_id_value
|
|
590
|
+
AND target.subject_id = p_command ->> 'targetSubjectId'
|
|
591
|
+
)
|
|
592
|
+
OR EXISTS (
|
|
593
|
+
SELECT 1 FROM workspace_memberships membership
|
|
594
|
+
WHERE membership.id = p_workspace_membership_id
|
|
595
|
+
OR (
|
|
596
|
+
membership.account_id = account_id_value
|
|
597
|
+
AND membership.workspace_id = workspace_id_value
|
|
598
|
+
AND membership.subject_id = p_command ->> 'targetSubjectId'
|
|
599
|
+
)
|
|
600
|
+
)
|
|
601
|
+
THEN
|
|
602
|
+
RAISE EXCEPTION 'organization workspace removal receipt is invalid'
|
|
603
|
+
USING ERRCODE = '55000';
|
|
604
|
+
END IF;
|
|
605
|
+
PERFORM pg_catalog.set_config(
|
|
606
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
607
|
+
'organization_membership_lifecycle', true
|
|
608
|
+
);
|
|
609
|
+
input_hash_value := pg_catalog.encode(
|
|
610
|
+
pg_catalog.sha256(pg_catalog.convert_to(p_command::text, 'UTF8')), 'hex'
|
|
611
|
+
);
|
|
612
|
+
result := pg_catalog.jsonb_build_object('removed', true, 'replay', false);
|
|
613
|
+
INSERT INTO organization_workspace_operation_receipts (
|
|
614
|
+
account_id, operation_id, action, input_hash, result
|
|
615
|
+
) VALUES (account_id_value, operation_id_value, 'revoke', input_hash_value, result);
|
|
616
|
+
INSERT INTO organization_workspace_lifecycle_events (
|
|
617
|
+
account_id, operation_id, actor_membership_id, workspace_id,
|
|
618
|
+
target_organization_membership_id, target_workspace_membership_id, kind
|
|
619
|
+
) VALUES (
|
|
620
|
+
account_id_value, operation_id_value, p_actor_membership_id, workspace_id_value,
|
|
621
|
+
target_membership_id_value, p_workspace_membership_id, 'revoke'
|
|
622
|
+
);
|
|
623
|
+
RETURN result;
|
|
624
|
+
END
|
|
625
|
+
$body$;
|
|
626
|
+
|
|
627
|
+
CREATE FUNCTION list_organization_administration_members(
|
|
628
|
+
p_account_id uuid,
|
|
629
|
+
p_actor_subject_id text
|
|
630
|
+
) RETURNS jsonb
|
|
631
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
632
|
+
SET search_path FROM CURRENT
|
|
633
|
+
AS $body$
|
|
634
|
+
DECLARE
|
|
635
|
+
actor organization_memberships%ROWTYPE;
|
|
636
|
+
result jsonb;
|
|
637
|
+
BEGIN
|
|
638
|
+
IF p_account_id IS NULL
|
|
639
|
+
OR p_actor_subject_id IS DISTINCT FROM opengeni_private.current_subject_id()
|
|
640
|
+
OR p_account_id IS DISTINCT FROM opengeni_private.current_account_id()
|
|
641
|
+
THEN
|
|
642
|
+
RAISE EXCEPTION 'organization member authority required' USING ERRCODE = '42501';
|
|
643
|
+
END IF;
|
|
644
|
+
PERFORM pg_catalog.set_config(
|
|
645
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
646
|
+
'organization_membership_lifecycle', true
|
|
647
|
+
);
|
|
648
|
+
SELECT * INTO actor FROM organization_memberships membership
|
|
649
|
+
WHERE membership.account_id = p_account_id
|
|
650
|
+
AND membership.subject_id = p_actor_subject_id;
|
|
651
|
+
IF NOT FOUND OR actor.status <> 'active' OR actor.role NOT IN ('owner', 'admin') THEN
|
|
652
|
+
RAISE EXCEPTION 'organization administration required' USING ERRCODE = '42501';
|
|
653
|
+
END IF;
|
|
654
|
+
IF (SELECT count(*) FROM organization_memberships WHERE account_id = p_account_id) > 1000 THEN
|
|
655
|
+
RAISE EXCEPTION 'organization member inventory exceeds the bounded projection'
|
|
656
|
+
USING ERRCODE = '54000';
|
|
657
|
+
END IF;
|
|
658
|
+
SELECT coalesce(pg_catalog.jsonb_agg(
|
|
659
|
+
pg_catalog.jsonb_build_object(
|
|
660
|
+
'id', membership.id,
|
|
661
|
+
'organizationId', membership.account_id,
|
|
662
|
+
'subjectId', membership.subject_id,
|
|
663
|
+
'name', auth_user.name,
|
|
664
|
+
'email', auth_user.email,
|
|
665
|
+
'role', membership.role,
|
|
666
|
+
'status', membership.status,
|
|
667
|
+
'authorizationRevision', membership.authorization_revision,
|
|
668
|
+
'sharedWorkspaceAccess', coalesce((
|
|
669
|
+
SELECT pg_catalog.jsonb_agg(pg_catalog.jsonb_build_object(
|
|
670
|
+
'workspaceId', workspace.id,
|
|
671
|
+
'workspaceName', workspace.name,
|
|
672
|
+
'membershipId', access.id,
|
|
673
|
+
'role', opengeni_private.workspace_member_role(access.role, access.permissions),
|
|
674
|
+
'updatedAt', access.updated_at
|
|
675
|
+
) ORDER BY lower(workspace.name), workspace.id)
|
|
676
|
+
FROM workspace_memberships access
|
|
677
|
+
JOIN workspaces workspace ON workspace.id = access.workspace_id
|
|
678
|
+
WHERE access.account_id = p_account_id
|
|
679
|
+
AND access.subject_id = membership.subject_id
|
|
680
|
+
AND NOT EXISTS (
|
|
681
|
+
SELECT 1 FROM organization_memberships personal_owner
|
|
682
|
+
WHERE personal_owner.account_id = p_account_id
|
|
683
|
+
AND personal_owner.personal_workspace_id = workspace.id
|
|
684
|
+
)
|
|
685
|
+
), '[]'::jsonb),
|
|
686
|
+
'revokedAt', membership.revoked_at,
|
|
687
|
+
'createdAt', membership.created_at,
|
|
688
|
+
'updatedAt', membership.updated_at
|
|
689
|
+
) ORDER BY lower(coalesce(auth_user.name, auth_user.email, membership.subject_id)), membership.id
|
|
690
|
+
), '[]'::jsonb) INTO result
|
|
691
|
+
FROM organization_memberships membership
|
|
692
|
+
LEFT JOIN auth_users auth_user
|
|
693
|
+
ON membership.subject_id = 'user:' || auth_user.id
|
|
694
|
+
WHERE membership.account_id = p_account_id;
|
|
695
|
+
RETURN result;
|
|
696
|
+
END
|
|
697
|
+
$body$;
|
|
698
|
+
|
|
699
|
+
CREATE OR REPLACE FUNCTION get_organization_administration_overview(
|
|
700
|
+
p_account_id uuid,
|
|
701
|
+
p_actor_subject_id text
|
|
702
|
+
) RETURNS jsonb
|
|
703
|
+
LANGUAGE plpgsql SECURITY DEFINER
|
|
704
|
+
SET search_path FROM CURRENT
|
|
705
|
+
AS $body$
|
|
706
|
+
DECLARE
|
|
707
|
+
actor organization_memberships%ROWTYPE;
|
|
708
|
+
result jsonb;
|
|
709
|
+
workspace_count integer;
|
|
710
|
+
oversized_workspace boolean;
|
|
711
|
+
BEGIN
|
|
712
|
+
IF p_account_id IS NULL
|
|
713
|
+
OR p_actor_subject_id IS DISTINCT FROM opengeni_private.current_subject_id()
|
|
714
|
+
OR p_account_id IS DISTINCT FROM opengeni_private.current_account_id()
|
|
715
|
+
THEN
|
|
716
|
+
RAISE EXCEPTION 'organization administration authority required' USING ERRCODE = '42501';
|
|
717
|
+
END IF;
|
|
718
|
+
PERFORM pg_catalog.set_config(
|
|
719
|
+
'opengeni.organization_tenancy_lifecycle',
|
|
720
|
+
'organization_membership_lifecycle', true
|
|
721
|
+
);
|
|
722
|
+
PERFORM 1 FROM managed_accounts account WHERE account.id = p_account_id FOR KEY SHARE;
|
|
723
|
+
IF NOT FOUND THEN
|
|
724
|
+
RAISE EXCEPTION 'organization not found' USING ERRCODE = 'P0002';
|
|
725
|
+
END IF;
|
|
726
|
+
SELECT * INTO actor FROM organization_memberships membership
|
|
727
|
+
WHERE membership.account_id = p_account_id
|
|
728
|
+
AND membership.subject_id = p_actor_subject_id FOR SHARE;
|
|
729
|
+
IF NOT FOUND OR actor.status <> 'active' OR actor.role NOT IN ('owner', 'admin') THEN
|
|
730
|
+
RAISE EXCEPTION 'organization administration required' USING ERRCODE = '42501';
|
|
731
|
+
END IF;
|
|
732
|
+
WITH shared_workspaces AS MATERIALIZED (
|
|
733
|
+
SELECT workspace.*,
|
|
734
|
+
(SELECT count(*)::integer FROM workspace_memberships access
|
|
735
|
+
WHERE access.workspace_id = workspace.id) AS member_count
|
|
736
|
+
FROM workspaces workspace
|
|
737
|
+
WHERE workspace.account_id = p_account_id
|
|
738
|
+
AND NOT EXISTS (
|
|
739
|
+
SELECT 1 FROM organization_memberships membership
|
|
740
|
+
WHERE membership.account_id = p_account_id
|
|
741
|
+
AND membership.personal_workspace_id = workspace.id
|
|
742
|
+
)
|
|
743
|
+
), bounds AS (
|
|
744
|
+
SELECT count(*)::integer AS workspace_count,
|
|
745
|
+
coalesce(bool_or(shared.member_count > 1000), false) AS oversized_workspace
|
|
746
|
+
FROM shared_workspaces shared
|
|
747
|
+
)
|
|
748
|
+
SELECT bounds.workspace_count, bounds.oversized_workspace,
|
|
749
|
+
CASE WHEN bounds.workspace_count <= 500 AND NOT bounds.oversized_workspace THEN
|
|
750
|
+
pg_catalog.jsonb_build_object(
|
|
751
|
+
'organization', pg_catalog.jsonb_build_object(
|
|
752
|
+
'id', account.id, 'name', account.name,
|
|
753
|
+
'createdAt', account.created_at, 'updatedAt', account.updated_at
|
|
754
|
+
),
|
|
755
|
+
'roles', pg_catalog.jsonb_build_array(
|
|
756
|
+
pg_catalog.jsonb_build_object(
|
|
757
|
+
'role', 'viewer', 'label', 'Viewer',
|
|
758
|
+
'description', 'Can view shared workspace sessions, files, and approved knowledge.',
|
|
759
|
+
'permissions', opengeni_private.workspace_member_role_permissions('viewer')
|
|
760
|
+
),
|
|
761
|
+
pg_catalog.jsonb_build_object(
|
|
762
|
+
'role', 'member', 'label', 'Member',
|
|
763
|
+
'description', 'Can create sessions and contribute shared workspace content.',
|
|
764
|
+
'permissions', opengeni_private.workspace_member_role_permissions('member')
|
|
765
|
+
),
|
|
766
|
+
pg_catalog.jsonb_build_object(
|
|
767
|
+
'role', 'admin', 'label', 'Workspace admin',
|
|
768
|
+
'description', 'Can manage shared workspace settings, access, and integrations.',
|
|
769
|
+
'permissions', opengeni_private.workspace_member_role_permissions('admin')
|
|
770
|
+
)
|
|
771
|
+
),
|
|
772
|
+
'workspaces', coalesce((SELECT pg_catalog.jsonb_agg(
|
|
773
|
+
pg_catalog.jsonb_build_object(
|
|
774
|
+
'id', workspace.id, 'name', workspace.name, 'slug', workspace.slug,
|
|
775
|
+
'createdAt', workspace.created_at, 'updatedAt', workspace.updated_at,
|
|
776
|
+
'members', coalesce((SELECT pg_catalog.jsonb_agg(
|
|
777
|
+
pg_catalog.jsonb_build_object(
|
|
778
|
+
'membershipId', access.id,
|
|
779
|
+
'organizationMembershipId', org_member.id,
|
|
780
|
+
'subjectId', access.subject_id,
|
|
781
|
+
'name', auth_user.name,
|
|
782
|
+
'email', auth_user.email,
|
|
783
|
+
'subjectLabel', CASE
|
|
784
|
+
WHEN org_member.id IS NULL THEN access.subject_label
|
|
785
|
+
ELSE coalesce(auth_user.name, auth_user.email)
|
|
786
|
+
END,
|
|
787
|
+
'principalKind', CASE WHEN org_member.id IS NULL THEN 'service' ELSE 'human' END,
|
|
788
|
+
'organizationRole', org_member.role,
|
|
789
|
+
'role', opengeni_private.workspace_member_role(access.role, access.permissions),
|
|
790
|
+
'permissions', access.permissions,
|
|
791
|
+
'createdAt', access.created_at,
|
|
792
|
+
'updatedAt', access.updated_at
|
|
793
|
+
) ORDER BY lower(coalesce(auth_user.name, auth_user.email, access.subject_label,
|
|
794
|
+
access.subject_id)), access.id
|
|
795
|
+
)
|
|
796
|
+
FROM workspace_memberships access
|
|
797
|
+
LEFT JOIN organization_memberships org_member
|
|
798
|
+
ON org_member.account_id = p_account_id
|
|
799
|
+
AND org_member.subject_id = access.subject_id
|
|
800
|
+
LEFT JOIN auth_users auth_user
|
|
801
|
+
ON access.subject_id = 'user:' || auth_user.id
|
|
802
|
+
WHERE access.workspace_id = workspace.id), '[]'::jsonb)
|
|
803
|
+
) ORDER BY lower(workspace.name), workspace.id
|
|
804
|
+
) FROM shared_workspaces workspace), '[]'::jsonb)
|
|
805
|
+
) ELSE NULL END
|
|
806
|
+
INTO workspace_count, oversized_workspace, result
|
|
807
|
+
FROM managed_accounts account CROSS JOIN bounds WHERE account.id = p_account_id;
|
|
808
|
+
IF workspace_count > 500 THEN
|
|
809
|
+
RAISE EXCEPTION 'organization workspace inventory exceeds the bounded projection'
|
|
810
|
+
USING ERRCODE = '54000';
|
|
811
|
+
END IF;
|
|
812
|
+
IF oversized_workspace THEN
|
|
813
|
+
RAISE EXCEPTION 'workspace access inventory exceeds the bounded projection'
|
|
814
|
+
USING ERRCODE = '54000';
|
|
815
|
+
END IF;
|
|
816
|
+
RETURN result;
|
|
817
|
+
END
|
|
818
|
+
$body$;
|
|
819
|
+
|
|
820
|
+
REVOKE ALL ON TABLE organization_workspace_operation_receipts FROM PUBLIC;
|
|
821
|
+
REVOKE ALL ON TABLE organization_workspace_lifecycle_events FROM PUBLIC;
|
|
822
|
+
REVOKE ALL ON FUNCTION get_workspace_kind(uuid,uuid) FROM PUBLIC;
|
|
823
|
+
REVOKE ALL ON FUNCTION organization_workspace_command(jsonb) FROM PUBLIC;
|
|
824
|
+
REVOKE ALL ON FUNCTION resolve_organization_workspace_removal_subject(uuid,text,uuid) FROM PUBLIC;
|
|
825
|
+
REVOKE ALL ON FUNCTION prepare_organization_workspace_member_removal(jsonb) FROM PUBLIC;
|
|
826
|
+
REVOKE ALL ON FUNCTION record_organization_workspace_member_removal(jsonb,uuid,uuid) FROM PUBLIC;
|
|
827
|
+
REVOKE ALL ON FUNCTION list_organization_administration_members(uuid,text) FROM PUBLIC;
|
|
828
|
+
REVOKE ALL ON FUNCTION get_organization_administration_overview(uuid,text) FROM PUBLIC;
|
|
829
|
+
REVOKE ALL ON FUNCTION opengeni_private.workspace_member_role_permissions(text) FROM PUBLIC;
|
|
830
|
+
REVOKE ALL ON FUNCTION opengeni_private.workspace_member_role(text,jsonb) FROM PUBLIC;
|
|
831
|
+
REVOKE ALL ON FUNCTION opengeni_private.workspace_member_custom_permissions_valid(jsonb) FROM PUBLIC;
|
|
832
|
+
|
|
833
|
+
DO $pin_and_grant$
|
|
834
|
+
DECLARE data_schema text := current_schema();
|
|
835
|
+
BEGIN
|
|
836
|
+
EXECUTE format(
|
|
837
|
+
'ALTER FUNCTION %I.get_workspace_kind(uuid,uuid) SET search_path = pg_catalog, %I, pg_temp',
|
|
838
|
+
data_schema, data_schema
|
|
839
|
+
);
|
|
840
|
+
EXECUTE format(
|
|
841
|
+
'ALTER FUNCTION %I.organization_workspace_command(jsonb) SET search_path = pg_catalog, %I, pg_temp',
|
|
842
|
+
data_schema, data_schema
|
|
843
|
+
);
|
|
844
|
+
EXECUTE format(
|
|
845
|
+
'ALTER FUNCTION %I.resolve_organization_workspace_removal_subject(uuid,text,uuid) SET search_path = pg_catalog, %I, pg_temp',
|
|
846
|
+
data_schema, data_schema
|
|
847
|
+
);
|
|
848
|
+
EXECUTE format(
|
|
849
|
+
'ALTER FUNCTION %I.prepare_organization_workspace_member_removal(jsonb) SET search_path = pg_catalog, %I, pg_temp',
|
|
850
|
+
data_schema, data_schema
|
|
851
|
+
);
|
|
852
|
+
EXECUTE format(
|
|
853
|
+
'ALTER FUNCTION %I.record_organization_workspace_member_removal(jsonb,uuid,uuid) SET search_path = pg_catalog, %I, pg_temp',
|
|
854
|
+
data_schema, data_schema
|
|
855
|
+
);
|
|
856
|
+
EXECUTE format(
|
|
857
|
+
'ALTER FUNCTION %I.list_organization_administration_members(uuid,text) SET search_path = pg_catalog, %I, pg_temp',
|
|
858
|
+
data_schema, data_schema
|
|
859
|
+
);
|
|
860
|
+
EXECUTE format(
|
|
861
|
+
'ALTER FUNCTION %I.get_organization_administration_overview(uuid,text) SET search_path = pg_catalog, %I, pg_temp',
|
|
862
|
+
data_schema, data_schema
|
|
863
|
+
);
|
|
864
|
+
IF EXISTS (SELECT 1 FROM pg_roles WHERE rolname = 'opengeni_app') THEN
|
|
865
|
+
EXECUTE format(
|
|
866
|
+
'REVOKE ALL ON TABLE %I.organization_workspace_operation_receipts FROM opengeni_app',
|
|
867
|
+
data_schema
|
|
868
|
+
);
|
|
869
|
+
EXECUTE format(
|
|
870
|
+
'REVOKE ALL ON TABLE %I.organization_workspace_lifecycle_events FROM opengeni_app',
|
|
871
|
+
data_schema
|
|
872
|
+
);
|
|
873
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %I.get_workspace_kind(uuid,uuid) TO opengeni_app', data_schema);
|
|
874
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %I.organization_workspace_command(jsonb) TO opengeni_app', data_schema);
|
|
875
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %I.resolve_organization_workspace_removal_subject(uuid,text,uuid) TO opengeni_app', data_schema);
|
|
876
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %I.prepare_organization_workspace_member_removal(jsonb) TO opengeni_app', data_schema);
|
|
877
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %I.record_organization_workspace_member_removal(jsonb,uuid,uuid) TO opengeni_app', data_schema);
|
|
878
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %I.list_organization_administration_members(uuid,text) TO opengeni_app', data_schema);
|
|
879
|
+
EXECUTE format('GRANT EXECUTE ON FUNCTION %I.get_organization_administration_overview(uuid,text) TO opengeni_app', data_schema);
|
|
880
|
+
END IF;
|
|
881
|
+
END
|
|
882
|
+
$pin_and_grant$;
|
|
883
|
+
|
|
884
|
+
COMMENT ON FUNCTION organization_workspace_command(jsonb) IS
|
|
885
|
+
'Idempotent and CAS-fenced organization-admin create, rename, and named-role shared workspace grant command. Personal workspaces are excluded by canonical membership pointers.';
|
|
886
|
+
COMMENT ON FUNCTION get_workspace_kind(uuid,uuid) IS
|
|
887
|
+
'Machine-readable personal/shared workspace kind derived only from canonical organization membership personal-workspace authority.';
|
|
888
|
+
COMMENT ON FUNCTION list_organization_administration_members(uuid,text) IS
|
|
889
|
+
'Safe organization-admin member projection with shared-workspace access and no personal-workspace metadata. The legacy list_organization_members projection remains unchanged for rolling compatibility.';
|