refactor(web): extract shared current_actor controller helper
This commit is contained in:
parent
561779e704
commit
1adf6aa664
4 changed files with 28 additions and 20 deletions
22
lib/mv_web/controllers/controller_helpers.ex
Normal file
22
lib/mv_web/controllers/controller_helpers.ex
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue