這就是我一直在讀: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.`
實現對錶的行級安全性障礙,需要哪些步驟?