Unify Pundit user type across frontend and backend contexts

オープン
#191 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
38/100
issue の種類
リファクタリング
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
ruby

調査の方向性

まず 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_user to current_identity instead of current_user (the Backend::User)
  • Backend-specific policy checks (like admin?) continue to work because Identity#backend_user resolves the admin role
  • Remove resolve_backend_user type-checking from ProgramPolicy once the user type is consistent
  • Audit all policies for user.backend_user calls to make sure they work with the unified approach
  • AuditLogsController (frontend) should probably include Pundit::Authorization with pundit_user = current_identity so policy() 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

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

hackclub/auth のほかの issue

hackclub/auth の issue をすべて見る

似ている issue

Ruby の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。