View Source Post (ex_membership v1.0.3)
Summary
Functions
Macro for wrapping protected code
Returns authorization result on collected member and required features/plans
Defines calculated permission to be evaluated in runtime
Requires a feature within permissions block
Perform feature check on passed member and feature
Requires an plan within permissions block
Perform authorization on passed member and plans
Requires an role within permissions block
Perform authorization on passed member and roles
The Function list to ignore when building the permissions registry's
Load the plans into ets for the module/functions
Macro for defining required permissions
Functions
Macro for wrapping protected code
Example
defmodule HelloTest do
use Membership
member = HelloTest.Repo.get(Membership.Member, 1)
{:ok, member } = load_and_authorize_member(member)
def test_authorization do
as_authorized(member, :test_authorization) do
IO.inspect("This code is executed only for authorized member")
end
end
end
@spec authorized?(Membership.Member.t(), String.t()) :: :ok | {:error, String.t()}
Returns authorization result on collected member and required features/plans
Example
defmodule HelloTest do
use Membership
def test_authorization do
case authorized? do
:ok -> "Member is authorized"
{:error, message: _message} -> "Member is not authorized"
end
end
end
Defines calculated permission to be evaluated in runtime
Examples
defmodule HelloTest do
use Membership
member = HelloTest.Repo.get(Membership.Member, 1)
{:ok, member } = load_and_authorize_member(member)
def test_authorization do
permissions do
calculated(fn member ->
member.email_confirmed?
end)
end
as_authorized(member) do
IO.inspect("This code is executed only for authorized member")
end
end
end
You can also use DSL form which takes function name as argument
defmodule HelloTest do
use Membership
def test_authorization do
use Membership
member = HelloTest.Repo.get(Membership.Member, 1)
{:ok, member } = load_and_authorize_member(member)
permissions do
calculated(member,:email_confirmed)
end
as_authorized(member) do
IO.inspect("This code is executed only for authorized member")
end
end
def email_confirmed(member) do
member.email_confirmed?
end
end
For more complex calculation you need to pass bindings to the function
defmodule HelloTest do
use Membership
member = HelloTest.Repo.get(Membership.Member, 1)
{:ok, member} = load_and_authorize_member(member)
def test_authorization do
post = %Post{owner_id: 1}
permissions do
calculated(member,:is_owner, [post])
calculated(fn member, [post] ->
post.owner_id == member.id
end)
end
as_authorized(member) do
IO.inspect("This code is executed only for authorized member")
end
end
def is_owner(member, [post]) do
post.owner_id == member.id
end
end
delete_post(id \\ 1, member_id \\ 1, function_name \\ "delete_post")
View SourceRequires a feature within permissions block
Example
defmodule HelloTest do
use Membership
def test_authorization do
permissions do
has_feature(:admin)
end
end
end
Perform feature check on passed member and feature
Requires an plan within permissions block
Example
defmodule HelloTest do
use Membership
def test_authorization do
permissions do
has_plan(:gold, :test_authorization)
end
end
end
@spec has_plan?(Membership.Member.t(), atom(), String.t()) :: boolean()
Perform authorization on passed member and plans
Requires an role within permissions block
Example
defmodule HelloTest do
use Membership
def test_authorization do
permissions do
has_role(:gold, :test_authorization)
end
end
end
@spec has_role?(Membership.Member.t(), atom(), String.t()) :: boolean()
Perform authorization on passed member and roles
The Function list to ignore when building the permissions registry's
Load the plans into ets for the module/functions
Macro for defining required permissions
Example
defmodule HelloTest do
use Membership
def test_authorization do
permissions do
has_feature(:admin_feature, :test_authorization)
has_plan(:gold, :test_authorization)
end
end
end