我正在尝试创建具有一些属性的单个模型:
type Guest @model {
id: ID!
name: String
dayGuest: Boolean
attending: Boolean
dietryRequirements: String
owner: String
}
我想调整不同用户如何访问此模型中的数据:
在 cognito 中分配到“管理员”组的用户应该能够创建、读取、更新和删除记录中的所有字段。
记录的所有者应该能够读取每个字段并更新
attending
和dietryRequirements
字段。- 任何拥有有效 JWT 的用户都应该能够更新该
owner
字段。
为了实现这一点,我实现了以下@auth 指令:
@auth(rules: [{allow: groups, groups: ["admins"]}])
在Guest
模型上@auth(rules: [{allow: owner, ownerField: "owner", operations:[read]}])
在Guest
模型和和字段@auth(rules: [{allow: owner, ownerField: "owner", operations: [update]}])
上。attending
dietryRequirements
@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]}])
}
这似乎不起作用,我不知道为什么。管理员用户可以查看所有内容并创建新对象。经过验证的用户(非所有者)无法更新所有者字段,因为这会返回未经授权的错误。所有者可以查看所有记录,无论他们是否是每个特定记录的所有者。
我怎样才能达到我想要的?