fix(TokenResource): don't silently drop notifications about token removal. (#432)

Closes #420.
This commit is contained in:
James Harton 2023-09-22 13:48:47 +12:00 committed by GitHub
parent 048da649d2
commit 6de08ac9a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,7 +3,7 @@ defmodule AshAuthentication.TokenResource.Actions do
The code interface for interacting with the token resource. The code interface for interacting with the token resource.
""" """
alias Ash.{Changeset, DataLayer, Query, Resource} alias Ash.{Changeset, DataLayer, Notifier, Query, Resource}
alias AshAuthentication.{TokenResource, TokenResource.Info} alias AshAuthentication.{TokenResource, TokenResource.Info}
import AshAuthentication.Utils import AshAuthentication.Utils
@ -48,8 +48,12 @@ defmodule AshAuthentication.TokenResource.Actions do
} }
) )
|> case do |> case do
{:ok, :ok} -> :ok {:ok, {:ok, notifications}} ->
{:error, reason} -> {:error, reason} Notifier.notify(notifications)
:ok
{:error, reason} ->
{:error, reason}
end end
:error -> :error ->
@ -203,7 +207,7 @@ defmodule AshAuthentication.TokenResource.Actions do
resource |> Query.new() |> Query.set_context(%{private: %{ash_authentication?: true}}), resource |> Query.new() |> Query.set_context(%{private: %{ash_authentication?: true}}),
query <- Query.for_read(query, read_expired_action_name, opts), query <- Query.for_read(query, read_expired_action_name, opts),
{:ok, expired} <- api.read(query) do {:ok, expired} <- api.read(query) do
Enum.reduce_while(expired, :ok, fn record, :ok -> Enum.reduce_while(expired, {:ok, []}, fn record, {:ok, notifications} ->
record record
|> Changeset.new() |> Changeset.new()
|> Changeset.set_context(%{ |> Changeset.set_context(%{
@ -212,10 +216,16 @@ defmodule AshAuthentication.TokenResource.Actions do
} }
}) })
|> Changeset.for_destroy(expunge_expired_action_name, opts) |> Changeset.for_destroy(expunge_expired_action_name, opts)
|> api.destroy() |> api.destroy(return_notifications?: true)
|> case do |> case do
:ok -> {:cont, :ok} :ok ->
{:error, reason} -> {:halt, {:error, reason}} {:cont, {:ok, notifications}}
{:ok, more_notifications} ->
{:cont, {:ok, Enum.concat(notifications, more_notifications)}}
{:error, reason} ->
{:halt, {:error, reason}}
end end
end) end)
end end