Skip to content

Attaching permission to role create the correct db records but not working in the routes #160

Description

@lucamarchiori

Firts of all i create roles and permissions:

$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

Then i set the routes middlewares:

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions