rubbergloves v0.0.7 Rubbergloves.Handler
A series of macros to determine how to authorize a specific action for a principle.
Usage
1. Define what your rubber gloves can handle
defmodule MyApp.Gloves do
use Rubbergloves.Handler, wearer: MyApp.User
# wizzards can handle any poison
can_handle!(%MyApp.User{type: "wizzard"}, :pickup_poison, %{poison: _any})
# apprentice need to be qualified
can_handle?(%MyApp.User{type: "apprentice"} = apprentice, :pickup_poison, %{poison: poison}) do
Apprentice.is_qualified_to_handle?(apprentice, poison)
end
# Can use multipule phase checks, so if previosu phase fails we can fallback to other checks
phase :secondary_check do
can_handle?(%MyApp.User{race: "human"} = apprentice, :pickup_poison, %{poison: poison}) do
ImmunityDatabase.is_immune_to(apprentice, poison)
end
end
end
2. Check if wearer can handle
defmodule MyApp.SomeController do
def index(conn, params) do
user = get_principle_somehow()
with :ok <- MyApp.Gloves.handle(user, :read_secret_recipe, params. [:default, :secondary_check]) do
fetch_recipe(params["recipe_id"])
end
end
end
3. Providing Insights
defmodule MyApp.Gloves do
use Rubbergloves, wearer: MyApp.User
# Return boolean to provide no isights
can_handle?(user, :read_secret_recipe) do
false
end
# Optionally return {:error, reason} tuple to give better feedback
can_handle?(user, :read_secret_recipe) do
{:error, :novice_warlock}
end
end
Link to this section Summary
Functions
Macro to confirm that the priniciple can handle a given action with speciic conditions
Macro to check if the priniciple can handle a given action with speciic conditions.
Link to this section Functions
Macro to confirm that the priniciple can handle a given action with speciic conditions
i.e. allow everyone to do anything
can_handle!(_any_principle, _any_action, _any_conditions)
Macro to check if the priniciple can handle a given action with speciic conditions.
i.e. allow everyone to do anything
can_handle?(_any_principle, :action, _any_conditions) do true end