Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel用户策略始终拒绝授权_Laravel_Laravel 5_Laravel 5.6 - Fatal编程技术网

Laravel用户策略始终拒绝授权

Laravel用户策略始终拒绝授权,laravel,laravel-5,laravel-5.6,Laravel,Laravel 5,Laravel 5.6,我在Laravel中创建了一个UserPolicy,并试图在控制器中使用$this->authorize(User::class),但是,当我访问UserController@index,如果删除$this->authorize(用户::类),则不会出现此问题fromUserController app/Providers/AuthServiceProvider.php namespace App\Providers; use App\Transaction; use App\Policies

我在Laravel中创建了一个
UserPolicy
,并试图在控制器中使用
$this->authorize(User::class)
,但是,当我访问
UserController@index
,如果删除
$this->authorize(用户::类),则不会出现此问题
from
UserController

app/Providers/AuthServiceProvider.php
namespace App\Providers;

use App\Transaction;
use App\Policies\UserPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        User::class => UserPolicy::class
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //
    }
}
namespace App\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class UserPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can index users.
     *
     * @param  \App\User  $model
     * @return mixed
     */
    public function index(User $user)
    {
        return true;
    }
}
namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->authorize(User::class);
        $users = User::all();
        return view('users.index', compact('users'));
    }
}
app/Policies/UserPolicy.php
namespace App\Providers;

use App\Transaction;
use App\Policies\UserPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        User::class => UserPolicy::class
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //
    }
}
namespace App\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class UserPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can index users.
     *
     * @param  \App\User  $model
     * @return mixed
     */
    public function index(User $user)
    {
        return true;
    }
}
namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->authorize(User::class);
        $users = User::all();
        return view('users.index', compact('users'));
    }
}
app/Controllers/UserController.php
namespace App\Providers;

use App\Transaction;
use App\Policies\UserPolicy;
use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        User::class => UserPolicy::class
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        //
    }
}
namespace App\Policies;

use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class UserPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can index users.
     *
     * @param  \App\User  $model
     * @return mixed
     */
    public function index(User $user)
    {
        return true;
    }
}
namespace App\Http\Controllers;

use App\User;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->authorize(User::class);
        $users = User::all();
        return view('users.index', compact('users'));
    }
}

使用App\Userapp/Providers/AuthServiceProvider.php
中缺少code>,感谢您的帮助


在app/Providers/AuthServiceProvider.php中添加使用app\User


使用App\Userapp/Providers/AuthServiceProvider.php
中缺少code>,感谢您的帮助


在app/Providers/AuthServiceProvider.php中添加使用app\User


尝试传递用户实例而不是类名
$this->authorize(auth()->user())
?@linktoahef刚刚尝试传递,但用户在访问用户控制器的索引路由时登录时没有任何更改?尝试$this->authorize('index',user::class));在app/Providers/AuthServiceProvider.php中添加使用app\User;尝试传递用户实例而不是类名
$this->authorize(auth()->user())
?@linktoahef刚刚尝试传递,但用户在访问用户控制器的索引路由时登录时没有任何更改?尝试$this->authorize('index',user::class));在app/Providers/AuthServiceProvider.php中添加使用app\User;救生员!这件事困扰了我好长时间。救命恩人!这件事困扰了我好长时间。