2022-09-23 13:03:33 +12:00
|
|
|
defmodule Ash.Test.Support.PolicySimple.Tweet do
|
|
|
|
@moduledoc false
|
|
|
|
use Ash.Resource,
|
|
|
|
data_layer: Ash.DataLayer.Ets,
|
|
|
|
authorizers: [Ash.Policy.Authorizer]
|
|
|
|
|
|
|
|
ets do
|
|
|
|
private?(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
actions do
|
|
|
|
defaults [:create, :read, :update, :destroy]
|
2023-02-23 14:12:08 +13:00
|
|
|
|
|
|
|
create :create_foo do
|
|
|
|
argument :foo, :string
|
|
|
|
end
|
2024-03-22 01:39:21 +13:00
|
|
|
|
|
|
|
create :create_bar do
|
|
|
|
argument :bar, :integer, allow_nil?: false
|
|
|
|
end
|
2022-09-23 13:03:33 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
attributes do
|
|
|
|
uuid_primary_key(:id)
|
|
|
|
end
|
|
|
|
|
|
|
|
policies do
|
|
|
|
bypass expr(^actor(:admin)) do
|
|
|
|
authorize_if always()
|
|
|
|
end
|
|
|
|
|
2023-02-23 14:12:08 +13:00
|
|
|
policy action_type([:read, :update, :destroy]) do
|
2022-09-28 07:58:48 +13:00
|
|
|
authorize_if(expr(user_id == ^actor(:id)))
|
2022-09-23 13:03:33 +12:00
|
|
|
end
|
2023-02-23 14:12:08 +13:00
|
|
|
|
|
|
|
policy action(:create) do
|
|
|
|
authorize_if relating_to_actor(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
policy action(:create_foo) do
|
2023-02-23 16:26:56 +13:00
|
|
|
authorize_if expr(is_foo(foo: arg(:foo)))
|
|
|
|
end
|
2024-03-22 01:39:21 +13:00
|
|
|
|
|
|
|
policy action(:create_bar) do
|
|
|
|
authorize_if matches("bar is big", &check_bar_is_big/2)
|
|
|
|
|
|
|
|
authorize_if matches("bar is even", fn _actor, context ->
|
|
|
|
rem(context.changeset.arguments.bar, 2) == 0
|
|
|
|
end)
|
|
|
|
end
|
2023-02-23 16:26:56 +13:00
|
|
|
end
|
|
|
|
|
|
|
|
calculations do
|
|
|
|
calculate :is_foo, :boolean, expr(^arg(:foo) == "foo") do
|
|
|
|
argument :foo, :string, allow_nil?: false
|
2023-02-23 14:12:08 +13:00
|
|
|
end
|
2022-09-23 13:03:33 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
relationships do
|
2023-02-23 14:12:08 +13:00
|
|
|
belongs_to :user, Ash.Test.Support.PolicySimple.User do
|
|
|
|
attribute_writable? true
|
|
|
|
end
|
2022-09-23 13:03:33 +12:00
|
|
|
end
|
2024-03-22 01:39:21 +13:00
|
|
|
|
|
|
|
defp check_bar_is_big(_actor, context) do
|
|
|
|
context.changeset.arguments.bar > 5
|
|
|
|
end
|
2022-09-23 13:03:33 +12:00
|
|
|
end
|