ash_authentication/lib/ash_authentication/user_identity/actions.ex
Zach Daniel 999bec00ee
improvement: add policy utilities and accompanying guide (#119)
* 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>
2023-01-12 15:34:41 +13:00

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