Policy not enforced
Hi, I created a "LocationPolicy" that looks like this:
and registered it like so:
But this policy doesn't seem to be enforced when using a select multiple:
Currently the user can still choose all locations, where it should only be those relateded with the user model.
What am I missing here?
Thank you!
18 Replies
You need to register the policy on your Location class, not User class
okay, I did that now but it doesn't seem to work either.
That correct now. I would assume the select would honor that policy now. Unfortunately, I’m walking out the door to work. Hopefully someone else can step in for the meantime. I’ll check back in an few hours to see if this is resolved.
if I dd() the view policy or return null nothing happens in the frontend, so I assume that this policy never gets called
dd() $user in view method in your policy. If it's registered correctly and being called correctly, then that should dump the user.
I'll be back
dd($user) doesn't dump anything :/
A Select doesn't rely on policies. Neither does ListPage. It's because policies work on the model, but we already need to filter on DB level
so for this to work, I need to make a custom query for the select which respects the users locations?
Yes.
the only thing that comes to my mind by doing that is, that you might be able to "hack" it by just passing other ids to the select field and save. Or is this not possible? 😅
Add validation
The relationship method can take a query argument at the end so try something like:
->relationship('locations', 'name', fn (Builder $query) => $query->where('user_id', auth()->id())
thank you, I did that, can you give me more context for that rule?
Not a tule. On your relationship method
yes I did that and it works. Just wonder how this ->rule() should look like that @pboivin mentioned
Ok cool!
Just a sec, I'll find a more complete example (I know I have one, lol)
@prodex I think this should work (had to change it a bit to simplify):
Oh I forgot about the
->relationship()
for a bit... I guess my solution is a bit more "manual", there may be a better way
Yeah nevermind, it looks like the Select field is doing all that for you with ->relationship()
😄okay 😄