【发布时间】:2018-10-07 17:51:17
【问题描述】:
我有下表:
Table "api_v1.person"
Column | Type | Modifiers
---------------+--------+-------------------------------------------------------
person_id | bigint | not null default...
name | text | not null
date_of_birth | date |
api_user | text | not null default "current_user"()
具有以下政策:
POLICY "api_user_only" FOR ALL
USING ((api_user = ("current_user"())::text))
WITH CHECK ((api_user = ("current_user"())::text))
我的理解是,该策略的FOR ALL 部分意味着它涵盖了插入,而WITH CHECK 确保插入到 api_user 中的值与当前用户相同,例如角色名称。 USING 子句应该只影响 SELECTS 或返回的其他数据。但是,当我尝试插入时,会得到以下结果:
demo=> INSERT INTO api_v1.person (name, api_user) VALUES ('Greg', current_user);
ERROR: query would be affected by row-level security policy for table "person"
如何插入?
我正在运行 PostgreSQL 9.6.8。
这是重现所需的 SQL:
BEGIN;
CREATE SCHEMA api_v1;
CREATE TABLE api_v1.person (
person_id BIGSERIAL PRIMARY KEY,
"name" TEXT NOT NULL,
date_of_birth DATE,
api_user TEXT NOT NULL DEFAULT current_user
);
ALTER TABLE api_v1.person ENABLE ROW LEVEL SECURITY;
CREATE POLICY
api_user_only
ON
api_v1.person
USING
(api_user = CURRENT_USER)
WITH CHECK
(api_user = CURRENT_USER)
;
CREATE ROLE test_role;
GRANT USAGE ON SCHEMA api_v1 TO test_role;
GRANT ALL ON api_v1.person TO test_role;
GRANT USAGE ON SEQUENCE api_v1.person_person_id_seq TO test_role;
COMMIT;
SET ROLE test_role;
INSERT INTO api_v1.person ("name") VALUES ('Greg');
【问题讨论】:
-
我试过你的例子,它适用于我的 PostgreSQL v10。您能否扩展问题以包含展示行为的完整示例(请提供完整的 SQL 语句)?
-
@LaurenzAlbe 我已添加完整的 SQL 语句来重现此问题。感谢您抽出宝贵时间提供帮助。
-
当我在 v10 和 9.6 上运行您的示例时,我收到错误
ERROR: permission denied for sequence person_person_id_seq。这与行级安全性无关,由GRANT USAGE ON SEQUENCE api_v1.person_person_id_seq TO test_role;修复。 -
@LaurenzAlbe 所以我授予使用该序列并将角色切换到 test_role 并且我仍然收到
ERROR: query would be affected by row-level security policy for table "person"消息。我可以作为超级用户插入,但不能插入 test_role。 -
我还测试了 PostgreSQL 10.3 的全新安装并得到了同样的错误。
标签: postgresql postgresql-9.6 row-level-security