【问题标题】:PostgreSQL - infinite recursion detected in policy for relationPostgreSQL - 在关系策略中检测到无限递归
【发布时间】:2018-06-22 15:56:21
【问题描述】:

在数据库中有 3 个表 - 部门、员工、帐户。一个部门有很多员工。 Employee 包含列 department_id bigint Account 表包含列 login varcharemployee_id bigint 并用于将 Postgres 用户(角色)绑定到 Employee 中的行。

我的目标是让用户只看到和使用那些 department_id 的值与用户相同的 Employee 行。

一定是这样的:

CREATE POLICY locale_policy ON employee
TO justuser, operator
USING (department_id =
    (SELECT department_id FROM employee WHERE id =
        (SELECT employee_id FROM account WHERE login = CURRENT_USER)
    )
)

但由于来自 Employee 的子查询,它正在提高 infinite recursion detected in policy for relation employee

编辑:关系定义为:

create table department(
    id serial primary key);
create table employee(
    id serial primary key,
    department_id int8 not null references department(id));
create table account(
    id serial primary key,
    login varchar(100) not null unique,
    employee_id int8 not null unique references employee(id));

【问题讨论】:

  • 请提供关系 ddl o 转载。此外,如果您为员工创建政策,SELECT department_id FROM employee 看起来很过分 - 为什么不 USING (id = (SELECT employee_id FROM account WHERE login = CURRENT_USER) ?...department_id 与 id 有一对多关系吗?...
  • @VaoTsun,我编辑了帖子,你是这个意思吗?许多员工可以拥有相同的department_id,这就是政策的重点 - 挑选具有重合部门的员工
  • 无法复制 - 抱歉 - 也许是在 rexter 上构建的?..

标签: sql postgresql policy rls


【解决方案1】:

唉 rexter 不允许创建角色.. http://rextester.com/QDYC6798

create table department(
    id serial primary key);
create table employee(
    id serial primary key,
    department_id int8 not null references department(id));
create table account(
    id serial primary key,
    login varchar(100) not null unique,
    employee_id int8 not null unique references employee(id));
insert into department default values;
insert into department default values;
insert into employee (department_id ) select 1;
insert into employee (department_id ) select 2;
insert into account (login,employee_id) select 'justuser',1;
insert into account (login,employee_id) select 'operator',2;
create role justuser;
create role operator;
set role justuser;
select * from employee;

无法复制。这不是一个答案 - 只是一个格式化的脚本。解决后我会删除它

【讨论】:

    【解决方案2】:

    嗯,我不知道它有多好,但它对我有用。我在创建当前用户部门的 id 的视图中找到了解决方案,然后检查它是否匹配:

    CREATE VIEW curr_department AS
        (SELECT department_id as id FROM employee WHERE id =
            (SELECT employee_id FROM account WHERE login = current_user)
        );
    
    CREATE POLICY locale_policy ON employee
        TO justuser, operator
        USING (department_id =
            (SELECT id FROM curr_department)
        );
    

    【讨论】:

    • 我相信这个工作的原因是因为 VIEW 由执行 CREATE VIEW 命令的任何用户拥有(在这种情况下,不是“员工”)。所以 VIEW 不受政策约束。
    猜你喜欢
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 1970-01-01
    • 2014-08-17
    • 2012-02-20
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多