samefold
09/02/2021, 1:16 AM-----------------------------------------------------------
| user_id | first_name | last_name | email_address | role |
-----------------------------------------------------------
In an UPDATE operation, how could I allow users to edit some but not all fields for their own record? Obviously it's not good if a customer can make themselves an administrator or change their user_id
.jason-lynx
09/02/2021, 2:37 AMjason-lynx
09/02/2021, 2:38 AMjason-lynx
09/02/2021, 2:38 AMjason-lynx
09/02/2021, 2:40 AMCREATE FUNCTION clean_update()
RETURNS SETOF trigger
LANGUAGE 'plpgsql'
AS $BODY$
BEGIN
NEW.id := OLD.id;
NEW.role := OLD.role;
RETURN NEW;
END;
$BODY$;
CREATE TRIGGER clean_update_on_my_table
BEFORE UPDATE
ON public.<MY TABLE>
FOR EACH ROW
EXECUTE PROCEDURE public.clean_update();
so before any update is made to your table, the id and role will be set to whatever it was previously, ignoring those fields in the incoming requestjason-lynx
09/02/2021, 2:40 AM.rpc
, since in that function you can control exactly the fields you want to updatesamefold
09/02/2021, 1:24 PM