【问题标题】:Manage user roles with Graphcool使用 Graphcool 管理用户角色
【发布时间】:2018-04-27 21:20:02
【问题描述】:

我正在使用模板 graphcool/templates/auth/email-password 和 Graphcool,我想添加管理用户角色的功能。

这是我的定义架构:

type User @model {
    id: ID! @isUnique
    createdAt: DateTime!
    updatedAt: DateTime!
    email: String! @isUnique
    password: String!
    role: UserRole!
}

enum UserRole {
    EDITOR,
    MODERATOR,
    ADMIN
}

我已经收到查询中的角色并将其保存在本地存储中,但任何人都可以更改它影响前端 UI(如果我们添加权限,我们不应该担心服务器端)。管理它的最佳/安全方式是什么?

【问题讨论】:

    标签: javascript jwt graphql react-apollo graphcool


    【解决方案1】:

    您在使用 Graphcool 框架吗?

    如果您需要在 graphcool.yml 中设置权限。我将包括以下内容:

    graphcool.yml

    - operation: User.create
      authenticated: true
      query: permissions/User.graphql:adminRole
    - operation: User.read
      authenticated: true
      query: permissions/User.graphql:adminRole
    - operation: User.update
      authenticated: true
      query: permissions/User.graphql:adminRole
    - operation: User.delete
      authenticated: true
      query: permissions/User.graphql:adminRole
    
    - operation: User.read
      authenticated: true
      query: permissions/User.graphql:user
      fields:
      - id
      - name
      - role
      - createdAt
      - updatedAt
      - email
      - company
    - operation: User.update
      authenticated: true
      query: permissions/User.graphql:user
      fields:
      - name
      - company
    

    User.graphql

    query user($node_id: ID, $user_id: ID!) {
      SomeUserExists(filter: {AND: [{id: $user_id}, {id: $node_id}]})
    }
    
    query adminRole($user_id: ID!) {
      SomeUserExists(filter: {id: $user_id, role: ADMIN})
    }
    

    这样用户只能更新他们的姓名和公司。然后管理员用户可以阅读和编辑每个人。只有 ADMIN 用户可以创建或更新新用户。

    那么您可能会问如何创建新用户? 我会使用 Graphcool 模板中的 FaaS 代码进行电子邮件密码身份验证:

    https://github.com/graphcool/templates/tree/master/auth/email-password

    signup.ts 文件应该告诉您新用户如何注册,然后管理员会为您创建一个新用户。在注册功能中,您可以将 UserRole 默认设置为您想要的任何内容。

    https://github.com/graphcool/templates/blob/master/auth/email-password/src/signup.ts

    【讨论】:

      猜你喜欢
      • 2011-03-20
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 2020-05-18
      • 2013-02-26
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      相关资源
      最近更新 更多