简体   繁体   English

如何在Laravel 5.3中执行授权策略?

[英]How can I do Authorization Policies in Laravel 5.3?

I read here : https://laravel.com/docs/5.3/authorization#writing-policies 我在这里读到: https : //laravel.com/docs/5.3/authorization#writing-policies

And I tried to like this 我试图喜欢

My FavoritePolicy is like this : 我的FavoritePolicy是这样的:

<?php
namespace App\Policies;
use App\User;
use App\Models\Favorite;
use Illuminate\Auth\Access\HandlesAuthorization;
class FavoritePolicy
{
    use HandlesAuthorization;
    public function view(User $user, Favorite $favorite)
    {
        return $user->id === $favorite->user_id;
    }
}

My FavoriteController is like this : 我的FavoriteController是这样的:

<?php
use App\Models\Favorite;
...
class FavoriteController extends ApiController
{
    ...
    public function index(Favorite $favorite)
    {
        $this->authorize('view', $favorite);
        return view('profile.favorite');
    }
}

My AuthServiceProvider is like this : 我的AuthServiceProvider是这样的:

<?php
namespace App\Providers;
use App\Models\Favorite;
use App\Policies\FavoritePolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
    protected $policies = [
        'App\Model' => 'App\Policies\ModelPolicy',
        Favorite::class => FavoritePolicy::class,
    ];
    public function boot()
    {
        $this->registerPolicies();
    }
}

When I run my system to display favorite listing, there exist error like this : 当我运行系统以显示收藏夹列表时,存在如下错误:

Whoops, looks like something went wrong. 哎呀,看起来像出事了。

1/1 HttpException in Handler.php line 115: This action is unauthorized. Handler.php第115行中的1/1 HttpException:此操作是未授权的。

What the implementation of Authorization Policies is correct? 什么是授权策略实施正确?

I try dd($user) in view method(FavoritePolicy), the result displays user data is being logged. 我在视图方法(FavoritePolicy)中尝试dd($user) ,结果显示正在记录用户数据。 It's true 这是真的

But I try dd($favorite) , the result does not display favorite data of the user who is currently logged. 但是我尝试了dd($favorite) ,结果没有显示当前登录用户的收藏夹数据。 Whereas I check on the table, favorite data of the user who is currently logged is exist 我在表上检查时,存在当前登录用户的收藏夹数据

How can I solve this problem? 我怎么解决这个问题?

Update 更新资料

There result of dd($favorite) : 结果为dd($favorite)

Favorite {#498 ▼
  #fillable: array:3 [▶]
  #connection: null
  #table: null
  #primaryKey: "id"
  #keyType: "int"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: []
  #original: []
  #relations: []
  #hidden: []
  #visible: []
  #appends: []
  #guarded: array:1 [▼
    0 => "*"
  ]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  +exists: false
  +wasRecentlyCreated: false
}

Thanks for the additional info in your update! 感谢您提供更新中的其他信息!

So, you want to show the detail page of one specific Favorite entity, but only if the user is the owner of this entity? 因此,您只想显示一个特定Favorite实体的详细信息页面,但前提是用户是该实体的所有者?

Firstly a minor "issue": usually in Laravel the controller method that shows details of a specific entity is called show , not index . 首先是一个小的“问题”:通常在Laravel中,显示特定实体详细信息的控制器方法称为show ,而不是index index is the name for methods that show a list of entities (in your example: a list of favorites). index是显示实体列表(在您的示例中:收藏夹列表)的方法的名称。

Regarding your problem: 关于您的问题:
Your policy checks if the currently logged in user can view an empty $favorite (see the dd($favorite) output in your post update). 您的政策会检查当前登录的用户是否可以查看空的$favorite (请参见更新后的dd($favorite)输出)。 Which means, that $favorite is also not set in your index method. 这意味着,您的index方法中也未设置$favorite

I guess, you have a route defined similar to this: 我想,您定义的路由与此类似:

Route::get('favorite/{id}', 'FavoriteController@index');

This means, that the value of id is injected into your index method as a parameter, but not a Favorite entity. 这意味着, id的值作为参数而不是Favorite实体注入到index方法中。 To query for the Favorite entity is something you need to do in the method. 要查询“ Favorite实体,您需要在方法中进行操作。

So, your method should more look like this: 因此,您的方法应如下所示:

public function index($favoriteId)
{
    $favorite = Favorite::findOrFail($favoriteId);
    $this->authorize('view', $favorite);
    return view('profile.favorite')->with('favorite', $favorite);
}

Hope that helps! 希望有帮助! If not, please add a comment! 如果没有,请添加评论!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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