gpt4 book ai didi

postgresql - PostgreSQL 中的传递/嵌套权限

转载 作者:行者123 更新时间:2023-11-29 12:49:17 24 4
gpt4 key购买 nike

我有以下设置:

create role test_user noinherit nologin;

create table test_me ( attr text );
create view v_test_me as select * from test_me;

create function fn() returns trigger language plpgsql as $$
begin
insert into test_me(attr) values (NEW.attr);
return NEW;
end; $$;
create trigger tg instead of insert on v_test_me
for each row execute procedure fn();

grant insert on v_test_me to test_user;

set role test_user;
insert into v_test_me(attr) values ('hello?');

这里我有一张 table 和一张 table 上的 View 。该 View 有一个 instead-of-trigger。我允许 test_user 插入到 View 中。但是我收到以下错误:

ERROR:  permission denied for table test_me
CONTEXT: SQL statement "insert into test_me(attr) values (NEW.attr)"

看起来我没有权限在 test_me 表上执行 insert 语句,这是预期的。有什么方法可以让用户插入 test_me 表而不直接授予他们这样的权限?

最佳答案

在这种情况下,触发函数 fn() 上的 SECURITY DEFINER 修饰符将解决问题。根据 CREATE FUNCTION documentation :

SECURITY DEFINER specifies that the function is to be executed with the privileges of the user that created it

这将允许插入到 test_me 表中,前提是函数的所有者具有此类权限。

关于postgresql - PostgreSQL 中的传递/嵌套权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58089401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com