diff --git a/code/app/Helpers/Permissions.php b/code/app/Helpers/Permissions.php index 62204704..a37639e7 100644 --- a/code/app/Helpers/Permissions.php +++ b/code/app/Helpers/Permissions.php @@ -42,6 +42,8 @@ function allPermissions() */ try { $gas = currentAbsoluteGas(); + $gas = $gas->fresh(); + if ($gas->multigas) { $ret['App\Gas']['gas.multi'] = _i('Amministrare la modalità Multi-GAS su questa istanza'); } diff --git a/code/app/Providers/AuthServiceProvider.php b/code/app/Providers/AuthServiceProvider.php index 291fbe75..5474de8d 100644 --- a/code/app/Providers/AuthServiceProvider.php +++ b/code/app/Providers/AuthServiceProvider.php @@ -2,7 +2,6 @@ namespace App\Providers; -use Illuminate\Support\Facades\Gate; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use App\Extensions\BypassUserProvider; @@ -18,22 +17,7 @@ class AuthServiceProvider extends ServiceProvider */ public function boot() { - $all_permissions = allPermissions(); - foreach ($all_permissions as $rules) { - foreach (array_keys($rules) as $identifier) { - Gate::define($identifier, function ($user, $obj = null) use ($identifier) { - foreach($user->roles as $role) { - if ($role->enabledAction($identifier)) { - if(is_null($obj) || $role->applies($obj)) { - return true; - } - } - } - - return false; - }); - } - } + app()->make('RolesService')->registerPolicies(); Auth::provider('bypass', function ($app, array $config) { return new BypassUserProvider($app['hash'], $config['model']); diff --git a/code/app/Services/RolesService.php b/code/app/Services/RolesService.php index e9eb233c..b020058b 100644 --- a/code/app/Services/RolesService.php +++ b/code/app/Services/RolesService.php @@ -3,6 +3,7 @@ namespace App\Services; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Gate; use App\Exceptions\AuthException; @@ -46,6 +47,39 @@ public function destroy($id) return $role; } + /* + Funzione che istruisce il sistema interno di controllo autorizzazioni a + gestire i permessi personalizzati. Nella stragrande maggioranza dei casi + è sufficiente invocare questa funzione in AuthServiceProvider, ma viene + comunque messa qui affinché possa essere nuovamente invocata in casi + particolari (e.g. viene abilitata la modalità Multi-GAS, che prevede + l'esistenza di un permesso nuovo da applicare) + */ + public function registerPolicies() + { + $all_permissions = allPermissions(); + + foreach ($all_permissions as $rules) { + foreach (array_keys($rules) as $identifier) { + if (Gate::has($identifier)) { + continue; + } + + Gate::define($identifier, function ($user, $obj = null) use ($identifier) { + foreach($user->roles as $role) { + if ($role->enabledAction($identifier)) { + if(is_null($obj) || $role->applies($obj)) { + return true; + } + } + } + + return false; + }); + } + } + } + public function setMasterRole($gas, $identifier, $role_id) { $this->ensureAuth(['gas.permissions' => 'gas']); diff --git a/code/config/app.php b/code/config/app.php index ebf31e33..90274930 100644 --- a/code/config/app.php +++ b/code/config/app.php @@ -119,6 +119,7 @@ */ 'providers' => [ + App\Providers\ServicesProvider::class, /* * Laravel Framework Service Providers... @@ -154,7 +155,6 @@ App\Providers\EventServiceProvider::class, App\Providers\RouteServiceProvider::class, - App\Providers\ServicesProvider::class, App\Providers\SingletonsProvider::class, App\Providers\GraphicInitServiceProvider::class, App\Providers\MenuServiceProvider::class, diff --git a/code/tests/Services/MultiGasServiceTest.php b/code/tests/Services/MultiGasServiceTest.php index aa9f228d..68498943 100644 --- a/code/tests/Services/MultiGasServiceTest.php +++ b/code/tests/Services/MultiGasServiceTest.php @@ -20,6 +20,17 @@ public function setUp(): void { parent::setUp(); + /* + Solo quando la configurazione "multigas" è abilitata, vengono + effettivamente caricate le regole di controllo permessi relative. + Pertanto qui, dopo aver impostato tale configurazione, le regole + vengono ricaricate; altrimenti, tutte le funzioni successive + ritornano un "not authorized" non essendo in grado di riconoscere il + permesso "gas.multi" + */ + $this->gas->setConfig('multigas', '1'); + app()->make('RolesService')->registerPolicies(); + $this->userSuperAdmin = $this->createRoleAndUser($this->gas, 'users.admin,gas.multi,supplier.view', $this->gas); $this->userWithNoPerms = \App\User::factory()->create(['gas_id' => $this->gas->id]); }