`change manage_relationship(...type: :remove)` not working
I have a
many_to_many relationship between Organization and SubscriptionPlan. The Organization has the following two actions:
The :subscribe action seems to work fine. But the :unsubscribe action fails with the following :invalid error:
I'm a little baffled by this one. Also, if I change the manage_relationship options from type: :remove to the following:
(this basically just changes on_no_match from :error to :ignore)
... my test also fails. It seems to remove the subscription from the Organization but not the SubscriptionPlan:
Not sure what I'm doing wrong here 🤔Solution:Jump to solution
So the mistake I was making was in my
accessing_through policy:
```elixir
policy accessing_from(MyApp.Accounts.Organization, :subscribers) do
authorize_if always()...14 Replies
Could it have to do with the relationship / policies?
So I assume it's that last
policy do forbid_if always() end that's causing my trouble here. If I set the user's role to :admin or use authorize?: false when I call unsubscribe, the test passes.
What I don't understand is how I can set a policy on the SubscriptionPlan that will allow deleting one of its OrgSubscription records
Is the following advisable?
I've added authorize?: false to the change manage_relationship call on the Organization resource. The Organization itself is protected by policies. So does that mean its ok for some of its internal manage_relationship call to use authorize?: false?Will give it a try 😄
Hmm. So
accessing_from gives me the same error I started with:
This is a many_to_many relationship - not sure I mentioned that beforedo you have other policies that apply?
All policies that apply have to pass
On the
SubscriptionPlan resource, which seems to be the one causing the trouble, these are my policies:
Then on the Organization resource, these are my policies:
The intermediate OrgSubscription resource doesn't have any policies
that forbids everyone from doing anything always
And always applies
Unless bypassed .... got it
Careful of overusing bypasses
I'm only using them for admin / super users
Oddly enough, removing the
forbid_if always() policy has now resulted in a Forbidden error on both subscribe and unsubscribe actions
I find policies generally to be slippery things
Got it!Solution
So the mistake I was making was in my
accessing_through policy:
I thought that second parameter was supposed to refer to the relationship defined on the resource that was defining the policy, but it's actually the relationship defined on the resource being referenced in the policy.So the corrected version is this:
Tests green ✅
Thank you!
🥳
haha
I didn't even see that
Easy to miss
Also the
forbid_if always() was also important