1

As it's write in doc, i used Laravel's Gate::before() method.

use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->registerPolicies();

        // Implicitly grant "Super Admin" role all permissions
        // This works in the app by using gate-related functions like auth()->user->can() and @can()
        Gate::before(function ($user, $ability) {
            return $user->hasRole('Super Admin') ? true : null;
        });
    }
}

But it doesn't work. I had the role in middleaware groupe in web.php and it's work. The role 'Super Admin' dont have any permission.

Route::group(['middleware' => ['auth:api','role:Super Admin|Provider']], function() {
  ...
}

In my ProviderController

class ProviderController extends Controller
{
    function __construct(){
       
        $this->middleware('permission:provider-list|provider-create|provider-edit|provider-delete', ['only' => ['index','show']]);
        $this->middleware('permission:provider-create', ['only' => ['create','store']]);
        $this->middleware('permission:provider-edit', ['only' => ['edit','update']]);
        $this->middleware('permission:provider-delete', ['only' => ['destroy']]);
        
    }

My question it's the right way or not ? I thank you in advance.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
Zekura
  • 317
  • 4
  • 13

2 Answers2

1

We use Spatie/laravel-permission often (docs). Your code for checking if a user has a role is correct. Spatie's hasRole function does not care for spaces or dashes. The function does however care for whether the role actually exists. Ensure you migrated the right tables, and actually inserted the role into your database.

\Spatie\Permission\Models\Role::create([
  'name' => 'Super Admin',
  'guard_name' => 'web',
]);

$user->assignRole(\Spatie\Permission\Models\Role::findByName('Super Admin'));
Yinci
  • 1,637
  • 1
  • 3
  • 14
  • I create the Super Admin's role ``` Role::find(1) => Spatie\Permission\Models\Role {#4168 id: "1", name: "Super Admin", guard_name: "web", created_at: "2020-12-08 10:00:29.000000", updated_at: "2020-12-08 10:00:29.000000", } ``` And my user have the role – Zekura Dec 09 '20 at 13:41
  • User::find(1)->roles – Zekura Dec 09 '20 at 13:48
  • Illuminate\Database\Eloquent\Collection {#4378 all: [ Spatie\Permission\Models\Role {#4130 id: "1", name: "Super Admin", guard_name: "web", created_at: "2020-12-08 10:00:29.000000", updated_at: "2020-12-08 10:00:29.000000", pivot: Illuminate\Database\Eloquent\Relations\MorphPivot {#3440 model_id: "1", role_id: "1", model_type: "App\Models\User", }, }, – Zekura Dec 09 '20 at 13:48
  • But, when i remove 'Super Admin' in Route::group(['middleware' => ['auth:api','role:Super Admin|Provider']], it's doesn't work. It's normal ? – Zekura Dec 09 '20 at 13:50
  • @Zekura The middleware should not have any interference with whether the user has a role or not. If you just go to a normal controller (e.g. to return a view), and you `dd($user->hasRole('Super Admin')`, does it return `false`? – Yinci Dec 10 '20 at 08:17
  • it's return true – Zekura Dec 10 '20 at 08:33
0

Your issue is likely that you only inserted the role for the web guard. You probably have it as the default guard, and that was the one inserted in your migration. The issue is that you are using the api guard to access that route. So you have to create the role for that guard too. I think they mention that in the Spatie documentation.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95