SimpleCan
A simple library to facilitate authorization in your app. This was inspired by Canada but with a more straightforward API.
Installation
The package can be installed by adding simple_can to your list of dependencies in mix.exs:
def deps do
[
{:simple_can, "~> 0.1.0"}
]
endThe docs can be found at https://hexdocs.pm/simple_can.
Usage
Define an implementation of the
SimpleCan.Canprotocol for a specific moduledefimpl SimpleCan.Can, for: MyApp.User do def can?(%MyApp.User{role: role}, :create, %MyApp.OtherResource{}) do case role do :admin -> true :user -> false _ -> false end end def can?(%MyApp.User{}, action, %MyApp.OtherResource{}) when action in [:read, :view] do true end endImport
SimpleCanand use itnote: (you can optionally specify
only: [can?: 3]but that is the only function defined anyway)import SimpleCan def create_object(object) do if can?(current_user, :create, object) do {:ok, create(object)} else {:error, :unauthorized} end end