使用Graphcool管理用户角色

问题描述 投票:2回答:1

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

这是我的定义架构:

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个回答
2
投票

您使用的是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用户可以读取和编辑每个人。只有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

© www.soinside.com 2019 - 2024. All rights reserved.