Skip to content

Commit a558c8a

Browse files
ydfzgyjlinhe0x0
authored andcommitted
BestPractice-Authorization.md (xitu#42)
1 parent ae2f0e2 commit a558c8a

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed
Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
---
2-
title: Authorization
2+
title: 授权
33
layout: ../_core/DocsLayout
4-
category: Best Practices
4+
category: 最佳实践
55
permalink: /learn/authorization/
66
next: /learn/pagination/
77
---
88

9-
> Delegate authorization logic to the business logic layer
9+
> 将授权逻辑委托给业务逻辑层
1010
11-
Authorization is a type of business logic that describes whether a given user/session/context has permission to perform an action or see a piece of data. For example:
11+
授权是一种业务逻辑,描述给定的用户、会话、上下文是否具有执行操作的权限或查看一条数据的权限。例如:
1212

13-
*“Only authors can see their drafts”*
13+
**“只有作者才能看到他们自己的草稿”**
1414

15-
Enforcing this kind of behavior should happen in the [business logic layer](/learn/thinking-in-graphs/#business-logic-layer). It is tempting to place authorization logic in the GraphQL layer like so:
15+
应当在 [业务逻辑层](/learn/thinking-in-graphs/#business-logic-layer) 实施这种行为。在 GraphQL 层中放置授权逻辑是很吸引人的,如下所示:
1616

1717
```javascript
1818
var postType = new GraphQLObjectType({
19-
name: Post,
19+
name: 'Post',
2020
fields: {
2121
body: {
2222
type: GraphQLString,
2323
resolve: (post, args, context, { rootValue }) => {
24-
// return the post body only if the user is the post's author
24+
// 只有当用户是帖子的作者时才返回帖子正文
2525
if (context.user && (context.user.id === post.authorId)) {
2626
return post.body;
2727
}
@@ -32,16 +32,16 @@ var postType = new GraphQLObjectType({
3232
});
3333
```
3434

35-
Notice that we define “author owns a post" by checking whether the post's `authorId` field equals the current user’s `id`. Can you spot the problem? We would need to duplicate this code for each entry point into the service. Then if the authorization logic is not kept perfectly in sync, users could see different data depending on which API they use. Yikes! We can avoid that by having a [single source of truth](/learn/thinking-in-graphs/#business-logic-layer) for authorization.
35+
可以看到我们通过检查帖子的 `authorId` 字段是否等于当前用户的 `id` 来定义“作者拥有一个帖子”。你能发现其中的问题吗?我们需要复制这段代码到服务中的每一个入口端点。一旦我们无法保证授权逻辑的完全同步,用户可能在使用不同的 API 时看到不同的数据。我们可以通过确定授权的 [唯一真实来源](/learn/thinking-in-graphs/#business-logic-layer) 来避免这种情况。
3636

37-
Defining authorization logic inside the resolver is fine when learning GraphQL or prototyping. However, for a production codebase, delegate authorization logic to the business logic layer. Here’s an example:
37+
在学习 GraphQL 或原型设计时,在解析器内定义授权逻辑是可以接受的。然而,对于生产代码库来说,还是将授权逻辑委托给业务逻辑层。这里有一个例子:
3838

3939
```javascript
40-
//Authorization logic lives inside postRepository
40+
// 授权逻辑在 postRepository
4141
var postRepository = require('postRepository');
4242

4343
var postType = new GraphQLObjectType({
44-
name: Post,
44+
name: 'Post',
4545
fields: {
4646
body: {
4747
type: GraphQLString,
@@ -53,6 +53,10 @@ var postType = new GraphQLObjectType({
5353
});
5454
```
5555

56-
In the example above, we see that the business logic layer requires the caller to provide a user object. If you are using GraphQL.js, the User object should be populated on the `context` argument or `rootValue` in the fourth argument of the resolver.
56+
在上面的例子中,我们看到业务逻辑层要求调用者提供一个用户对象。如果您使用 GraphQL.js,您应当在解析器的 `context` 参数或是第四个参数中的 `rootValue` 上填充 User 对象。
5757

58-
We recommend passing a fully-hydrated User object instead of an opaque token or API key to your business logic layer. This way, we can handle the distinct concerns of [authentication](/graphql-js/authentication-and-express-middleware/) and authorization in different stages of the request processing pipeline.
58+
我们建议将完全混合 <sup>[\[1\]](#note1)</sup> 的 User 对象传递给业务逻辑层,而非传递不透明的 token 或 API 密钥。这样,我们可以在请求处理管道的不同阶段处理 [身份验证](/graphql-js/authentication-and-express-middleware/) 和授权的不同问题。
59+
60+
<ol>
61+
<li><a name="note1"></a> “混合(hydrated)”一个对象是指:对一个存储在内存中且尚未包含任何域数据(“真实”数据)的对象,使用域数据(例如来自数据库、网络或文件系统的数据)进行填充。 <a href="https://stackoverflow.com/questions/6991135/what-does-it-mean-to-hydrate-an-object">*</a></li>
62+
</ol>

0 commit comments

Comments
 (0)