0

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...?

roberttolton
  • 111
  • 7
  • What value do you have for `user_model_fqn` in `config/backpack/base.php`? `backpack_user()` should use that value. I wonder if that is set to the BackpackUser class and doesn't have the `HasRoles` trait applied. – Wesley Smith May 05 '19 at 04:44
  • @DelightedD0D the `BackpackUser` class was indeed missing the `HasRoles` trait, however adding this didn't resolve the issue - `getRoleNames` still returns empty and the routes are forbidden for me by the middleware. – roberttolton May 07 '19 at 14:43
  • Also forgot to mention that `BackpackUser` extends `User` which did already have the trait `HasRoles` on it. – roberttolton May 07 '19 at 14:48

0 Answers0