AuthServiceProvider.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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\Cache;
  7. use Illuminate\Support\Facades\Gate;
  8. use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
  9. use Illuminate\Support\Facades\Schema;
  10. class AuthServiceProvider extends ServiceProvider
  11. {
  12. /**
  13. * The policy mappings for the application.
  14. *
  15. * @var array
  16. */
  17. protected $policies = [
  18. // 'App\Model' => 'App\Policies\ModelPolicy',
  19. ];
  20. /**
  21. * Register any authentication / authorization services.
  22. *
  23. * @return void
  24. */
  25. public function boot()
  26. {
  27. $this->registerPolicies();
  28. if(!Schema::hasTable('users')){return;}
  29. Gate::before(function ($user) {
  30. if ($user->isSuperAdmin()) {
  31. Cache::put('isSuperAdmin', true);
  32. }else{
  33. Cache::put('isSuperAdmin', false);
  34. }
  35. });
  36. if(!Schema::hasTable('authorities')){return;}
  37. $authorities = Authority::with('roles')->get();
  38. foreach($authorities as $authority) {
  39. Gate::define($authority->name, function($user) use ($authority) {
  40. if(Cache::get('isSuperAdmin')
  41. && $authority['permission']=='允许'){
  42. return true;
  43. }
  44. return $user->hasRole($authority->roles);
  45. });
  46. }
  47. }
  48. }