Skip to content

Commit

Permalink
docs: add example simple check
Browse files Browse the repository at this point in the history
  • Loading branch information
zachdaniel committed Feb 10, 2025
1 parent d447b80 commit 8d0701c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions lib/ash/policy/simple_check.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,33 @@ defmodule Ash.Policy.SimpleCheck do
A type of check that operates only on request context, never on the data
Define `c:match?/3`, which gets the actor, request context, and opts, and returns true or false
## Example
This is a simple check that checks if the user is changing anything other than the
provided list.
```elixir
defmodule ChangingNothingExcept do
use Ash.Policy.SimpleCheck
def match?(_actor, %{subject: %Ash.Changeset{} = changeset}, opts) do
allowed = opts[:attributes]
{:ok, Enum.all?(Map.keys(changeset.attributes), &(&1 in allowed))}
end
def match?(_, _, _), do: true
end
```
You could then use this like
```elixir
policy actor_attribute_equals(:role, :foobar) do
authorize_if {ChangingNothingExcept, attributes: [:foo, :bar]}
end
```
"""
@type actor :: Ash.Policy.Check.actor()
@type context :: Ash.Policy.Authorizer.t()
Expand Down

0 comments on commit 8d0701c

Please sign in to comment.