mirror of
https://github.com/team-alembic/ash_authentication.git
synced 2024-09-20 21:33:10 +12:00
999bec00ee
* improvement: add policy utilities and accompanying guide fix: improve some error message/validation logic * chore: update castore & fix check definition * improvement: fix build/warnings/dialyzer/format * chore: add private context to new `get_token` action. * chore: fix build. I'd rather have the warning than a build failure. Co-authored-by: James Harton <james@harton.nz>
36 lines
1.1 KiB
Elixir
36 lines
1.1 KiB
Elixir
defmodule AshAuthentication.UserIdentity.Actions do
|
|
@moduledoc """
|
|
Code interface for provider identity actions.
|
|
|
|
Allows you to interact with UserIdentity resources without having to mess
|
|
around with changesets, apis, etc. These functions are delegated to from
|
|
within `AshAuthentication.UserIdentity`.
|
|
"""
|
|
|
|
alias Ash.{Changeset, Resource}
|
|
alias AshAuthentication.UserIdentity
|
|
|
|
@doc """
|
|
Upsert an identity for a user.
|
|
"""
|
|
@spec upsert(Resource.t(), map) :: {:ok, Resource.record()} | {:error, term}
|
|
def upsert(resource, attributes) do
|
|
with {:ok, api} <- UserIdentity.Info.user_identity_api(resource),
|
|
{:ok, upsert_action_name} <-
|
|
UserIdentity.Info.user_identity_upsert_action_name(resource),
|
|
action when is_map(action) <- Resource.Info.action(resource, upsert_action_name) do
|
|
resource
|
|
|> Changeset.new()
|
|
|> Changeset.set_context(%{
|
|
private: %{
|
|
ash_authentication?: true
|
|
}
|
|
})
|
|
|> Changeset.for_create(upsert_action_name, attributes,
|
|
upsert?: true,
|
|
upsert_identity: action.upsert_identity
|
|
)
|
|
|> api.create()
|
|
end
|
|
end
|
|
end
|