在我的应用中,用户可以创建按相关管理器ID分类的事件。
我想检查提交创建新事件请求的用户是否可以访问他们正在为其创建事件的组织者。
例如:
$organiser_id = $request->input('organiser_id');
if($user->hasOrganiser($organiser_id)) {
// User has permission
}
显然上面的内容可以在我的控制器中工作,但理想情况下我想在我的EventPolicy类中或者在EventRequest中实现这一点。
在此先感谢您的帮助。
Laravel提供了许多方法来解决这个问题,您可以随时查看文档,在文档中您可以找到控制器中的检查(可以排除),模型和中间件。
检查authorizing-actions-using-policies
你可以随时使用middleware which handles the HTTP requests before hitting your app isntance,从而更好地控制你的应用程序。
Laravel包含一个中间件,可以在传入请求到达您的路由或控制器之前授权操作。默认情况下,Illuminate \ Auth \ Middleware \ Authorize中间件在App \ Http \ Kernel类中分配了can键。
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($request->age <= 200) {
return redirect('home');
}
return $next($request);
}
如链接所示,将其添加到route file,以便您查看请求并应用您的警卫。
Route::post('/post', function () {
// The current user may create posts...
})->middleware('can:create,App\Post');
您也可以在第一个链接中查看模型方式。
我正在旅行,但我认为这会对你有所帮助。
Laravel使用一个名为Auth的类,所以你可以调用那个类的staticky:Auth :: id()
更多信息:https://laravel.com/docs/5.6/authentication
希望这可以帮助。