$role = Defender::createRole('superadmin');
$roleSuperadmin = Defender::findRole('superadmin');
$permission = Defender::createPermission('user.create', 'Create Users');
$roleSuperadmin->attachPermission($permission);
//... And so on with all the roles and permissions
Route::group(['prefix' => 'admin', 'as' => 'admin.'], function () {
Route::group(['prefix' => 'user', 'as' => 'user.'], function () {
Route::group(['middleware' => ['auth']], function () {
Route::get('/index', [UserController::class, 'index'])->name('index')->middleware(['auth', 'needsPermission:user.index']);
Route::get('/create', [UserController::class, 'create'])->name('create')->middleware(['auth', 'needsPermission: user.create']);
Route::post('/store', [UserController::class, 'store'])->name('store')->middleware(['auth', 'needsPermission: user.store']);
Route::get('/destroy/{id}', [UserController::class, 'destroy'])->name('destroy')->middleware(['auth', 'needsPermission: user.destroy']);
Route::get('/edit/{id}', [UserController::class, 'edit'])->name('edit')->middleware(['auth', 'needsPermission: user.edit']);
Route::post('/update/{id}', [UserController::class, 'update'])->name('update')->middleware(['auth', 'needsPermission: user.update']);
});
});
I check the database and all the records about the roles and the permissions seems to be created in the right way.
When i go to one of the routes i get:
Artesaos\Defender\Exceptions\ForbiddenException
You don't have permission to access this resource
Firts of all i create roles and permissions:
Then i set the routes middlewares:
I check the database and all the records about the roles and the permissions seems to be created in the right way.
When i go to one of the routes i get: