I currently have two route groups where one route group has six routes and the other has two routes (that are also in the previous group).
/**
* Foo Routes for admin
*/
Route::group(['middleware' => 'bar:admin'], function () {
Route::put('foo/{uuid}/publish', 'FooController@publish');
Route::put('foo/{uuid}/disable', 'FooController@disable');
Route::put('foo/{uuid}/enable', 'FooController@enable');
Route::delete('foo/{uuid}', 'FooController@destroy');
Route::post('foo', 'FooController@store');
Route::put('foo/{uuid}', 'FooController@update');
});
/**
* Foo Routes for creator
*/
Route::group(['middleware' => 'bar:creator'], function () {
Route::post('foo', 'FooController@store');
Route::put('foo/{uuid}', 'FooController@update');
});
The reason for this split is because the creator needs access to two of the routes from the admin group, but admin needs permission to all the routes. Access is given via the middleware bar
.
However, whenever I am an admin
and I try to access one of the two routes available in the second route group, my bar
class denies its request. It says that I must be a creator
to access the route. Does this mean that routes have a cascading behaviour where the last instance of a route group is the one laravel uses? If it does, how can I format my routes to avoid this issue?
bar
code:
public function handle($request, \Closure $next, ...$permissionRules)
{
.
.
.
$userPermissions = $decodedToken['user']['permissions'];
// If the user does not have every permission defined via route parameters, deny.
foreach ($permissionRules as $permissions) {
if (!in_array($permissions, $userPermissions)) {
return $this->denyResponse();
}
}
// The user has every permission rule defined via route parameters, so allow.
return $next($request);
}