joshgallagher24
1 year ago

Policy Issue

Posted 1 year ago by joshgallagher24

I have an EventPolicy which defines a create method. This create method only allows an event to be created if the User has provided a contact number.

Event Policy 'create':

public function create(User $user)
{
    return $user->contact_number;
}

Now in my controller I do this:

$this->authorize('create', auth()->user());

And my AuthServiceProvider:

'App\Models\Event' => 'App\Policies\EventPolicy',

Now, this should work right? Well, it doesn't. It gives me a 403 - doesn't even reach the Policy?

So the work around is this:

'App\Models\User' => 'App\Policies\EventPolicy',

Giving the User the EventPolicy - this now works.

However, this does not seem right? Do I have to specify the User has the Event policy? Shouldn't it just work when I pass auth()->user() which returns a User instance?

Or, is this right and I've lost my mind?

Please sign in or create an account to participate in this conversation.