I have two types of user roles. Admin and manager. An admin can to to all the routes, but a manager can only access some of them. Most of my routes are resource route. Currently I have this route group for admin:
Route::middleware(['auth', 'admin'])->prefix('admin')->group(function () {
Route::resource('post','PostController')
}
Here is my 'admin' middleware if you need to check:
if (!Auth::user()->isAdmin())
{
return redirect('/home');
}
return $next($request);
Here, all the routes are accessible by the admin. But I want to allow access some of the routes such as post.index, post.show, post.edit
for the manager.
What should I do now?
Below I am explaining my question elaborately with and example
I have three middleware, auth, manager, admin
. As the name suggests, auth
middleware checks if a user is authenticated, manager
middleware checks if the user is manager and certainly admin
middleware checks if the user is admin.
Now, for Route::resource('post','PostController')
route,
auth
has access topost.index, post.view
manager
has access topost.index, post.view, post.edit
admin
has access to all the routes. What is the best way to apply middleware to solve the problem?