【发布时间】:2021-08-19 03:35:42
【问题描述】:
我正在 Amplify 中实现一个项目,并有 3 种 GraphQL 模式类型,如下所示。它是一个具有不同用户类型的多租户应用程序。
type Seller
@model
@auth (rules: [
{ allow: owner, ownerField: "id", operations: [read, update] },
])
{
id: ID!
name: String
...
}
type Customer
@model
@auth (rules: [
{ allow: owner, ownerField: "id", operations: [read, update] },
])
{
id: ID!
name: String
...
}
type Order
@model
@auth (rules: [
{ allow: owner, ownerField: "seller_id", operations: [create, read, update, delete] },
{ allow: owner, ownerField: "customer_id", operations: [read] },
])
{
id: ID!
seller_id: ID
seller: Seller @connection(fields: ["seller_id"])
customer_id: ID
customer: Customer @connection(fields: ["customer_id"])
quantity: Int
product: String
...
}
当 Customer 类型的用户登录并调用 listOrders 时,它会为属性“sellers”返回 null,这可能是因为 Seller 的 @auth 规则只允许所有者读取访问权限。我可以通过在卖方中放置一个组规则来解决这个问题,如下所示:
type Seller
...
@auth (rules: [
...
{ allow: groups, groups: ["Customer"], operations: [read] },
...
但是,客户现在可以查询所有卖家(通过 listSellers 或 getSellers),我不希望他们这样做。
有没有办法定义授权,以便如果客户在 Order 上获得授权,并且 Order 包含对象卖方,则允许对该卖方进行授权?
【问题讨论】:
-
更新:我尝试关闭卖家查询 (@model(queries: null))。这样就保证了Seller info只能通过Order.seller属性查询,当然有Sellers不能查询自己的效果。
标签: aws-amplify