在 Postgraphile 中,对多对多关系使用权限

问题描述

如何使用 Postgraphile 定义多对多关系的扩展权限和授权?

想象一下我有这种多对多的关系(从 official docs 借来的,并用简化的权限进行了调整):

create table post (
  id serial primary key,headline text,body text,summary text
);
create table author (
  id serial primary key,name text
);

create type post_permission as enum (
  'owner',-- assume owner can change `headline`,`summary` and `body` of a post
  'editor' -- assume editor can modify `summary` of a post
);
create table post_author (
  post_id integer references post,author_id integer references author,permission post_permission,primary key (post_id,author_id)
);

我可以制定行级安全策略,例如:

create policy update_post on post for update to app_user using (
  EXISTS(
    SELECT 1
    FROM post_author as pa
    WHERE pa.post_id = post.id 
      AND pa.author_id = app_hidden.current_user_id()
      AND pa.permission = 'owner'
  )
);

-- Assume `app_hidden.current_user_id()` returns a logged in user id

但由于我是最近将 MysqL 转换为 Postgresql,因此我想看看是否可以针对尝试的更改进行上面的策略检查 pa.permission,并且只允许 permission = owner 更新所有字段一个帖子,而拥有 permission = editor用户可以只更新 summary

我知道这通常是在应用层而不是数据库中处理的,但我想我会先看看什么是可能的。

谢谢!

另见related topic here

解决方法

根据调查和反复试验,这似乎是最好使用用于更新帖子的自定义函数来解决的问题。

所有者可以通过 GraphQL/Postgraphile 使用此功能:

create function updatePost(
  headline text,body text,summary text
) returns post as $$
-- implement this function to check that the user found via 
-- app_hidden.current_user_id() exists in join table
-- with an `owner` permission
 -- then modify post
$$ language plpgsql strict security definer;

编辑器可以通过 GraphQL/Postgraphile 使用此功能:

create function updatePostMeta(
  summary text
) returns post as $$
-- implement this function to check that the user found via 
-- app_hidden.current_user_id() exists in join table 
-- with an `editor` or `owner` permission
-- then modify post
$$ language plpgsql strict security definer;

此外,使用 RLS,人们会希望防止任何人直接通过 GraphQL/Postgraphile 更改 post,因此我们只让用户 SELECT 来自 post