-
-
Notifications
You must be signed in to change notification settings - Fork 6.1k
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
tiangolo
merged 3 commits into
tiangolo:master
from
sh0nk:translation/add-ja-security-get-current-user
Jan 28, 2024
+114
−0
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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を別のどんなところでも使うことができます: | ||||||
|
||||||
```Python hl_lines="5 12-16" | ||||||
{!../../../docs_src/security/tutorial002.py!} | ||||||
``` | ||||||
|
||||||
## 依存関係 `get_current_user` を作成 | ||||||
|
||||||
依存関係 `get_current_user` を作ってみましょう。 | ||||||
|
||||||
依存関係はサブ依存関係を持つことができるのを覚えていますか? | ||||||
|
||||||
`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!} | ||||||
``` | ||||||
|
||||||
## 現在のユーザーの注入 | ||||||
|
||||||
ですので、 `get_current_user` に対して同様に *path operation* の中で `Depends` を利用できます。 | ||||||
|
||||||
```Python hl_lines="31" | ||||||
{!../../../docs_src/security/tutorial002.py!} | ||||||
``` | ||||||
|
||||||
Pydanticモデルの `User` として、 `current_user` の型を宣言することに注意してください。 | ||||||
|
||||||
その関数の中ですべての入力補完や型チェックを行う際に役に立ちます。 | ||||||
|
||||||
!!! tip "豆知識" | ||||||
リクエストボディはPydanticモデルでも宣言できることを覚えているかもしれません。 | ||||||
|
||||||
ここでは `Depends` を使っているおかげで、 **FastAPI** が混乱することはありません。 | ||||||
|
||||||
|
||||||
!!! check "確認" | ||||||
依存関係システムがこのように設計されているおかげで、 `User` モデルを返却する別の依存関係(別の"dependables")を持つことができます。 | ||||||
|
||||||
同じデータ型を返却する依存関係は一つだけしか持てない、という制約が入ることはないのです。 | ||||||
|
||||||
|
||||||
## 別のモデル | ||||||
|
||||||
これで、*path operation関数* の中で現在のユーザーを直接取得し、`Depends` を使って、 **依存性注入** レベルでセキュリティメカニズムを処理できるようになりました。 | ||||||
|
||||||
そして、セキュリティ要件のためにどんなモデルやデータでも利用することができます。(この場合は、 Pydanticモデルの `User`) | ||||||
|
||||||
しかし、特定のデータモデルやクラス、型に制限されることはありません。 | ||||||
|
||||||
モデルを、 `id` と `email` は持つが、 `username` は全く持たないようにしたいですか? わかりました。同じ手段でこうしたこともできます。 | ||||||
|
||||||
ある `str` だけを持ちたい? あるいはある `dict` だけですか? それとも、データベースクラスのモデルインスタンスを直接持ちたいですか? すべて同じやり方で機能します。 | ||||||
|
||||||
実際には、あなたのアプリケーションにはログインするようなユーザーはおらず、単にアクセストークンを持つロボットやボット、別のシステムがありますか?ここでも、全く同じようにすべて機能します。 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 operation関数* の中で直接現在のユーザーを取得できるようになりました。 | ||||||
|
||||||
既に半分のところまで来ています。 | ||||||
|
||||||
あとは、 `username` と `password` を実際にそのユーザーやクライアントに送る、 *path operation* を追加する必要があるだけです。 | ||||||
|
||||||
次はそれを説明します。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be more natural.