I'm trying to find a solution in a use case where the admin user does not have the assigned role. In dashboard view it doesn't render the url for users, while if I directly access the dashboard/users, I get:
Spatie \ Permission \ Exceptions \ UnauthorizedException
user does not have the right roles
app/Http/Kernel.php
protected $routeMiddleware = [
....
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
];
routes/web.php
Route::group(
['middleware' => ['role:admin']],
function () {
Route::get('/dashboard/users', 'UsersController@index');
Route::get('/dashboard/users/{id}', 'UsersController@edit');
Route::patch('/dashboard/users/{id}', 'UsersController@update');
}
);
view/dashboard.blade.php
<div class="panel-body">
@hasrole('admin')
<li><a href="/dashboard/users">Manage Users</a></li>
@endhasrole
</div>
I have successfully generated the default roles & permission with
Commands/GenerateRoles.php
public function handle()
{
$this->info('Generating default roles and permissions...');
$admin = User::create(
[
'name' => 'administrator',
'email' => 'adm@mail.com',
'password' => bcrypt('12345'),
]
);
// Create roles.
$adminRole = Role::create(['name' => 'admin']);
$supportRole = Role::create(['name' => 'support']);
$admin->assignRole('admin');
// Create permissions.
$userManagement = Permission::create(['name' => 'users management']);
$deleteImages = Permission::create(['name' => 'delete images']);
$datasetStatus = Permission::create(
['name' => 'change dataset building status']
);
$adminRole->givePermissionTo($userManagement);
$deleteImages->syncRoles([$adminRole, $supportRole]);
$datasetStatus->syncRoles([$adminRole, $supportRole]);
}
What it could possibly goes wrong? Thanks for you time.