policy_wonk v1.0.0 PolicyWonk.Enforce View Source
This turns your policy module into a plug that can be used in a router.
Usage
The only time you should directly use the PolicyWonk.Enforce
module is to call
use PolicyWonk.Enforce
when defining your policy module.
Example policy module:
defmodule MyAppWeb.Policies do
use PolicyWonk.Policy # set up support for policies
use PolicyWonk.Enforce # turn this module into an enforcement plug
def policy( assigns, :current_user ) do
case assigns[:current_user] do
%MyApp.Account.User{} ->
:ok
_ ->
{:error, :current_user}
end
end
def policy_error(conn, :current_user) do
MyAppWeb.ErrorHandlers.unauthenticated(conn, "Must be logged in")
end
end
To enforce your policies as a plug, you can just use the new module you created.
Enforce policies in a router:
pipeline :browser_session do
plug MyAppWeb.Policies, :current_user
plug MyAppWeb.Policies, [:policy_a, :policy_b]
end