[英]How do you prevent updates to the ownerField of an AWS Amplify GraphQL API?
I created a GraphQL API using AWS' Amplify.我使用 AWS 的 Amplify 创建了 GraphQL API。 In the schema, I have a Comment model that looks like this:
在架构中,我有一条评论 model,如下所示:
type Comment
@auth(rules: [{ allow: owner }, { allow: private, operations: [read] }, { allow: public, operations: [read] }])
@model
@key(name: "byAuthor", fields: ["authorID"])
@key(name: "byPost", fields: ["postID"]) {
content: String!
createdAt: AWSDateTime!
id: ID!
owner: String
postID: ID!
updatedAt: AWSDateTime!
}
This gives the owner permission to create, read, update, and delete, and restricts unauthenticated/authenticated-non-owner users to read-only.这赋予所有者创建、读取、更新和删除的权限,并将未经身份验证/经过身份验证的非所有者用户限制为只读。 This works as expected;
这按预期工作; however, the owner can update the ownerField's value, essentially attributing the comment to another user...which is a no-no.
但是,所有者可以更新 ownerField 的值,本质上是将评论归因于另一个用户……这是一个禁忌。 To prevent this, I tried using field-level permissions (see below);
为了防止这种情况,我尝试使用字段级权限(见下文); however, that doesn't appear to be stopping the update.
但是,这似乎并没有停止更新。
...
owner: String @auth(rules: [{ allow: owner, operations: [ create ]}])
...
Is there something I'm missing?有什么我想念的吗? Any help is much appreciated--thank you!
非常感谢任何帮助-谢谢!
You're very close.你很亲密。 Using a field-level
@auth
directive, you want to explicitly grant the owner the ability to create
and read
and explicitly deny the world the ability to update
and delete
.使用字段级
@auth
指令,您希望明确授予所有者create
和read
的能力,并明确拒绝世界update
和delete
的能力。 It might look something like:它可能看起来像:
type Todo
@model
@auth(rules: [{ allow: owner, ownerField: "author" }]) {
id: ID!
name: String!
author: String
@auth(
rules: [
{ allow: groups, groups: ["FORBIDDEN"], operations: [update, delete] }
{ allow: owner, ownerField: "author", operations: [create, read] }
]
)
}
Here, the "FORBIDDEN" group is a non-existent group.这里,“FOBIDDEN”组是不存在的组。 Its name can be anything so long as you do not plan to actually create a group by that name in the future.
它的名称可以是任何名称,只要您将来不打算使用该名称实际创建组即可。 Because no user will ever have the "FORBIDDEN" group claim, any/all
update
or delete
operations on the field will fail.因为任何用户都不会拥有“FORBIDDEN”组声明,所以该字段上的任何/所有
update
或delete
操作都将失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.