mirror of
https://github.com/team-alembic/ash_authentication.git
synced 2024-09-19 12:52:55 +12:00
improvement: infer api
from a resource
This commit is contained in:
parent
557627a77b
commit
bb81521d6c
7 changed files with 22 additions and 9 deletions
|
@ -51,8 +51,7 @@ defmodule AshAuthentication.Dsl do
|
|||
api: [
|
||||
type: {:behaviour, Api},
|
||||
doc:
|
||||
"The name of the Ash API to use to access this resource when doing anything authenticaiton related.",
|
||||
required: true
|
||||
"The name of the Ash API to use to access this resource when doing anything authenticaiton related."
|
||||
],
|
||||
get_by_subject_action_name: [
|
||||
type: :atom,
|
||||
|
|
|
@ -11,8 +11,7 @@ defmodule AshAuthentication.TokenResource do
|
|||
type: {:behaviour, Ash.Api},
|
||||
doc: """
|
||||
The Ash API to use to access this resource.
|
||||
""",
|
||||
required: true
|
||||
"""
|
||||
],
|
||||
expunge_expired_action_name: [
|
||||
type: :atom,
|
||||
|
|
|
@ -34,7 +34,8 @@ defmodule AshAuthentication.TokenResource.Transformer do
|
|||
@spec transform(map) ::
|
||||
:ok | {:ok, map} | {:error, term} | {:warn, map, String.t() | [String.t()]} | :halt
|
||||
def transform(dsl_state) do
|
||||
with {:ok, dsl_state} <-
|
||||
with {:ok, dsl_state} <- maybe_set_api(dsl_state, :token),
|
||||
{:ok, dsl_state} <-
|
||||
maybe_build_attribute(dsl_state, :jti, :string,
|
||||
primary_key?: true,
|
||||
allow_nil?: false,
|
||||
|
|
|
@ -30,7 +30,8 @@ defmodule AshAuthentication.Transformer do
|
|||
@spec transform(map) ::
|
||||
:ok | {:ok, map} | {:error, term} | {:warn, map, String.t() | [String.t()]} | :halt
|
||||
def transform(dsl_state) do
|
||||
with :ok <- validate_at_least_one_strategy(dsl_state),
|
||||
with {:ok, dsl_state} <- maybe_set_api(dsl_state, :authentication),
|
||||
:ok <- validate_at_least_one_strategy(dsl_state),
|
||||
:ok <- validate_unique_strategy_names(dsl_state),
|
||||
:ok <- validate_unique_add_on_names(dsl_state),
|
||||
{:ok, dsl_state} <- maybe_transform_token_lifetime(dsl_state),
|
||||
|
|
|
@ -7,8 +7,7 @@ defmodule AshAuthentication.UserIdentity do
|
|||
schema: [
|
||||
api: [
|
||||
type: {:behaviour, Ash.Api},
|
||||
doc: "The Ash API to use to access this resource.",
|
||||
required: true
|
||||
doc: "The Ash API to use to access this resource."
|
||||
],
|
||||
user_resource: [
|
||||
type: {:behaviour, Ash.Resource},
|
||||
|
|
|
@ -34,7 +34,8 @@ defmodule AshAuthentication.UserIdentity.Transformer do
|
|||
@spec transform(map) ::
|
||||
:ok | {:ok, map} | {:error, term} | {:warn, map, String.t() | [String.t()]} | :halt
|
||||
def transform(dsl_state) do
|
||||
with {:ok, resource} <- persisted_option(dsl_state, :module),
|
||||
with {:ok, dsl_state} <- maybe_set_api(dsl_state, :user_identity),
|
||||
{:ok, resource} <- persisted_option(dsl_state, :module),
|
||||
{:ok, dsl_state} <-
|
||||
maybe_build_attribute(dsl_state, :id, Type.UUID,
|
||||
allow_nil?: false,
|
||||
|
|
|
@ -79,6 +79,19 @@ defmodule AshAuthentication.Utils do
|
|||
def maybe_concat(collection, test, _new_elements) when is_falsy(test), do: collection
|
||||
def maybe_concat(collection, _test, new_elements), do: Enum.concat(collection, new_elements)
|
||||
|
||||
@doc """
|
||||
Used within transformers to infer `api` from a resource if the option is not set.
|
||||
"""
|
||||
def maybe_set_api(dsl_state, section) do
|
||||
api = Transformer.get_persisted(dsl_state, :api)
|
||||
|
||||
if api && !Transformer.get_option(dsl_state, [section], :api) do
|
||||
{:ok, Transformer.set_option(dsl_state, [section], :api, api)}
|
||||
else
|
||||
{:ok, dsl_state}
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
Used within transformers to optionally build actions as needed.
|
||||
"""
|
||||
|
|
Loading…
Reference in a new issue