feat(cloud): RBAC permission matrix
This commit is contained in:
parent
76ea1b4ecf
commit
9983bc8fe3
2 changed files with 43 additions and 0 deletions
15
cloud/src/Auth/Rbac.php
Normal file
15
cloud/src/Auth/Rbac.php
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace App\Auth;
|
||||
|
||||
final class Rbac
|
||||
{
|
||||
private const MATRIX = [
|
||||
'admin' => ['view_events','acknowledge','edit_zones','manage_thresholds','manage_compliance'],
|
||||
'operator' => ['view_events','acknowledge','edit_zones'],
|
||||
'viewer' => ['view_events'],
|
||||
];
|
||||
public static function can(string $role, string $permission): bool {
|
||||
return in_array($permission, self::MATRIX[$role] ?? [], true);
|
||||
}
|
||||
}
|
||||
28
cloud/tests/RbacTest.php
Normal file
28
cloud/tests/RbacTest.php
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace App\Tests;
|
||||
use App\Auth\Rbac;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class RbacTest extends TestCase
|
||||
{
|
||||
public function testViewerCanOnlyView(): void {
|
||||
$this->assertTrue(Rbac::can('viewer', 'view_events'));
|
||||
$this->assertFalse(Rbac::can('viewer', 'acknowledge'));
|
||||
$this->assertFalse(Rbac::can('viewer', 'edit_zones'));
|
||||
}
|
||||
public function testOperator(): void {
|
||||
$this->assertTrue(Rbac::can('operator', 'acknowledge'));
|
||||
$this->assertTrue(Rbac::can('operator', 'edit_zones'));
|
||||
$this->assertFalse(Rbac::can('operator', 'manage_thresholds'));
|
||||
}
|
||||
public function testAdminAll(): void {
|
||||
foreach (['view_events','acknowledge','edit_zones','manage_thresholds','manage_compliance'] as $p) {
|
||||
$this->assertTrue(Rbac::can('admin', $p));
|
||||
}
|
||||
}
|
||||
public function testUnknownDenied(): void {
|
||||
$this->assertFalse(Rbac::can('ghost', 'view_events'));
|
||||
$this->assertFalse(Rbac::can('admin', 'unknown_perm'));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue