mathewcst
07/25/2022, 4:37 PMNeedle
07/25/2022, 4:37 PMmathewcst
07/25/2022, 4:39 PMmathewcst
07/25/2022, 4:39 PMsql
create policy "Team members can update team members if they belong to the team."
on members
for all using (
team_id in (
select get_teams_for_user(auth.uid())
)
);mathewcst
07/25/2022, 4:42 PMgaryaustin
07/25/2022, 5:06 PMget_teams_for_user(auth.uid()) && get_teams_for_user(id)
Where && is array operator overlaps https://www.postgresql.org/docs/14/functions-array.html
This looks like it could be painful performance if you were just to select * from profiles as both functions would get run for each row... Not sure if you will have a filter to narrow the results. You might have two functions and declare get_teams_for_current_user() a stable function in hopes it does not run also on each row.mathewcst
07/25/2022, 5:08 PMmathewcst
07/25/2022, 5:11 PMgaryaustin
07/25/2022, 5:17 PMmathewcst
07/25/2022, 5:26 PMmathewcst
07/25/2022, 5:26 PMmathewcst
07/25/2022, 6:35 PMsql
create or replace function get_spaces_for_user(user_id uuid)
returns setof uuid as $$
select space_id
from members
where user_id = $1
$$ stable language sql security definer;
1) Created a new function for getting profiles from the same space
sql
create or replace function get_users_for_space(user_id uuid)
returns setof uuid as $$
SELECT a.user_id
FROM members a
WHERE a.space_id IN (
SELECT space_id
FROM members
WHERE space_id = a.space_id
)
$$ stable language sql security definer;
2) New policies
`members`: no policy
`profiles`: (id IN ( SELECT get_users_for_space(uid()) AS get_users_for_space))
`spaces`: (id IN ( SELECT get_spaces_for_user(uid()) AS get_spaces_for_user))Needle
07/25/2022, 6:36 PM