When I use Gate::before Defining a Super-Admin, it does not work. Why? #2470
Answered
by
erikn69
carveybunt
asked this question in
Q&A
-
I got a error following: "message": "User does not have any of the necessary access rights.",
"exception": "Spatie\\Permission\\Exceptions\\UnauthorizedException",
"file": "/var/www/xuehai-api-new/vendor/spatie/laravel-permission/src/Exceptions/UnauthorizedException.php",
"line": 49, AuthServiceProvider file following: use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use Laravel\Passport\Passport;
class AuthServiceProvider extends ServiceProvider
{
/**
* The model to policy mappings for the application.
*
* @var array<class-string, class-string>
*/
protected $policies = [
//
];
/**
* Register any authentication / authorization services.
*/
public function boot(): void
{
Gate::before(function ($user, $ability) {
return $user->hasRole('super admin',back_users') ? true : null;
});
// passport
Passport::tokensExpireIn(now()->addDays(15)); // token有效期 addMinutes addHours
Passport::refreshTokensExpireIn(now()->addDays(30)); // 刷新token 的有效期
Passport::personalAccessTokensExpireIn(now()->addMonths(6));
}
} My route following: Route::get("/sidebar/index", "index")->middleware(['role_or_permission:edit,back_users']);
protected $middlewareAliases = [
'role' => \Spatie\Permission\Middlewares\RoleMiddleware::class,
'permission' => \Spatie\Permission\Middlewares\PermissionMiddleware::class,
'role_or_permission' => \Spatie\Permission\Middlewares\RoleOrPermissionMiddleware::class,
]; Could someone help me, please? |
Beta Was this translation helpful? Give feedback.
Answered by
erikn69
Jul 31, 2023
Replies: 2 comments
-
|
Beta Was this translation helpful? Give feedback.
0 replies
-
Must be an array return $user->hasRole(['super admin', 'back_users']) ? true : null; Or return $user->hasAnyRole('super admin', 'back_users') ? true : null; laravel-permission/src/Traits/HasRoles.php Line 243 in 39cd78b |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
drbyte
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Must be an array
Or
hasAnyRole
laravel-permission/src/Traits/HasRoles.php
Line 243 in 39cd78b