Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why wouldn't the Policy ability method get called?

I have used the generator to generate a simple policy:

php artisan make:policy TeamPolicy

And, I have registered it in AuthServiceProvider as:

 protected $policies = [
        Team::class => TeamPolicy::class,
    ];

I tried to call it in the TeamsController as:

$this->authorize('update', $team);

Here is my policy file Policies\TeamPolicy.php as:

<?php
namespace App\Policies;
use App\Team;
use Illuminate\Auth\Access\HandlesAuthorization;
class TeamPolicy
{
    use HandlesAuthorization;
    /**
     * Create a new policy instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }
    public function update( Team $team)
    {
        App:debug("Policy update called!");
        return true;
    }
}

However, the update method in the policy is never called, and I get error 403 when calling $this->authorize('update', $team);

Please advise!

like image 669
WingsOfAltair Avatar asked Jan 25 '17 11:01

WingsOfAltair


1 Answers

The first argument of the Policy methods should be the user to check authorization on. Try instead:

public function update(User $user, Team $team)
{
    //...
}
like image 179
alepeino Avatar answered Nov 02 '22 03:11

alepeino