Skip to content

Commit

Permalink
fix in gestione dei permessi per unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Sep 15, 2024
1 parent ea2d3da commit 7d1d357
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 18 deletions.
2 changes: 2 additions & 0 deletions code/app/Helpers/Permissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
18 changes: 1 addition & 17 deletions code/app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;

use App\Extensions\BypassUserProvider;
Expand All @@ -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']);
Expand Down
34 changes: 34 additions & 0 deletions code/app/Services/RolesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;

use App\Exceptions\AuthException;

Expand Down Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion code/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
*/

'providers' => [
App\Providers\ServicesProvider::class,

/*
* Laravel Framework Service Providers...
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 11 additions & 0 deletions code/tests/Services/MultiGasServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down

0 comments on commit 7d1d357

Please sign in to comment.