这就是我一直在读:PostgreSQL的SELECT规则,继承,行级权限
http://www.postgresql.org/docs/9.2/static/rules-views.html
http://www.postgresql.org/docs/9.2/static/rules-privileges.html
我的目标是让一个登录只看到那些行,它“拥有”,可以这么说。
假设数据库中的每个表,从该表继承:
create table WHOAMI
(
tenant varchar(25) not null default current_user
);
例如:
create table FOO
(
id int primary key,
invoicedate date
) inherits (WHOAMI);
insert into FOO(id, invoicedate) values(1,now()::date);
select * from FOO;
--abclogin|1|2013-02-01
有PostgreSQL中这样的事,作为一个架构级的选择规则,影响模式所有表和视图,即追加到每一个选择,插入,更新或删除声明说,在,..AND WHERE TENANT = current_user
效果的条件?如果没有这样一个全球性的规则,是否可以一桌一桌地完成?我的尝试没有取得任何成功,并且可能误解了有关创建规则的一些事情。以下是我试图做:
我尝试创建一个选择规则:
CREATE RULE "_RETURN" AS ON SELECT TO FOO DO INSTEAD
SELECT * FROM FOO where tenant = current_user;
,但得到这个错误:ERROR: could not convert table "foo" to a view because it has indexes
我试图创建一个视图安全 - 屏障:
CREATE VIEW TENANTFOO WITH (security_barrier) AS
SELECT * FROM FOO WHERE tenant=current_user;
,然后尝试插入:
insert into TENANTFOO(id,invoicedate)
values(2,(now()::date);
,但得到这个错误:
`ERROR: cannot insert into view "tenantfoo"
HINT: You need an unconditional ON INSERT DO INSTEAD rule
or an INSTEAD OF INSERT trigger.`
实现对表的行级安全性障碍,需要哪些步骤?