I’m trying to filter in Ash Policy :read.
How can I access the resource that the policy applies to from the rule?
Is there a way to access the contract in question when this rule runs?
E.g check if contract.client.email == actor.email.
defmodule MyApp.Policies.IsContractRecipient do
use Ash.Policy.FilterCheck
require Ash.Query
import Ash.Filter.TemplateHelpers, only: [actor: 1]
def filter(_options) do
# Only allow to read contracts where the actor is the recipient
# Eg. contract.client.email == actor(:email)
Ash.Query.expr(contract.client.email == ^actor(:email))
end
end
Filter checks are attached to the query, so in case of a read, it would be part of the where clause if used with AshPostgres. Therefore, you don’t need to explicitly tell Ash to load the data, it will do it itself.