aegis v0.1.0 Aegis View Source

Lightweight, flexible authorization.

Link to this section Summary

Functions

Returns scope for a resource for a user for a given action as dictated by the resource’s corresponding policy definition

Returns true if a user is authorized to perform an action on a given resource as dictated by the resource’s corresponding policy definition

Link to this section Functions

Link to this function auth_scope(user, scope, action) View Source
auth_scope(user :: any(), scope :: any(), action :: atom()) :: any()

Returns scope for a resource for a user for a given action as dictated by the resource’s corresponding policy definition.

Example

defmodule Puppy do
  defstruct [id: nil, user_id: nil, hungry: false]
end

defmodule Puppy.Policy do
  @behaviour Aegis.Policy

  def scope(_user, _scope, :index), do: :index_scope
  def scope(_user, _scope, :show), do: :show_scope
end

defmodule Kitten do
  defstruct [id: nil, user_id: nil, hungry: false]
end

iex> user = :user iex> scope = %{from: {“puppies”, Puppy}} iex> Aegis.auth_scope(user, scope, :index) :index_scope iex> Aegis.auth_scope(user, scope, :show) :show_scope

iex> user = :user iex> scope = %{from: {“kittens”, Kitten}} iex> Aegis.auth_scope(user, scope, :index) ** (RuntimeError) Policy not found: Elixir.Kitten.Policy

Link to this function auth_scope(mod, user, scope, action) View Source
auth_scope(mod :: module(), user :: any(), scope :: any(), action :: atom()) :: any()
Link to this function authorized?(user, action, resource) View Source
authorized?(user :: any(), action :: atom(), resource :: any()) :: boolean()

Returns true if a user is authorized to perform an action on a given resource as dictated by the resource’s corresponding policy definition.

Example

defmodule Puppy do
  defstruct [id: nil, user_id: nil, hungry: false]
end

defmodule Puppy.Policy do
  @behaviour Aegis.Policy

  def authorize(_user, :index, _puppy), do: true
end

defmodule Kitten do
  defstruct [id: nil, user_id: nil, hungry: false]
end

iex> user = :user iex> resource = Puppy iex> Aegis.authorized?(user, :index, resource) true iex> Aegis.authorized?(user, :show, resource) false

iex> user = :user iex> action = :index iex> resource = Kitten iex> Aegis.authorized?(user, action, resource) ** (RuntimeError) Policy not found: Elixir.Kitten.Policy

Link to this function authorized?(mod, user, action, resource) View Source
authorized?(mod :: module(), user :: any(), action :: atom(), resource :: any()) :: boolean()
Link to this function fetch_policy_module(arg) View Source
fetch_policy_module(any()) :: module() | :error