terminator v0.5.1 Terminator View Source
Main Terminator module for including macros
Terminator has 3 main components:
Terminator.Ability
- Representation of a single permission e.g. :view, :delete, :updateTerminator.Performer
- Main actor which is holding given abilitiesTerminator.Role
- Grouped set of multiple abilities, e.g. :admin, :manager, :editor
Relations between models
Terminator.Performer
-> Terminator.Ability
[1-n] - Any given performer can hold multiple abilities
this allows you to have very granular set of abilities per each performer
Terminator.Performer
-> Terminator.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.Role
-> Terminator.Ability
[m-n] - Any role can have multiple abilities therefore
you can have multiple roles to have different/same 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
Terminator.has_ability/1
- Requires single ability to be present on performerTerminator.has_role/1
- Requires single role to be present on performer
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
as_authorized(list) View Source (macro)
Macro for wrapping protected code
Example
defmodule HelloTest do
use Terminator
def test_authorization do
as_authorized do
IO.inspect("This code is executed only for authorized performer")
end
end
end
calculated(func_name) View Source (macro)
Defines calculated permission to be evaluated in runtime
Examples
defmodule HelloTest do
use Terminator
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
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
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
calculated(func_name, bindings) View Source (macro)
has_ability(ability) View Source
Requires an ability within permissions block
Example
defmodule HelloTest do
use Terminator
def test_authorization do
permissions do
has_ability(:can_run_test_authorization)
end
end
end
has_ability(ability, entity) View Source
has_ability?(performer, ability_name)
View Source
has_ability?(Terminator.Performer.t(), atom()) :: boolean()
has_ability?(Terminator.Performer.t(), atom()) :: boolean()
Perform authorization on passed performer and abilities
has_ability?(performer, ability_name, entity) View Source
has_role(role) View Source
Requires a role within permissions block
Example
defmodule HelloTest do
use Terminator
def test_authorization do
permissions do
has_role(:admin)
end
end
end
has_role?(performer, role_name) View Source
Perform role check on passed performer and role
is_authorized?()
View Source
is_authorized?() :: :ok | {:error, String.t()}
is_authorized?() :: :ok | {:error, String.t()}
Returns authorization result on collected performer and required roles/abilities
Example
defmodule HelloTest do
use Terminator
def test_authorization do
case is_authorized? do
:ok -> "Performer is authorized"
{:error, message: _message} -> "Performer is not authorized"
end
end
permissions(list) View Source (macro)
Macro for defining required permissions
Example
defmodule HelloTest do
use Terminator
def test_authorization do
permissions do
has_role(:admin)
has_ability(:view)
end
end
end
reset_session() View Source
Resets ETS table