ThePhilip
01/21/2022, 1:26 AMScott P
01/21/2022, 1:57 AMsql
GRANT SELECT(id) ON public.my_table TO authenticated;
GRANT ALL(id) ON public.my_table TO authenticated;
GRANT INSERT(id), UPDATE(id) ON public.my_table TO authenticated;
The first example would grant only read / SELECT
permission to the authenticated
role, meaning that people in that role (i.e. usually everyone that logs in via the supabase libraries) to the id
column of my_table
.
The second example would grand all permissions (SELECT
, UPDATE
, DELETE
, INSERT
, etc).
The last example would grant only INSERT
and UPDATE
permissions.Scott P
01/21/2022, 2:01 AMsql
CREATE OR REPLACE FUNCTION public.prevent_user_updating_blocked_fields()
RETURNS trigger
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
RAISE EXCEPTION 'You can not update these fields';
END;
$BODY$;
CREATE TRIGGER trigger_prevent_user_updating_blocked_fields
BEFORE UPDATE OF created_at, email, id
ON public.my_table
FOR EACH ROW
EXECUTE FUNCTION public.prevent_user_updating_blocked_fields();
This should prevent users from updating the created_at
, email
or id
columns of my_table
.
Generally speaking, using column-level permissions would be the best route to take unless you need finer-grained logic (e.g. checking the auth.uid()
of the user trying to modify the table matches a specific value, and then either exiting early or allowing the request to proceed).Scott P
01/21/2022, 2:03 AMThePhilip
01/21/2022, 2:06 AMThePhilip
01/21/2022, 2:18 AMScott P
01/21/2022, 2:26 AMid
column matches auth.uid()
, but you don't have any column policies. The user can only perform actions against the rows they have access to, but they can modify any column.
- You don't have an RLS policy, but you do have a column policy preventing UPDATE
of id
column. Any user can perform any action against any row, but they can not update the ID
column of any row.
- You have an RLS policy, and a column policy preventing UPDATE
of id
column. The user can only modify rows they have access to, and they can perform any action against any column in those rows, except for the id
column which they will be unable to update.