【问题标题】:Hasura permissions to fetch data depending on users block statusHasura 根据用户阻止状态获取数据的权限
【发布时间】:2021-09-24 22:37:29
【问题描述】:

我有这些表:

Table blocked_users
id (pk)  | initiator_id (fk) | target_id (fk)
1        | a                 | b 


Table post
id (pk)  | author_profile (fk)
1        | a               
2        | b
3        | c
4        | d

我正试图了解正确的选择权限,我尝试了很多组合,但我无法获得所需的结果 -> 获取帖子并排除我已阻止或他们拥有的用户阻止了我。

这是我尝试过的众多权限之一:

blocked_by_meblocked_users.initiator_id → author_profile.id

been_blocked_byblocked_users.target_id → author_profile.id

{
  "_and": [
    { "deleted_at": { "_is_null": true } },
    { "author_profile": { "deleted_at": { "_is_null": true } } },
    {
      "_and": [
        {
          "author_profile": {
            "been_blocked_by": {
              "initiator_id": {
                "_eq": "X-Hasura-User-Id"
              }
            }
          }
        },
        {
          "author_profile": {
            "blocked_by_me": {
              "initiator_id": {
                "_eq": "X-Hasura-User-Id"
              }
            }
          }
        }
      ]
    }
  ]
}

我尝试过的众多查询之一:(对帖子没有权限)

query GetPosts(
  $created_at: order_by = desc
  $limit: Int! = 12
  $offset: Int! = 0
) {
  post(
    limit: $limit
    offset: $offset
    order_by: { created_at: $created_at }
    where: {
      _not: {
        author_profile: {
          _or: [
            {
              been_blocked_by: { initiator_id: { _eq: "a" } }
              blocked_by_me: { target_id: { _neq: "b" } }
            }
          ]
        }
      }
      _and: {
        deleted_at: { _is_null: true }
        author_profile: { deleted_at: { _is_null: true } }
      }
    }
  ) {
    author_profile {
      id
      first_name
    }
  }
}


通过上面的查询,如果用户发布了帖子,并且该用户的idblocked_users不存在,则查询不会返回该用户的帖子,换句话说,查询只返回在blocked_users 上至少有一条记录的用户的帖子(未被我阻止或该用户没有阻止我)。

【问题讨论】:

    标签: postgresql graphql hasura


    【解决方案1】:

    获取帖子并排除我已阻止或他们已阻止我的用户。

    您希望您的选择权限排除那些阻止您被您阻止的用户的帖子。使用与您相同的数组关系,我们可以为 post 编写权限,例如:

    {
      "_not": {
        "_or": [
          {
            "author_profile": {
              "been_blocked_by": { "initiator_id": { "_eq": "X-Hasura-User-Id" } }
            }
          },
          {
            "author_profile": {
              "blocked_by_me": { "target_id": { "_eq": "X-Hasura-User-Id" } }
            }
          }
        ]
      }
    }
    

    我们基本上是在告诉 Hasura 只显示 _not 的作者的帖子,这些作者有 been_blocked_by 我们 _or 的作者是屏蔽我们的作者。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 1970-01-01
      • 1970-01-01
      • 2012-01-15
      • 2023-03-30
      • 2015-09-30
      • 1970-01-01
      相关资源
      最近更新 更多