AE
Ash Elixir•3y ago
Blibs

Use Ash policies with simple functions

I was wondering if there is a way easy way to use Ash policies in places that you don't actually have an Ash resource. For example, I'm using FunWithFlags package for feature flags. I have a live view to handle the flags, and also a context module for it. Now I wan't to create some authorization code to this context module, and since I'm already using Ash for the rest of the system, I was wondering if there is a way to configure policies for a specific function.
1 Reply
ZachDaniel
ZachDaniel•3y ago
There is not a way currently, no. However well It depends 😆 If you can put your stuff behind a resource, then you can use policies 😆
defmodule MyApp.Flags do
use Ash.Resource,
authorizers: [Ash.Policy.Authorizer]

actions do
read :read do
prepare fn query, _ ->
Ash.DataLayer.Simple.set_data(query, [
%__MODULE__{name: "foo", value: "value"}
%__MODULE__{name: "bar", value: "value"}
%__MODULE__{name: "baz", value: "value"}
])
end
end
end

attributes do
# pkey is a necessity, but is a formality
attribute :name, :string, allow_nil?: true, primary_key?: true
attribute :value, :string, allow_nil?: true
end

policies do

end
end
defmodule MyApp.Flags do
use Ash.Resource,
authorizers: [Ash.Policy.Authorizer]

actions do
read :read do
prepare fn query, _ ->
Ash.DataLayer.Simple.set_data(query, [
%__MODULE__{name: "foo", value: "value"}
%__MODULE__{name: "bar", value: "value"}
%__MODULE__{name: "baz", value: "value"}
])
end
end
end

attributes do
# pkey is a necessity, but is a formality
attribute :name, :string, allow_nil?: true, primary_key?: true
attribute :value, :string, allow_nil?: true
end

policies do

end
end

Did you find this page helpful?