简体   繁体   中英

Laravel Policies - $this->authorize not working

Task policy:

class TaskPolicy
{
    use HandlesAuthorization;

    public function canSeeTeam()
    {
        return true;
    }
}

AuthServiceProvider:

class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        'App\Models\Task' => 'App\Policies\TaskPolicy',
    ];

Task controller:

public function update(Request $request, Task $task)
    {      
        $this->authorize('canSeeTeam');
        dd('Authorized!');
    }

Instead of getting Authorized! I get:

"message": "This action is unauthorized.", "exception": "Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException",

I'm logged in, and have access to the team, not it matters because canSeeTeam always true .

You also have to pass the task object to the authorize method:

$this->authorize('canSeeTeam', $task);

Please send me error message or replace this:

 public function canSeeTeam()
    {
        return true; => return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM