Add policies for Author resource.
This commit is contained in:
@@ -3,7 +3,8 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
otp_app: :decentralised_book_index,
|
||||
domain: DecentralisedBookIndex.Metadata,
|
||||
data_layer: AshPostgres.DataLayer,
|
||||
extensions: [AshJsonApi.Resource]
|
||||
extensions: [AshJsonApi.Resource],
|
||||
authorizers: [Ash.Policy.Authorizer]
|
||||
|
||||
require Ash.Query
|
||||
alias DecentralisedBookIndex.Metadata
|
||||
@@ -24,11 +25,14 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
primary? true
|
||||
accept [:name, :description, :avatar_url, :author_alias_registry_id]
|
||||
|
||||
change fn changeset, _ ->
|
||||
change fn changeset, context ->
|
||||
actor = Map.get(context, :actor, nil)
|
||||
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_author_alias_registry()
|
||||
{:ok, registry} =
|
||||
DecentralisedBookIndex.Metadata.create_author_alias_registry(actor: actor)
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, registry.id)
|
||||
else
|
||||
@@ -74,11 +78,13 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
:dbi_server_id
|
||||
]
|
||||
|
||||
change fn changeset, _ ->
|
||||
change fn changeset, context ->
|
||||
actor = Map.get(context, :actor, nil)
|
||||
registry_id = Ash.Changeset.get_attribute(changeset, :author_alias_registry_id)
|
||||
|
||||
if registry_id == nil do
|
||||
{:ok, registry} = DecentralisedBookIndex.Metadata.create_author_alias_registry()
|
||||
{:ok, registry} =
|
||||
DecentralisedBookIndex.Metadata.create_author_alias_registry(actor: actor)
|
||||
|
||||
Ash.Changeset.force_change_attribute(changeset, :author_alias_registry_id, registry.id)
|
||||
else
|
||||
@@ -135,6 +141,7 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
end
|
||||
|
||||
update :sync do
|
||||
description "Sync the data for an existing record."
|
||||
require_atomic? false
|
||||
|
||||
accept [
|
||||
@@ -171,6 +178,32 @@ defmodule DecentralisedBookIndex.Metadata.Author do
|
||||
end
|
||||
end
|
||||
|
||||
policies do
|
||||
bypass actor_attribute_equals(:role, :admin) do
|
||||
authorize_if always()
|
||||
end
|
||||
|
||||
policy action_type(:read) do
|
||||
authorize_if always()
|
||||
end
|
||||
|
||||
policy action(:get_author_ids) do
|
||||
authorize_if always()
|
||||
end
|
||||
|
||||
policy action_type(:create) do
|
||||
authorize_if actor_attribute_equals(:role, :moderator)
|
||||
end
|
||||
|
||||
policy action_type(:update) do
|
||||
authorize_if actor_attribute_equals(:role, :moderator)
|
||||
end
|
||||
|
||||
policy action_type(:destroy) do
|
||||
authorize_if actor_attribute_equals(:role, :admin)
|
||||
end
|
||||
end
|
||||
|
||||
attributes do
|
||||
uuid_primary_key :id, writable?: true
|
||||
|
||||
|
||||
Reference in New Issue
Block a user