I'm new to Supabase and Postgres (though not new to SQL) but I'm having a difficult time wrapping my head around writing policies.
Let's say I have a member
table structured like this:
// member //
id (text) | name (text) | group (text) | admin (int)
----------------------------------------------------
abc | Bob | alpha | 0
----------------------------------------------------
xyz | Sally | bravo | 1
----------------------------------------------------
The id
in the member
table matches the id
of the authenticated users of my app.
I want to restrict other tables in my database based on group:
// program //
id (text) | name (text) | group (text)
--------------------------------------
def | First | alpha
--------------------------------------
ghi | Second | bravo
--------------------------------------
Desired policy #1:
Allow any user to
SELECT
if they are in the program'sgroup
.
Desired policy #2:
Allow any user to
INSERT
,UPDATE
, orDELETE
if they are in the program'sgroup
and if they are anadmin
.
I think I'm supposed to write a USING
statement that is something like:
SELECT program
WHERE group IN (
SELECT group
FROM member
WHERE member.id == auth().id
)
...and then a similar one for policy #2 that adds AND member.admin = 1
, but I'm not clear on this.
Am I on the right track? Any suggestions?