Unify Pundit user type across frontend and backend contexts
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 38/100
- issue の種類
- リファクタリング
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- ruby
- 領域
- authorization, backend
調査の方向性
まず IdentityAuthorizable、バックエンドのコントローラー、ProgramPolicy、AuditLogsController、および issue で指定されている activity partials を読みます。user.backend_user を前提としている policy 呼び出しを監査し、フロントエンドとバックエンドのビューで Pundit がどのようにユーザーを選択しているかを追跡します。選択されたコンテキストで一貫して Identity が使用され、両方のコンテキストで policy チェックが機能し、ProgramPolicy の workaround が不要になれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Problem
We have two Pundit user types: Identity (frontend, via IdentityAuthorizable) and Backend::User (backend, via Backend::ApplicationController). Policies were written assuming they'd only ever see one type — and that was true when policies only ran inside controller actions, where the context is unambiguous.
Activity partials broke that assumption. Partials like _collaborator_invited.html.erb call policy(activity.trackable).manage_collaborators? to decide whether to show sensitive data (email addresses). These partials render in both frontend and backend views, so the policy can receive either user type. ProgramPolicy#admin? calls user.backend_user, which works on an Identity but raises NoMethodError on a Backend::User (because it is the backend user).
We fixed this in #188 with a resolve_backend_user helper that checks user.is_a?(Backend::User). It works, but it's the kind of thing that'll bite again the next time someone writes user.backend_user in a policy method without thinking about cross-context rendering.
Current workaround
# in ProgramPolicy
def resolve_backend_user
user.is_a?(Backend::User) ? user : user.backend_user
end
Activity partials also guard with activity.trackable && ... rescue false for nil trackables (deleted programs) and contexts where Pundit isn't available at all (frontend AuditLogsController).
Ideal end state
Every Pundit-evaluated context uses Identity as the user type. Backend controllers would set pundit_user to the Identity behind the Backend::User, and policies would check admin privileges through identity.backend_user uniformly. This is essentially making IdentityAuthorizable's approach the default.
What this would involve
- Backend controllers set
pundit_usertocurrent_identityinstead ofcurrent_user(theBackend::User) - Backend-specific policy checks (like
admin?) continue to work becauseIdentity#backend_userresolves the admin role - Remove
resolve_backend_usertype-checking fromProgramPolicyonce the user type is consistent - Audit all policies for
user.backend_usercalls to make sure they work with the unified approach AuditLogsController(frontend) should probably includePundit::Authorizationwithpundit_user = current_identitysopolicy()is available in its rendered partials
Scope
This doesn't need to happen all at once. The workaround is solid and the comment explains why it exists. But it's worth doing before more policies end up rendering across both contexts.
- 主要言語
- Ruby
- スター
- 85
- フォーク
- 72
- 平均マージ
- 3日 12時間
- マージ済み PR(30日)
- 22
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
hackclub/auth のほかの issue
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 86/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
メンテナーはふだん 1 日以内に返信
-
bug
難易度 1/5 1時間未満 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 57/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
SecureRandom.alphanumeric can silently return an empty or wrong-length secret instead of raisingオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
ruby/securerandom#92 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 85/100
open-feature/ruby-sdk-contrib#340 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
zold-io/wts.zold.io#651 ·
メンテナーはふだん 1 日以内に返信
-
good first issue help wanted
難易度 2/5 1〜3時間 初心者へのやさしさ 87/100
erkamyaman/rust-anything#788 ·
メンテナーはふだん 1 日以内に返信