Current section

Files

Jump to
step_flow lib step_flow controllers helpers.ex
Raw

lib/step_flow/controllers/helpers.ex

defmodule StepFlow.Controller.Helpers do
@moduledoc """
The Helper Controller context.
"""
alias StepFlow.Roles
@doc """
Check whether a user is allowed to use a rightable structure
"""
def has_right(rightable, user, action) do
roles = Roles.get_roles(user.roles)
if roles != [] do
# Concat rights from roles
Enum.concat(for %{rights: rights} <- roles, do: rights)
# Filter entities
|> Enum.filter(fn element -> match?(^rightable, element.entity) end)
# Check if action is here
|> Enum.any?(fn element ->
Enum.find(element.action, fn local_action -> local_action == action end)
end)
else
false
end
end
end