【问题标题】:How are multiple @auth directives handled on an AWS AppSync GraphQL Schema?如何在 AWS AppSync GraphQL 架构上处理多个 @auth 指令?
【发布时间】:2020-07-12 12:38:46
【问题描述】:

我正在尝试创建具有一些属性的单个模型:

type Guest @model {
    id: ID!
    name: String
    dayGuest: Boolean
    attending: Boolean
    dietryRequirements: String
    owner: String
}

我想调整不同用户在此模型中访问数据的方式:

  1. 在 cognito 中分配到“管理员”组的用户应该能够创建、读取、更新和删除记录中的所有字段。

  2. 记录的所有者应该能够读取每个字段并更新attendingdietryRequirements 字段。

  3. 任何拥有有效 JWT 的用户都应该能够更新 owner 字段。

为了实现这一点,我实现了以下@auth 指令:

  1. @auth(rules: [{allow: groups, groups: ["admins"]}])Guest 模型上
  2. @auth(rules: [{allow: owner, ownerField: "owner", operations:[read]}])Guest 模型上,@auth(rules: [{allow: owner, ownerField: "owner", operations: [update]}])attendingdietryRequirements 字段上。
  3. @auth(rules: [{allow: private, operations: [update]}])owner 字段上。

最终模型如下所示:

type Guest @model @auth(rules: [{allow: groups, groups: ["admins"]}, {allow: owner, ownerField: "owner", operations:[read]}]){
    id: ID!
    name: String
    dayGuest: Boolean
    attending: Boolean @auth(rules: [{allow: owner, ownerField: "owner", operations: [update]}])
    dietryRequirements: String @auth(rules: [{allow: owner, ownerField: "owner", operations: [update]}])
    owner: String @auth(rules: [{allow: private, operations: [update]}])
}

这似乎不起作用,我不知道为什么。 管理员用户可以查看所有内容并创建新对象。 经过验证的用户(非所有者)无法更新所有者字段,因为这会返回未经授权的错误。 所有者可以查看所有记录,无论他们是否是每个特定记录的所有者。

我怎样才能实现我想要的?

【问题讨论】:

  • 嘿,你有没有找到任何解决方案来关联这个多个@auth 指令

标签: graphql amazon-cognito aws-amplify aws-appsync aws-amplify-cli


【解决方案1】:

当您执行字段级身份验证时,您实际上是在为该字段设置新的身份验证规则。阻止管理员更新“参加”和“饮食要求”。

您还必须在此字段中包含管理员身份验证规则。 例如:

attending: Boolean 
@auth(rules: [
  {allow: owner, ownerField: "owner", operations: [read, update]}
  {allow: groups, groups: ["admins"]} 
])

Documentation

【讨论】:

    猜你喜欢
    • 2020-05-19
    • 2021-03-04
    • 2019-04-15
    • 2021-11-22
    • 2019-06-23
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 2019-05-03
    相关资源
    最近更新 更多