我使用的模板图形/模板/auth/电子邮件密码与Graphcool,我想添加管理用户角色的能力。
这是我的定义模式:
type User @model {
id: ID! @isUnique
createdAt: DateTime!
updatedAt: DateTime!
email: String! @isUnique
password: String!
role: UserRole!
}
enum UserRole {
EDITOR,
MODERATOR,
ADMIN
}我已经在接受查询中的角色,并将其保存在本地存储中,但是任何人都可以更改它,从而影响前端UI (如果我们添加权限,那么在服务器端就不用担心了)。管理它的最佳/安全方法是什么?
发布于 2017-12-22 16:19:22
您正在使用Graph酷框架吗?
如果您需要在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
- companyUser.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})
}这样,用户只能更新他们的名字和公司。然后管理员用户可以读取和编辑每个人。只有管理员用户才能创建或更新新用户。
那么,您可能会问如何创建新用户?我将使用图形模板中的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
https://stackoverflow.com/questions/47294038
复制相似问题