简体   繁体   English

Laravel 政策总是返回 false

[英]Laravel policies always return false

I have created policy, and add method view:我已经创建了策略,并添加了方法视图:

public function view(User $user, Contact $contact)
{
    return $user->id === $contact->manager;
} 

Then I have registered it:然后我注册了它:

protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy',
    Contact::class => ContactPolicy::class,
]; 

And then I have tried to use it with controller helper:然后我尝试将它与控制器助手一起使用:

public function view($id)
{
    $contact = Contact::find($id);
    $user = Auth::user();

    $this->authorize('view', $contact);

    return view('contact.edit')->with('contact', $contact);
}

And middleware:和中间件:

Route::get('/contact/edit/{id}', 'EditContactController@view')->middleware('can:view,contact');

But I always get 403 error.但我总是收到 403 错误。 contact->manager and user->id are the same. contact->manager 和 user->id 是一样的。 Also, Contact table scheme:此外,联系表方案:

CREATE TABLE `contacts` (
  `id` int(11) NOT NULL,
  `first_name` varchar(25) NOT NULL,
  `last_name` varchar(25) NOT NULL,
  `email` varchar(35) NOT NULL,
  `home_phone` int(10) DEFAULT NULL,
  `work_phone` int(10) DEFAULT NULL,
  `cell_phone` int(10) DEFAULT NULL,
  `best_phone` enum('home_phone','work_phone','cell_phone') NOT NULL,
  `address_1` varchar(100) DEFAULT NULL,
  `address_2` varchar(100) DEFAULT NULL,
  `city` varchar(35) DEFAULT NULL,
  `state` varchar(35) DEFAULT NULL,
  `zip` int(6) DEFAULT NULL,
  `country` varchar(35) DEFAULT NULL,
  `birth_date` date DEFAULT NULL,
  `manager` int(11) UNSIGNED NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

I know this is an old question, but if anyone else runs across this, make sure you look at your controller to determine if the model is being declared in the function call我知道这是一个老问题,但是如果其他人遇到了这个问题,请确保您查看您的控制器以确定模型是否在函数调用中声明

in the original poster's code, it should be在原始海报的代码中,它应该是

EditController.php编辑控制器.php

public function view(Contact $contact)

and the web.php和 web.php

Route::get('/contact/edit/{contact}', 'EditContactController@view')->middleware('can:view,contact');

so that the dependency injection can work properly.以便依赖注入可以正常工作。

I`ve just replaced我刚换了

protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy',
    Contact::class => ContactPolicy::class,
]; 

with

protected $policies = [
    'App\Model' => 'App\Policies\ModelPolicy',
    'App\Contact' => 'App\Policies\ContactPolicy',
];

and now it works with $this->authorize('view', $contact);现在它适用于$this->authorize('view', $contact); , but middleware still return 403 ,但中间件仍然返回 403

Kindly check your route link to that method if it has your middleware if not you can set it like.如果它有你的中间件,请检查你的路由链接到该方法,如果没有你可以设置它。

Route::get('/view', CONTROLLER@view)->middleware('YOUR-MIDDLEWARE'); Route::get('/view', CONTROLLER@view)->middleware('YOUR-MIDDLEWARE');

Example:例子:

Route::get('/view', UserController@view)->middleware('auth:user'); Route::get('/view', UserController@view)->middleware('auth:user');

You can check the pluralizations if you translate the model in your lang.如果您用语言翻译模型,则可以检查复数形式。

Let me explain:让我解释:

Example: Model Pagamento示例:模型Pagamento

Url slugs: pagamenti网址 slug: pagamenti

In my case using the check inside the functions works fine, but not works using constructor:在我的情况下,使用函数内部的检查工作正常,但使用构造函数不起作用:

 public function edit(Pagamento $pagamenti)
  {

   $this->authorize('update', $pagamenti);
   //..

I changed:我变了:

public function __construct()
{

    //Abilita su tutto il resource
    //$this->authorizeResource(Pagamento::class,'pagamento'); // Not Works
    $this->authorizeResource(Pagamento::class,'pagamenti'); // Works

}

Now works fine ;)现在工作正常;)

Hope can help ;)希望能帮上忙;)

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

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