Laravel ACL | Fórum | School of Net

Deseja poder participar de nosso fórum e tirar todas as suas dúvidas?
Clique aqui e assine nosso plano de acesso ilimitado. Saiba mais.

por Hugo

3 semanas, 6 dias atrás Hugo

Laravel ACL

Olá, gostaria de saber como eu posso pegar o user logado no AuthServiceProvider, atualmente a gestão de criação dos Gates tá fazendo sempre a mesma consulta para cada definição de gate. com isso se tivesse alguma maneira de obter o usuário na classe AuthServiceProvider acredito que resolveria o problema. Detalhes: Método hasPermission sempre vai buscar as permissões do usuário no foreach de definição dos gates (definePermissionsGates) ``` <?php namespace App\Providers; use App\Models\Permission; use App\Models\User; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Support\Facades\Gate; class AuthServiceProvider extends ServiceProvider { /** * The policy mappings for the application. * * @var array */ protected $policies = [ // 'App\Model' => 'App\Policies\ModelPolicy', ]; /** * Register any authentication / authorization services. * * @return void */ public function boot() { $this->registerPolicies(); $this->defineGates(); } public function defineGates() { $this->defineAdminGate(); $this->definePermissionsGates(); } public function defineAdminGate() { Gate::before(function($user){ if($user->isAdmin()){ return true; } }); } public function definePermissionsGates() { $permissions = Permission::all(); foreach($permissions as $permission) { Gate::define($permission->name, function(User $user) use ($permission) { return $user->hasPermission($permission->name); }); } } public function defineExempleGate() { // Exemplo de Gate que verifica se o usuario é dono Gate::define('owner', function(User $user, $object) { return $user->id === $object->user_id; }); // Usar no controller //Gate::denies('owner', $product); } } <?php namespace App\Models\Traits; use App\Models\Tenant; trait UserACLTrait { public function getUserPermissions() { $getPlanPermissions = $this->getPlanPermissions(); $getRolePermissions = $this->getRolePermissions(); return array_unique(array_intersect($getPlanPermissions, $getRolePermissions)); } public function getPlanPermissions(): array { $tenant = Tenant::with(['plan.modules.permissions'])->where('id', $this->tenant_id)->first(); $plan = $tenant->plan; $permissions = []; foreach($plan->modules as $module) { foreach($module->permissions as $permission) { array_push($permissions, $permission->name); } } return $permissions; } public function getRolePermissions() { $roles = $this->roles()->with(['permissions'])->get(); $permissions = []; foreach($roles as $role) { foreach($role->permissions as $permission) { array_push($permissions, $permission->name); } } return $permissions; } public function hasPermission(string $permission): bool { return in_array($permission, $this->getUserPermissions()); } public function isAdmin(): bool { return in_array($this->email, config('acl.admins')); } public function isTenant(): bool { return !in_array($this->email, config('acl.admins')); } } ```

7 Respostas