View Source Terminator.UUID (terminator_uuid v0.6.0)

Main Terminator module for including macros

Terminator has 3 main components:

relations-between-models

Relations between models

Terminator.UUID.Performer -> Terminator.UUID.Ability [1-n] - Any given performer can hold multiple abilities this allows you to have very granular set of abilities per each performer

Terminator.UUID.Performer -> Terminator.UUID.Role [1-n] - Any given performer can act as multiple roles this allows you to manage multple sets of abilities for multiple performers at once

Terminator.UUID.Role -> Terminator.UUID.Ability [m-n] - Any role can have multiple abilities therefore you can have multiple roles to have different/same abilities

calculating-abilities

Calculating abilities

Calculation of abilities is done by OR and DISTINCT abilities. That means if you have

Role[:admin, abilities: [:delete]], Role[:editor, abilities: [:update]], Role[:user, abilities: [:view]] and all roles are granted to single performer, resulting abilities will be [:delete, :update, :view]

available-permissions

Available permissions

Link to this section Summary

Functions

Macro for wrapping protected code

Defines calculated permission to be evaluated in runtime

Requires an ability within permissions block

Perform authorization on passed performer and abilities

Requires a role within permissions block

Perform role check on passed performer and role

Returns authorization result on collected performer and required roles/abilities

Macro for defining required permissions

Resets ETS table

Link to this section Functions

Link to this macro

as_authorized(list)

View Source (macro)

Macro for wrapping protected code

example

Example

defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    as_authorized do
      IO.inspect("This code is executed only for authorized performer")
    end
  end
end
Link to this macro

calculated(func_name)

View Source (macro)

Defines calculated permission to be evaluated in runtime

examples

Examples

defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    permissions do
      calculated(fn performer ->
        performer.email_confirmed?
      end)
    end

    as_authorized do
      IO.inspect("This code is executed only for authorized performer")
    end
  end
end

You can also use DSL form which takes function name as argument

  defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    permissions do
      calculated(:email_confirmed)
    end

    as_authorized do
      IO.inspect("This code is executed only for authorized performer")
    end
  end

  def email_confirmed(performer) do
    performer.email_confirmed?
  end
end

For more complex calculation you need to pass bindings to the function

  defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    post = %Post{owner_id: 1}

    permissions do
      calculated(:is_owner, [post])
      calculated(fn performer, [post] ->
        post.owner_id == performer.id
      end)
    end

    as_authorized do
      IO.inspect("This code is executed only for authorized performer")
    end
  end

  def is_owner(performer, [post]) do
    post.owner_id == performer.id
  end
end
Link to this macro

calculated(func_name, bindings)

View Source (macro)
@spec has_ability(atom()) :: {:ok, atom()}

Requires an ability within permissions block

example

Example

defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    permissions do
      has_ability(:can_run_test_authorization)
    end
  end
end
Link to this function

has_ability(ability, entity)

View Source
Link to this function

has_ability?(performer, ability_name)

View Source
@spec has_ability?(Terminator.UUID.Performer.t(), atom()) :: boolean()

Perform authorization on passed performer and abilities

Link to this function

has_ability?(performer, ability_name, entity)

View Source
@spec has_role(atom()) :: {:ok, atom()}

Requires a role within permissions block

example

Example

defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    permissions do
      has_role(:admin)
    end
  end
end
Link to this function

has_role?(performer, role_name)

View Source

Perform role check on passed performer and role

@spec is_authorized?() :: :ok | {:error, String.t()}

Returns authorization result on collected performer and required roles/abilities

example

Example

defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    case is_authorized? do
      :ok -> "Performer is authorized"
      {:error, message: _message} -> "Performer is not authorized"
  end
end
Link to this macro

permissions(list)

View Source (macro)

Macro for defining required permissions

example

Example

defmodule HelloTest do
  use Terminator.UUID

  def test_authorization do
    permissions do
      has_role(:admin)
      has_ability(:view)
    end
  end
end

Resets ETS table