Filter check is returning an error tuple, rather than filtering rows.
I'm trying to use policies to filter out rows whose
visibility
attribute is not 1. The table contains some rows where visibility == 2, so I expected the query will filter those out.
But Task |> Ash.Query.for_read(:read) |> Tasks.read()
returns an error tuple like this. What am I missing?
7 Replies
ah
so in that case, its because of the way the policy is worded
its not possible for that policy to pass
Policies have to have a check inside of them that explicitly authorizes the policy, otherwise we assume it to fail
If you were to do that, your policy would filter the way that you want it to
Any policy that only contains (one or more)
forbid_if
statements will always fail with a forbidden error.Ahhh.... makes perfect sense 🙂 Thank you.
I have been doing these filtering through making calculations (such as
calculate :invisible
, calculate: :is_author, etc) and then mixing and matching them in read actions. It appears that using policies can replace them in many cases, but it feels that having calculations will provide more flexibility because it can be used elsewhere (e.g., to show visibility of a task, rather than filtering invisible ones out altogether). Is there a useful rule of thumb in terms of where to define filters, for instance, policies vs. calculation+action?There isn't really :/
It typically depends on what you're doing
BTW, you can do expressions inline if you want
But I can see why you might want to put them in a module to centralize the logic/make it clear
Yeah, I read it from the doc.
Invisible was a simplified example. The real one is more complex like visible if admin, or author, or autorized by author, etc. It becomes pretty complex pretty quickly.
Then that is also used for routing as well. For instance, if an unauthorized user tries to access a url that requires authorization, the condition defined as calculations is used to determine it is an unauthorized access and route them to the appropriate page.
router
<live_user_auth.ex>
Let me know if there is an easier, more recommended Ash way to do this.
You can try using
YourApi.can?
If there aren't good examples in the docs we should add them, but there are likely examples in the forums hereWill do. Thanks!