| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- <?php
- namespace App\Providers;
- use App\Authority;
- use App\User;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Gate;
- use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
- use Illuminate\Support\Facades\Schema;
- 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();
- if(!Schema::hasTable('users')){return;}
- Gate::before(function ($user) {
- if ($user->isSuperAdmin()) {
- Cache::put('isSuperAdmin', true);
- }else{
- Cache::put('isSuperAdmin', false);
- }
- });
- if(!Schema::hasTable('authorities')){return;}
- $authorities = Authority::with('roles')->get();
- foreach($authorities as $authority) {
- Gate::define($authority->name, function($user) use ($authority) {
- if(Cache::get('isSuperAdmin')
- && $authority['permission']=='允许'){
- return true;
- }
- return $user->hasRole($authority->roles);
- });
- }
- }
- }
|