Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
lucamarchiori opened this issue Dec 6, 2021 · 0 comments

Comments

@lucamarchiori
Copy link

lucamarchiori commented Dec 6, 2021

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 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant