我当前有一个具有两个架构app_private和app_public(除了默认的公共架构)的数据库。我还有一个角色,该角色已被授予app_public模式的使用权限,但未被授予[[app_private模式的使用权限。我还在桌上使用了两个函数(一个触发函数和一个检查约束函数)。
请参见下面的代码:((1)创建模式(和授权)
CREATE SCHEMA app_public;
CREATE SCHEMA app_private;
grant usage on schema public, app_public to "grant_test_role";
权限。((2)撤消来自PUBLIC用户的授权]然后我有这个特殊的DDL语句。对于公共用户角色(所有其他角色都继承自)的任何新添加的功能,应该具有
REVOKE
alter default privileges revoke all on functions from public;
((3)函数定义(触发器和约束)]
-- Trigger Function create OR replace function app_private.tg__timestamps() returns trigger as $$ begin NEW.created_at = (case when TG_OP = 'INSERT' then NOW() else OLD.created_at end); NEW.updated_at = (case when TG_OP = 'UPDATE' and OLD.updated_at >= NOW() then OLD.updated_at + interval '1 millisecond' else NOW() end); return NEW; end; $$ language plpgsql volatile set search_path to pg_catalog, app_private, public, pg_temp; -- Constraint Function CREATE OR REPLACE FUNCTION app_private.constraint_max_length( value text, maxLength integer, error_message text default 'The value "$1" is too long. It must be maximum $2 characters long.', error_code text default 'MXLEN' ) RETURNS boolean AS $$ begin if length(value) > maxLength then error_text = replace(replace(error_message, '$1', value), '$2', maxLength); raise exception '%', error_text using errcode = error_code; end if; return true; end; $$ LANGUAGE plpgsql set search_path to pg_catalog, app_private, public, pg_temp;
((4)表定义(使用上面的触发器和约束函数)]
create table app_public.test_tab ( id INT not null primary key, name text not null, created_at timestamptz not null default now(), updated_at timestamptz not null default now(), constraint name_length_check check (app_private.constraint_max_length(name, 5)); ); create trigger _100_timestamps before insert or update on app_public.test_tab for each row execute procedure app_private.tg__timestamps(); -- Setting some restrictions on the test_tab for the "grant_test_role" REVOKE ALL ON TABLE app_public.test_tab FROM "grant_test_role"; GRANT SELECT, DELETE ON app_public.test_tab TO "grant_test_role"; GRANT INSERT(id, name), UPDATE(id, name) ON app_public.test_tab TO "grant_test_role";
运行)(5)代码(以grant_test_role
begin;
set local role to grant_test_role;
insert into app_public.test_tab (id, name) values (1, 'Very Long Name');
commit;
我每次都试图在一个新的数据库中执行此操作,以了解PostgreSQL权限如何在不同的调用上下文中工作(例如,触发函数,自动检查函数的约束检查等)模式的事件,但该事件。但是在存在代码块(2)的情况下,代码可以很好地执行触发器,但为我提供了[当我没有从PUBLIC用户撤消功能许可的代码块(2)时,代码块(5)的执行没有任何错误。尽管用户角色没有授予存在触发器功能和约束功能的
app_private
"permission denied for function constraint_max_length"
作为检查约束。所以我想了解,到目前为止,这就是PostgreSQL的工作方式(无论它是否符合SQL规范:)