php - Laravel 政策返回未授权

标签 php laravel policies

我正在尝试在我的项目中实现政策。尽管严格遵守文件,但所有尝试都被证明是不成功的。并且还阅读了关于它和其他媒体的大量帖子。我按照 docs 中的描述做了,但它仍然不起作用。给了什么?

在 AuthServiceProvider 中:

<?php

namespace App\Providers;

use App\User;
use App\Job;
use App\Policies\JobPolicy;

use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        'App\Job' => 'App\Policies\JobPolicy',
        //Job::class => JobPolicy::class,
    ];

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

        //
    }
}

在政策中:

<?php

namespace App\Policies;

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

class JobPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any jobs.
     *
     * @param  \App\User  $user
     * @return mixed
     */
    public function viewAny(User $user,Job $job)
    {
        //return (($user->isAdmin() || $user->isModerator() || $user->isUser()) && $user->status==1);
        //return ($user->isMod());
        return true;
    }

在 Controller 中:

public function index()
    {
        $this->authorize('viewAny', User::class, Job::class);
        return view("jobs.index");
    }

我的用户模型:

<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Role;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',"role_id"
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function role(){
        return $this->belongsTo("App\Role", "role_id");
    }

    public function isMod()
    {
        $user = User::find(auth()->user()->id);
        $role = $user->role()->first()->name;

        if($role==="job board moderator"){
            return true;
        }
        else{
            return false;
        }

    }

}

和作业模型:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use App\User;

class Job extends Model
{
    protected $fillable = [
        "title", "description", "email"
    ];

    public function user(){
        return $this->belongsTo("App\User","user_id");
    }

}

最佳答案

在政策中:

public function viewAny(User $user)
{
  return true;
}

在 Controller 中:

public function index()
{
  $this->authorize('viewAny', Job::class);

  return view("jobs.index");
}

关于php - Laravel 政策返回未授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58765665/

相关文章:

Laravel Eloquent where 子句

php - 如何根据用户名获取角色名?

php - PHP 的 MySQL 语法错误

php - 在 Blade 中使用@each将附加变量传递给部分

php - 将新列添加到数据库表中,迁移失败并显示 - 无需迁移

php - Laravel 创建表和关系

azure - Microsoft Azure API 管理 - 缓存策略不起作用

iOS 获取设备用户所在国家

c++ - 具有政策的 Alexandrescu 单例

php - 从 symfony 4 中完全删除实体