Exop v1.3.0 Exop.Policy behaviour View Source

Provides macros for policy validation.

Example

defmodule MonthlyReportPolicy do
  # not only Keyword or Map as an argument since 1.1.1
  def can_read?(%{user_role: "admin"}), do: true
  def can_read?("admin"), do: true
  def can_read?(%User{role: "manager"}), do: true
  def can_read?(:manager), do: true
  def can_read?(_opts), do: false

  def can_write?(%{user_role: "manager"}), do: true
  def can_write?(_opts), do: false
end

defmodule ReadOperation do
  use Exop.Operation

  policy MonthlyReportPolicy, :can_read?

  parameter :user, required: true, struct: %User{}

  def process(params) do
    authorize(params.user)

    # make some reading...
  end
end

If authorization fails, any code after (below) auth check is postponed: an error {:error, {:auth, _reason}} is returned immediately.

Link to this section Summary

Callbacks

Authorizes the possibility to invoke an action.

Link to this section Types

Link to this type

t() View Source
t() :: Exop.Policy

Link to this section Callbacks

Link to this callback

authorize(atom, any) View Source
authorize(atom(), any()) :: true | false

Authorizes the possibility to invoke an action.