how to handle if users dont have roles to panel with shield plugin ?
already gate for boot to appserviceprovider but if dont have roles cant login, why if im login with no roles can still access ?
class AppServiceProvider extends ServiceProvider
{
/
* Register any application services.
*/
public function register(): void
{
//
}
/
* Bootstrap any application services.
*/
public function boot(): void
{
Model::unguard();
Gate::before(function ($user, $ability) {
if ($user->hasRole('Super-Admin')) {
return true;
}
//return $user->hasRole('super_admin') ? true : null;
});
}
}
Solution:Jump to solution
i have 2 answer like this,
1. if you want to determine what roles for system you can $this->hasAnyRole(['super_admin', 'manager']); in canAccessPanel, and then if you login without roles you cant login to panel.
2. if you want simple no need to determine what roles for system you can just use HasPanelShield then no need canAccessPanel in models users. if you use HasPanelShield in models users then shield will create panel_user roles for access panel. and if no have roles panel_user cant login.
its my result im try before...
20 Replies
table users like this
If you don’t want them to login at all then you need to do that in the canAccessPqnel method on your user model. https://filamentphp.com/docs/3.x/panels/users#authorizing-access-to-the-panel
wait how to get data roles from access panel ?
$this->hasAnyRoles()
It’s covered in the spatie laravel-permission docs.
public function canAccessPanel(Panel $panel): bool
{
$this->hasAnyRole();
return true; } like this ?
return true; } like this ?
Return has any role.
If you just return true then they can always access the panel
wait but i cant login from my route show like this
read the doc i sent you
if you can't login now, it's because the user doesn't have any roles. that's how it works.
why cant login ? email have roles super_admin
i have 1 user tester with roles super_admin but why i cant login ? already return $this->hasAnyRole()
already input my password not wrong but can still login ?
i think $this->hasAnyRole() cant detect roles, i dont no how to works
you'll need to look at the docs https://spatie.be/index.php/docs/laravel-permission/v6/basic-usage/basic-usage to figure out what you need for your system to determine what roles a user has etc.
Basic Usage | laravel-permission
laravel-permission
return $this->hasAnyRole(['super_admin', 'manager']); yeah maybe like this if not declare roles cant login
i'm doing the same thing, but i also have to check for a specific role to, but it works for me.
yes im also works if like that, if just hasAnyRole() cant to getting specific role maybe ?
read the Spatie docs. there's numerous ways to work with roles and permissions.
Sorry but you lack really basics. You should learn them first...
You can give me suggestion how to setting canaccesspanel if users dont have roles cant login to panel ?
Solution
i have 2 answer like this,
1. if you want to determine what roles for system you can $this->hasAnyRole(['super_admin', 'manager']); in canAccessPanel, and then if you login without roles you cant login to panel.
2. if you want simple no need to determine what roles for system you can just use HasPanelShield then no need canAccessPanel in models users. if you use HasPanelShield in models users then shield will create panel_user roles for access panel. and if no have roles panel_user cant login.
its my result im try before
@awcodes thanks for help and give me answer