F
Filament2mo ago
Sayy

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; }); } }
No description
No description
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...
Jump to solution
20 Replies
Sayy
Sayy2mo ago
table users like this
No description
awcodes
awcodes2mo ago
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
Sayy
Sayy2mo ago
wait how to get data roles from access panel ?
awcodes
awcodes2mo ago
$this->hasAnyRoles() It’s covered in the spatie laravel-permission docs.
Sayy
Sayy2mo ago
public function canAccessPanel(Panel $panel): bool { $this->hasAnyRole();
return true; } like this ?
awcodes
awcodes2mo ago
Return has any role. If you just return true then they can always access the panel
Sayy
Sayy2mo ago
wait but i cant login from my route show like this
No description
awcodes
awcodes2mo ago
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.
Sayy
Sayy2mo ago
why cant login ? email have roles super_admin
No description
Sayy
Sayy2mo ago
i have 1 user tester with roles super_admin but why i cant login ? already return $this->hasAnyRole()
No description
Sayy
Sayy2mo ago
already input my password not wrong but can still login ? i think $this->hasAnyRole() cant detect roles, i dont no how to works
awcodes
awcodes2mo ago
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.
Sayy
Sayy2mo ago
return $this->hasAnyRole(['super_admin', 'manager']); yeah maybe like this if not declare roles cant login
awcodes
awcodes2mo ago
i'm doing the same thing, but i also have to check for a specific role to, but it works for me.
Sayy
Sayy2mo ago
yes im also works if like that, if just hasAnyRole() cant to getting specific role maybe ?
awcodes
awcodes2mo ago
read the Spatie docs. there's numerous ways to work with roles and permissions.
krekas
krekas2mo ago
Sorry but you lack really basics. You should learn them first...
Sayy
Sayy2mo ago
You can give me suggestion how to setting canaccesspanel if users dont have roles cant login to panel ?
Solution
Sayy
Sayy2mo ago
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
Sayy
Sayy2mo ago
@awcodes thanks for help and give me answer