Hello!!
I have a UserEmotion
Resource (it’s a join between User
resource and Emotion
resource).
In my User
resource, I allow only users with role
== :admin
to access the :read
action:
policy action(:read) do
authorize_if actor_attribute_equals(:role, :admin)
end
The UserEmotion
exposes a create function named add_user_emotion
which is quite basic:
create :add_user_emotion do
description "Adds a new entry to user_emotions."
argument :user, :uuid do
allow_nil? false
end
argument :emotion, :uuid do
allow_nil? false
end
change manage_relationship(:user, type: :append)
change manage_relationship(:emotion, type: :append)
end
I want this function to be available for all User
s, including those who are not :admin
(maybe I’ll populate the :user
myself from the actor
to ensure a User
only adds Emotion
s for itself).
To fix the permission issue, I could:
- create a
:list
function inUser
resource, and I expose this function instead of:read
in the API, so I put restricting policies on:list
and I open all permissions on:read
→ it doesn’t feel the right way to me, since there’s a breach if I later expose the:read
function by mistake
OR - create an intermediary function before the
:add_user_emotion
that will set an actor likeactor: %{internal: true}
, and then authorize this internal new actor in the policy like so:
policy action(:read) do
authorize_if actor_attribute_equals(:role, :admin)
authorize_if actor_attribute_equals(:internal, :true) // NEW ACTOR
end
None of my solutions seems good to me. Do you have any advice on this case? I guess it’s a common challenge, but I haven’t found a solution while checking at the docs (but I found many responses to other questions I had ).
P.S. I’ve also tried the accessing_from
builtin policy, but It doesn’t work since my UserEmotion Resource is not yet created but on the path to be created…
policy action(:read) do
authorize_if actor_attribute_equals(:role, :admin)
authorize_if accessing_from(UserEmotion, :emotions)
end