I have this policy that I use to identify if the user is himself or not:
defmodule Core.Ash.Policies.Self do
@moduledoc false
use Ash.Policy.FilterCheck
@impl Ash.Policy.Check
def describe(opts) do
field = Keyword.get(opts, :field, :id)
"record #{field} matches actor id"
end
@impl Ash.Policy.FilterCheck
def filter(_actor, _context, opts) do
field = Keyword.get(opts, :field, :id)
expr(^ref(field) == ^actor(:id))
end
end
This works fine for read actions, but it will fail for create/update/destroy actions. To fix that I made these changes:
defmodule Core.Ash.Policies.Self do
@moduledoc false
use Ash.Policy.FilterCheck
defoverridable strict_check: 3
@impl Ash.Policy.Check
def describe(opts) do
field = Keyword.get(opts, :field, :id)
"record #{field} matches actor id"
end
@impl Ash.Policy.FilterCheck
def filter(_actor, _context, opts) do
field = Keyword.get(opts, :field, :id)
expr(^ref(field) == ^actor(:id))
end
@impl Ash.Policy.Check
def strict_check(actor, %{changeset: %Ash.Changeset{action_type: :create} = changeset}, opts) do
field = Keyword.get(opts, :field, :id)
{:ok, Ash.Changeset.get_attribute(changeset, field) == actor.id}
end
def strict_check(actor, %{changeset: %Ash.Changeset{} = changeset}, opts) do
field = Keyword.get(opts, :field, :id)
{:ok, Ash.Changeset.get_data(changeset, field) == actor.id}
end
def strict_check(actor, authorizer, opts) do
super(actor, authorizer, opts)
end
end
Now it seems to work fine, but I’m not sure if this is the best way to do that. Any thoughts?






















