简体   繁体   English

AWS Amplify with GraphQL - 定义不同类型用户的身份验证规则

[英]AWS Amplify with GraphQL - Defining authentication rules by different types of users

Using Amplify, GraphQL, AppSync, Cognito, DynamoDB使用 Amplify、GraphQL、AppSync、Cognito、DynamoDB

Having the following model:具有以下model:

type Post
@model
{
  id: ID!
  content: String!
  author: String!
}

I want my rules to enable the following case:我希望我的规则启用以下情况:

  1. Only Admin users can create, update and delete Post只有管理员用户可以创建、更新和删除帖子
  2. Some Posts where only premium users allow to read一些只有高级用户才允许阅读的帖子
  3. Some Posts where all logged in users allow to read一些所有登录用户都允许阅读的帖子
  4. Some Posts where all users (also unauthenticated) allow to read所有用户(也未经身份验证)允许阅读的一些帖子

What is the best way to implement it using the mentioned tools?使用上述工具实现它的最佳方法是什么?

Thanks谢谢

From your question, it is not clear how you define "Some Posts" and how you would differentiate one from another.从你的问题来看,不清楚你如何定义“一些帖子”以及你如何将一个帖子与另一个帖子区分开来。 If I was designing this, I would have at least one more field in my Post type to manage the access level (For example: 3 (Admin) > 2 (Premium) > 1 (Logged-in) > 0 (Unregistered)), like so;如果我正在设计这个,我将在我的Post类型中至少有一个字段来管理访问级别(例如:3(管理员)> 2(高级)> 1(登录)> 0(未注册)),像这样;

type Post
@model
{
  id: ID!
  content: String!
  author: String!
  accessLevel: Int!
}

To manage this on user level, I think your best bet is to manage it using Cognito groups (like mentioned in the official documentation ) and assign appropriate permission for each group.要在用户级别进行管理,我认为最好的办法是使用 Cognito 组(如官方文档中所述)进行管理,并为每个组分配适当的权限。

Things you would need in Cognito:在 Cognito 中你需要的东西:

  1. A user pool which will contain all of your registered users.一个包含所有注册用户的用户

  2. A user group for premium members.高级会员的用户

  3. A user group for your admins.您的管理员的用户

Things you would need in your AppSync:您在 AppSync 中需要的东西:

  1. For Admin users to create, update and delete Post:管理员用户创建、更新和删除帖子:

     type Mutation { createPost(id:ID,: content,String:: author:String:),Post: @aws_auth(cognito_groups, ["Admin"]) updatePost(id:ID:: content:String,: author,String:):Post: @aws_auth(cognito_groups: ["Admin"]) deletePost(id:ID!, content:String!, author:String!):Post! @aws_auth(cognito_groups: ["Admin"]) }
  2. For some posts only visible to premium , logged-in or unregistered users to read:对于一些只对premium登录或未注册用户可见的帖子:

     type Query { getPost(id:ID:):Post! @aws_api_key @aws_cognito_user_pools }

    Furthermore, you can use the accessLevel in your resolver to filter out the result based on which post you want to be visible to premium, logged-in or unregistered users.此外,您可以使用解析器中的accessLevel根据您希望高级用户、登录用户或未注册用户看到的帖子来过滤结果。

I used @Myz answers.我使用了@Myz 的答案。 And https://aws.amazon.com/blogs/mobile/graphql-security-appsync-amplify/ for full solution:https://aws.amazon.com/blogs/mobile/graphql-security-appsync-amplify/完整的解决方案:

  type Post
  @model
  @auth(
    rules: [
      { allow: owner }
      { allow: groups, groups: ["Admin"], operations: [create, update, delete] }
      { allow: groups, groupsField: "group", operations: [read] }
    ]
  ) {
  id: ID!
  content: String!
  author: String!
  group: [String] # or String for a single group
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Amplify GraphQL 回溯架构 - AWS Amplify GraphQL Recusive Schema 在 AWS Amplify GraphQL 架构中使用用户信息 - Using user info in AWS Amplify GraphQL schema aws amplify appsync 中的 Graphql 变异错误 - Graphql mutation error in aws amplify appsync 如何在 AWS Amplify GraphQL 客户端中进行过滤 - How to do filteration in AWS Amplify GraphQL Client AWS Amplify CLI 生成的 GraphQL 突变中的 $condition 输入参数是什么? - What is the $condition input parameter for in a GraphQL mutation generated by AWS Amplify CLI? AWS Amplify React GET 请求错误 - 缺少身份验证令牌 - AWS Amplify React GET request error - missing authentication token AWS Amplify Authentication 使用相同的 email 多次登录 - AWS Amplify Authentication sign in multiple times with same email Apollo Vue (using AWS Amplify/AppSync) Graphql 查询不拉父数据 - Apollo Vue (using AWS Amplify/AppSync) Graphql query does not pull parent data AWS Amplify 部署失败 - AWS Amplify deployments failing 使用 Javascript 的 Amplify SDK 为未经授权的用户使用 Cognito 身份池访问 AWS API 网关方法 - Access AWS API Gateway method using Cognito Identity pool for unauthorized users using Amplify SDK for Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM