ash/lib/ash/authorizer.ex

95 lines
3.1 KiB
Elixir
Raw Normal View History

defmodule Ash.Authorizer do
2020-06-02 17:47:25 +12:00
@moduledoc """
The interface for an ash authorizer
These will typically be implemented by an extension, but a custom
one can be implemented by defining an extension that also adopts this behaviour.
Then you can extend a resource with `authorizers: [YourAuthorizer]`
2020-06-02 17:47:25 +12:00
"""
2020-05-21 10:59:58 +12:00
@type state :: map
2020-05-31 15:55:01 +12:00
@type context :: map
@callback initial_state(
Ash.Resource.t(),
Ash.Resource.record(),
Ash.Resource.Actions.action(),
boolean
) :: state
2020-05-21 10:59:58 +12:00
@callback strict_check_context(state) :: [atom]
2020-05-31 15:55:01 +12:00
@callback strict_check(state, context) ::
{:authorized, state}
2020-05-30 19:29:27 +12:00
| {:continue, state}
| {:filter, Keyword.t()}
| {:filter, Keyword.t(), state}
2020-05-31 15:55:01 +12:00
| {:filter_and_continue, Keyword.t(), state}
| {:error, term}
@callback alter_filter(filter :: Ash.Filter.t(), state, context) ::
{:ok, Ash.Filter.t()} | {:error, Ash.Error.t()}
2023-06-06 05:30:17 +12:00
@callback add_calculations(Ash.Query.t() | Ash.Changeset.t(), state, context) ::
{:ok, Ash.Query.t() | Ash.Changeset.t(), state} | {:error, Ash.Error.t()}
2023-06-06 05:30:17 +12:00
@callback alter_results(state, list(Ash.Resource.record()), context) ::
{:ok, list(Ash.Resource.record())} | {:error, Ash.Error.t()}
2020-05-21 10:59:58 +12:00
@callback check_context(state) :: [atom]
2020-05-31 15:55:01 +12:00
@callback check(state, context) ::
:authorized | {:data, list(Ash.Resource.record())} | {:error, term}
@callback exception(atom, state) :: no_return
@optional_callbacks [exception: 2, add_calculations: 3, alter_results: 3, alter_filter: 3]
2020-05-21 10:59:58 +12:00
def initial_state(module, actor, resource, action, verbose?) do
module.initial_state(actor, resource, action, verbose?)
end
def exception(module, reason, state) do
if function_exported?(module, :exception, 2) do
module.exception(reason, state)
else
if reason == :must_pass_strict_check do
Ash.Error.Forbidden.MustPassStrictCheck.exception([])
else
Ash.Error.Forbidden.exception([])
end
end
end
2020-05-21 10:59:58 +12:00
def strict_check_context(module, state) do
2023-06-23 06:19:40 +12:00
Enum.uniq(module.strict_check_context(state) ++ [:query, :changeset])
2020-05-21 10:59:58 +12:00
end
def strict_check(module, state, context) do
module.strict_check(state, context)
end
2023-06-06 05:30:17 +12:00
def add_calculations(module, query_or_changeset, state, context) do
if function_exported?(module, :add_calculations, 3) do
module.add_calculations(query_or_changeset, state, context)
else
{:ok, query_or_changeset, state}
2023-06-06 05:30:17 +12:00
end
end
def alter_results(module, state, records, context) do
if function_exported?(module, :alter_results, 3) do
2023-06-06 05:30:17 +12:00
module.alter_results(state, records, context)
else
{:ok, records}
end
end
def alter_filter(module, state, filter, context) do
if function_exported?(module, :alter_filter, 3) do
module.alter_filter(filter, state, context)
else
{:ok, filter}
end
end
2020-05-21 10:59:58 +12:00
def check_context(module, state) do
module.check_context(state)
end
2020-05-31 15:55:01 +12:00
def check(module, state, context) do
module.check(state, context)
2020-05-21 10:59:58 +12:00
end
end