Skip to content

Commit 495d241

Browse files
committed
allow viewing PlatformAccounts of shared spaces
1 parent 722a54a commit 495d241

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

packages/database/src/dbTypes.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,10 @@ export type Database = {
12911291
}
12921292
}
12931293
account_in_shared_space: {
1294-
Args: { p_account_id: number }
1294+
Args: {
1295+
access_level?: Database["public"]["Enums"]["SpaceAccessPermissions"]
1296+
p_account_id: number
1297+
}
12951298
Returns: boolean
12961299
}
12971300
author_of_concept: {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CREATE OR REPLACE FUNCTION public.account_in_shared_space(p_account_id BIGINT, access_level public."SpaceAccessPermissions" = 'reader') RETURNS boolean
2+
STABLE SECURITY DEFINER
3+
SET search_path = ''
4+
LANGUAGE sql AS $$
5+
SELECT EXISTS (
6+
SELECT 1
7+
FROM public."LocalAccess" AS la
8+
JOIN public."SpaceAccess" AS sa USING (space_id)
9+
JOIN public.my_user_accounts() ON (sa.account_uid = my_user_accounts)
10+
WHERE la.account_id = p_account_id
11+
AND sa.permissions >= access_level
12+
);
13+
$$;
14+
15+
DROP POLICY IF EXISTS platform_account_select_policy ON public."PlatformAccount";
16+
CREATE POLICY platform_account_select_policy ON public."PlatformAccount" FOR SELECT USING (dg_account = (SELECT auth.uid() LIMIT 1) OR public.account_in_shared_space(id, 'partial'));
17+
18+
DROP POLICY IF EXISTS agent_identifier_select_policy ON public."AgentIdentifier";
19+
20+
DROP function public.account_in_shared_space(p_account_id BIGINT);
21+
22+
CREATE POLICY agent_identifier_select_policy ON public."AgentIdentifier" FOR SELECT USING (public.account_in_shared_space(account_id));

packages/database/supabase/schemas/account.sql

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ $$;
286286

287287
COMMENT ON FUNCTION public.in_space IS 'security utility: does current user have access to this space?';
288288

289-
CREATE OR REPLACE FUNCTION public.account_in_shared_space(p_account_id BIGINT) RETURNS boolean
289+
CREATE OR REPLACE FUNCTION public.account_in_shared_space(p_account_id BIGINT, access_level public."SpaceAccessPermissions" = 'reader') RETURNS boolean
290290
STABLE SECURITY DEFINER
291291
SET search_path = ''
292292
LANGUAGE sql AS $$
@@ -296,7 +296,7 @@ LANGUAGE sql AS $$
296296
JOIN public."SpaceAccess" AS sa USING (space_id)
297297
JOIN public.my_user_accounts() ON (sa.account_uid = my_user_accounts)
298298
WHERE la.account_id = p_account_id
299-
AND sa.permissions >= 'reader'
299+
AND sa.permissions >= access_level
300300
);
301301
$$;
302302

@@ -459,7 +459,7 @@ WHERE id IN (
459459
DROP POLICY IF EXISTS platform_account_policy ON public."PlatformAccount";
460460

461461
DROP POLICY IF EXISTS platform_account_select_policy ON public."PlatformAccount";
462-
CREATE POLICY platform_account_select_policy ON public."PlatformAccount" FOR SELECT USING (dg_account = (SELECT auth.uid() LIMIT 1) OR public.account_in_shared_space(id));
462+
CREATE POLICY platform_account_select_policy ON public."PlatformAccount" FOR SELECT USING (dg_account = (SELECT auth.uid() LIMIT 1) OR public.account_in_shared_space(id, 'partial'));
463463

464464
DROP POLICY IF EXISTS platform_account_delete_policy ON public."PlatformAccount";
465465
CREATE POLICY platform_account_delete_policy ON public."PlatformAccount" FOR DELETE USING (dg_account = (SELECT auth.uid() LIMIT 1) OR (dg_account IS null AND public.unowned_account_in_shared_space(id)));

0 commit comments

Comments
 (0)