如何锁定某些列以防止在 postgresql 中为用户编辑
How to lock certain columns from being edited for a user in postgresql
如何锁定 某些列 不被编辑,即使用户有权访问 postgresql 中 table 的编辑权限。
您可以添加一个触发器,如果禁用的列被更改,它会呕吐:
CREATE OR REPLACE FUNCTION cerberus() RETURNS trigger
LANGUAGE plpgsql AS
$$BEGIN
IF NEW.forbiddencol IS DISTINCT FROM OLD.forbiddencol
AND current_user = 'luser'
THEN
RAISE EXCEPTION '"luser" must not update "forbiddencol"';
END IF;
RETURN NEW;
END;$$;
CREATE TRIGGER cerberus BEFORE UPDATE OF mytable
FOR EACH ROW EXECUTE PROCEDURE cerberus();
PostgreSQL 支持列安全(以及行安全)
让我们称呼我们有限的角色authors
create table staff (
name text primary key,
salary decimal(19,4)
);
create role authors;
grant select, insert, delete, update(name) on table staff to authors;
set role authors;
insert into staff values ('frank', 100); -- works!
select * from staff; -- works!
update staff set name='jim'; -- works!
update staff set salary=999; -- permission denied
如何锁定 某些列 不被编辑,即使用户有权访问 postgresql 中 table 的编辑权限。
您可以添加一个触发器,如果禁用的列被更改,它会呕吐:
CREATE OR REPLACE FUNCTION cerberus() RETURNS trigger
LANGUAGE plpgsql AS
$$BEGIN
IF NEW.forbiddencol IS DISTINCT FROM OLD.forbiddencol
AND current_user = 'luser'
THEN
RAISE EXCEPTION '"luser" must not update "forbiddencol"';
END IF;
RETURN NEW;
END;$$;
CREATE TRIGGER cerberus BEFORE UPDATE OF mytable
FOR EACH ROW EXECUTE PROCEDURE cerberus();
PostgreSQL 支持列安全(以及行安全)
让我们称呼我们有限的角色authors
create table staff (
name text primary key,
salary decimal(19,4)
);
create role authors;
grant select, insert, delete, update(name) on table staff to authors;
set role authors;
insert into staff values ('frank', 100); -- works!
select * from staff; -- works!
update staff set name='jim'; -- works!
update staff set salary=999; -- permission denied