Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🌐 Add Japanese translation for docs/ja/docs/tutorial/security/get-current-user.md #2681

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
114 changes: 114 additions & 0 deletions docs/ja/docs/tutorial/security/get-current-user.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
# 現在のユーザーの取得

一つ前の章では、(依存性注入システムに基づいた)セキュリティシステムは、 *path operation関数* に `str` として `token` を与えていました:

```Python hl_lines="10"
{!../../../docs_src/security/tutorial001.py!}
```

しかし、それはまだそんなに有用ではありません。

現在のユーザーを取得するようにしてみましょう。

## ユーザーモデルの作成

まずは、Pydanticのユーザーモデルを作成しましょう。

ボディを宣言するのにPydanticを使用するのと同じやり方で、Pydanticを別のどんなところでも使うことができます:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
ボディを宣言するのにPydanticを使用するのと同じやり方で、Pydanticを別のどんなところでも使うことができます:
ボディを宣言するのにPydanticを使用するのと同じやり方で、Pydanticを他のどんなところでも使うことができます:

It might be more natural.


```Python hl_lines="5 12-16"
{!../../../docs_src/security/tutorial002.py!}
```

## `get_current_user` への依存を作成
sh0nk marked this conversation as resolved.
Show resolved Hide resolved

`get_current_user` への依存を作ってみましょう。
sh0nk marked this conversation as resolved.
Show resolved Hide resolved

依存はサブ依存を持つことができるのを覚えていますか?
sh0nk marked this conversation as resolved.
Show resolved Hide resolved

`get_current_user` は前に作成した `oauth2_scheme` と同じ依存関係を持ちます。

以前直接 *path operation* の中でしていたのと同じように、新しい依存関係である `get_current_user` は `str` として `token` を受け取るようになります:

```Python hl_lines="25"
{!../../../docs_src/security/tutorial002.py!}
```

## ユーザーの取得

`get_current_user` は作成した(偽物の)ユーティリティ関数を使って、 `str` としてトークンを受け取り、先ほどのPydanticの `User` モデルを返却します:

```Python hl_lines="19-22 26-27"
{!../../../docs_src/security/tutorial002.py!}
```

## 現在のユーザーの注入

ですので、ここで *path operation* の中で先ほどの `get_current_user` とともに全く同様に `Depends` を利用できます。
sh0nk marked this conversation as resolved.
Show resolved Hide resolved

```Python hl_lines="31"
{!../../../docs_src/security/tutorial002.py!}
```

Pydanticモデルの `User` として、 `current_user` の型を宣言することに注意してください。

こうするのは、その関数の中ですべての入力補完や型チェックを行う際に役に立ちます。
sh0nk marked this conversation as resolved.
Show resolved Hide resolved

!!! tip "豆知識"
リクエストボディはPydanticモデルでも宣言できることを覚えているかもしれません。

ここでは `Depends` を使っているおかげで、 **FastAPI** が混乱することはありません。


!!! check "確認"
依存関係システムがこのように設計されているおかげで、 `User` モデルを返却する別の依存関係(別の"dependables")を持つことができます。

同じデータ型を返却する依存関係は一つだけしか持てない、という制約が入ることはないのです。


## 別のモデル

これで、*path operation関数* の中で現在のユーザーを直接取得し、`Depends` を使って、 **依存性注入** レベルでセキュリティメカニズムを処理できるようになりました。

そして、セキュリティ要件のためにどんなモデルやデータでも利用することができます。(この場合は、 Pydanticモデルの `User`)

しかし、特定のデータモデルやクラス、型に制限されることはありません。

モデルを、 `id` と `email` は持つが、 `username` は全く持たないようにしたいですか? わかりました。同じ手段でこうしたこともできます。

ある `str` だけを持ちたい? あるいはある `dict` だけですか? それとも、データベースクラスのモデルインスタンスを直接持ちたいですか? すべて同じやり方で機能します。

実際には、あなたのアプリケーションにはログインするようなユーザーはおらず、単にアクセストークンを持つロボットやボット、別のシステムがありますか?ここでも、全く同じようにすべて機能します。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
実際には、あなたのアプリケーションにはログインするようなユーザーはおらず、単にアクセストークンを持つロボットやボット、別のシステムがありますか?ここでも、全く同じようにすべて機能します
実際には、あなたのアプリケーションにログインするのはユーザーではなく、アクセストークンだけを持つロボットやボット、別のシステムですか?繰り返しますが、すべて同じように機能します

It might be more easy to read.


あなたのアプリケーションに必要なのがどんな種類のモデル、どんな種類のクラス、どんな種類のデータベースであったとしても、 **FastAPI** は依存性注入システムでカバーしてくれます。


## コードサイズ

この例は冗長に見えるかもしれません。セキュリティとデータモデルユーティリティ関数および *path operations* が同じファイルに混在しているということを覚えておいてください。

しかし、ここに重要なポイントがあります。

セキュリティと依存性注入に関するものは、一度だけ書きます。

そして、それは好きなだけ複雑にすることができます。それでも、一箇所に、一度だけ書くのです。すべての柔軟性を備えます。

しかし、同じセキュリティシステムを使って何千ものエンドポイント(*path operations*)を持つことができます。

そして、それらエンドポイントのすべて(必要な、どの部分でも)がこうした依存関係や、あなたが作成する別の依存関係を再利用する利点を享受できるのです。

さらに、こうした何千もの *path operations* は、たった3行で表現できるのです:

```Python hl_lines="30-32"
{!../../../docs_src/security/tutorial002.py!}
```

## まとめ

これで、 *path operatio関数* の中で直接現在のユーザーを取得できるようになりました。
sh0nk marked this conversation as resolved.
Show resolved Hide resolved

既に半分のところまで来ています。

あとは、 `username` と `password` を実際にそのユーザーやクライアントに送る、 *path operation* を追加する必要があるだけです。

次はそれを説明します。
1 change: 1 addition & 0 deletions docs/ja/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ nav:
- tutorial/body-updates.md
- セキュリティ:
- tutorial/security/first-steps.md
- tutorial/security/get-current-user.md
- tutorial/cors.md
- 高度なユーザーガイド:
- advanced/path-operation-advanced-configuration.md
Expand Down