我创建了策略,并添加了方法视图:
public function view(User $user, Contact $contact)
{
return $user->id === $contact->manager;
}
然后我注册了它:
protected $policies = [
'App\Model' => 'App\Policies\ModelPolicy',
Contact::class => ContactPolicy::class,
];
然后我尝试将它与控制器助手一起使用:
public function view($id)
{
$contact = Contact::find($id);
$user = Auth::user();
$this->authorize('view', $contact);
return view('contact.edit')->with('contact', $contact);
}
和中间件:
Route::get('/contact/edit/{id}', 'EditContactController@view')->middleware('can:view,contact');
但我总是收到 403 错误。contact->manager 和 user->id 是一样的。此外,联系表方案:
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;