Returning success on failure as a security consideration

There are a few cases where I would like to pretend that an action was successful—sometimes regardless of the outcome, sometimes in specific cases. Is there a way to simulate a successful result currently without committing the changeset? Generally to control the response independent of the action result? Definitely related to https://discord.com/channels/711271361523351632/1089558275873325198
8 Replies
ZachDaniel
ZachDaniel3y ago
Something like this should do it
manual fn changeset, _ ->
Ash.Changeset.apply_changes(changeset)
end
manual fn changeset, _ ->
Ash.Changeset.apply_changes(changeset)
end
You can also do this:
change after_transaction(fn changeset, {:error, error} ->
{:ok, ...}

changeset, {:ok, result} ->
{:ok, result}
end)
change after_transaction(fn changeset, {:error, error} ->
{:ok, ...}

changeset, {:ok, result} ->
{:ok, result}
end)
\ ឵឵឵
\ ឵឵឵OP3y ago
Ah, that second one looks lovely. In case anybody finds this later, the first one is Changeset.apply_attributes The first one definitely makes sense as part of a manual change with control flow involved. Will it work for create actions?
ZachDaniel
ZachDaniel3y ago
Pretty much, yes But you'd have to do your logic to actually attempt the create and on failure return the apply_attributes logic
\ ឵឵឵
\ ឵឵឵OP3y ago
Right, but once I return a changeset with apply_attributes it should bail out there returning whatever was before?
ZachDaniel
ZachDaniel3y ago
🤔 not sure I undertand when you do manual you take over the entire action flow so wether it not it actually inserts is totally upto you (to do something like call a diferent create action on the resource for example) well, sorry, you don't take over the "entire action flow" you are replacing the "commit" step So returning Ash.Changeset.apply_changes/1 is basically saying "pretend like it happened"
\ ឵឵឵
\ ឵឵឵OP3y ago
Ok, but does the result of a manual action need to have the same shape as the resource is normally expecting, or can it be whatever you want? I guess it should still be a %App.Resource{} | [%App.Resource{}], depending on the action?
ZachDaniel
ZachDaniel3y ago
Yeah you have to return {:ok, %Resource{}} in that instance
\ ឵឵឵
\ ឵឵឵OP3y ago
Still covers a lot of cases that way for doing transformations etc.

Did you find this page helpful?