PHP 7.3 Laravel 5.8 Laravel Backpack 3.6
I am trying to use the middlware 'role:admin'
within my routes/backpack/permissionmanager.php
file, to restrict access to the User, Roles and Permissions areas of Backpack to a subset of users with certain roles.
I have made sure that my User account has been granted the correct role.
My 'user'
model in config/backpack/permissionmanager.php
is set to App\User::class
and my User
model has and uses the necessary traits as outlined in the documentation.
I have placed a role
Middleware into my app, as follows:
<?php
namespace App\Http\Middleware;
use Closure;
class RoleMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $role)
{
if (backpack_auth()->guest()) {
return redirect('login');
}
if (! backpack_user()->hasRole($role)) {
abort(403);
}
return $next($request);
}
}
However, it seems that this middleware's backpack_user()
, while knowing who I am through the correct return of the ->name
property, has absolutely no idea of the roles or permissions that I am supposed to have assigned to myself. I have checked this using the ->getRoleNames()
method and it returns an empty collection.
Within the database, the correct entries and IDs are set within the model_has_roles
table for my User account and the Role I want.
However, navigating to myapp.dev/admin/user
results in a 403 Forbidden
.
I think this might be a bug, or something I must not be seeing correctly...?