AuthServiceProvider.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Providers;
  3. use App\Authority;
  4. use App\User;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Gate;
  7. use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
  8. use Illuminate\Support\Facades\Schema;
  9. class AuthServiceProvider extends ServiceProvider
  10. {
  11. /**
  12. * The policy mappings for the application.
  13. *
  14. * @var array
  15. */
  16. protected $policies = [
  17. // 'App\Model' => 'App\Policies\ModelPolicy',
  18. ];
  19. /**
  20. * Register any authentication / authorization services.
  21. *
  22. * @return void
  23. */
  24. public function boot()
  25. {
  26. $this->registerPolicies();
  27. if(!Schema::hasTable('users')){return;}
  28. Gate::before(function ($user) {
  29. if ($user->isSuperAdmin()) {
  30. return true;
  31. }
  32. });
  33. if(!Schema::hasTable('authorities')){return;}
  34. $authorities = Authority::with('roles')->get();
  35. foreach($authorities as $authority) {
  36. Gate::define($authority->name, function($user) use ($authority) {
  37. return $user->hasRole($authority->roles);
  38. });
  39. }
  40. }
  41. }