Reputation: 10641
I am trying to create custom authorization and authentication in Laravel 5.2.
So in my routes file, first i check if user is authenticated or not, using auth middleware.
Route::group(['middleware' => ['auth']], function () {
});
I have 3 users role.
1.visitor
2.manager
3.admin
and for manager and admin i have two middleware.
ManagerMiddleware (manager) and AminMiddleware (admin)
For a specific function i want to give access to both admin and manager.
For this purpose , i used this code
Route::group(['middleware' => ['auth']], function () {
Route::group(['middleware' => ['admin','manager']], function () {
Route::get('test','TestController@index')
});
});
Of course it does not work. Second middleware check both admin and manager and it fails because an user only belongs to a specific role.
So i want to use or in ['middleware' => ['admin','manager']]
Is there any way to do it in Laravel?
Upvotes: 3
Views: 1957
Reputation: 835
Why not just call the admin middleware:
Route::group(['middleware' => ['auth']], function () {
Route::group(['middleware' => ['admin']], function () {
Route::get('test','TestController@index')
});
});
If the user if registered as an Admin process to next request, if not, call the manager middleware :
class AdminMiddleware
{
public function handle($request, Closure $next)
{
if (isAdmin())
return $next($request);
//If not admin call to ManagerMiddleware
return app(ManagerMiddleware::class)->handle($request, function ($request) use ($next) {
if (isManager())
return $next($request);
//if neither admin nor manager reject request
return redirect()->home();
});
}
}
Or checking it the other way around, first manager then admin so that you can still use the admin priviledge independently. I don't know if that solution is convenient, that's what I would do so far.
Upvotes: 1