refactor(web): extract shared current_actor controller helper

This commit is contained in:
Moritz 2026-06-16 15:06:34 +02:00 committed by moritz
parent 561779e704
commit 1adf6aa664
4 changed files with 28 additions and 20 deletions

View file

@ -0,0 +1,22 @@
defmodule MvWeb.ControllerHelpers do
@moduledoc """
Shared helpers for plug-based controllers.
The LiveView equivalent lives in `MvWeb.LiveHelpers`; this module is the
controller-side counterpart that works on a `Plug.Conn`.
"""
alias Mv.Authorization.Actor
@doc """
Returns the request actor for a controller, loaded for authorization.
Reads `:current_user` from the connection assigns and ensures it is loaded via
`Mv.Authorization.Actor.ensure_loaded/1`.
"""
@spec current_actor(Plug.Conn.t()) :: Mv.Accounts.User.t() | nil
def current_actor(conn) do
conn.assigns[:current_user]
|> Actor.ensure_loaded()
end
end