Skip to content

Commit 426643e

Browse files
authored
Add allocation to role permission models & make sure user can target node of allocation (#2124)
1 parent 3ca0f64 commit 426643e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

app/Enums/RolePermissionModels.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
enum RolePermissionModels: string
66
{
77
case ApiKey = 'apiKey';
8+
case Allocation = 'allocation';
89
case DatabaseHost = 'databaseHost';
910
case Database = 'database';
1011
case Egg = 'egg';
@@ -34,4 +35,9 @@ public function update(): string
3435
{
3536
return RolePermissionPrefixes::Update->value . ' ' . $this->value;
3637
}
38+
39+
public function delete(): string
40+
{
41+
return RolePermissionPrefixes::Delete->value . ' ' . $this->value;
42+
}
3743
}

app/Policies/AllocationPolicy.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Policies;
44

55
use App\Enums\SubuserPermission;
6+
use App\Models\Allocation;
67
use App\Models\Server;
78
use App\Models\User;
89
use Filament\Facades\Filament;
@@ -21,6 +22,23 @@ class AllocationPolicy
2122

2223
protected string $modelName = 'allocation';
2324

25+
public function before(User $user, string $ability, string|Allocation $allocation): ?bool
26+
{
27+
// For "viewAny" the $allocation param is the class name
28+
if (is_string($allocation)) {
29+
return null;
30+
}
31+
32+
/** @var ?Server $server */
33+
$server = Filament::getTenant();
34+
35+
if (!$server && !$user->canTarget($allocation->node)) {
36+
return false;
37+
}
38+
39+
return null;
40+
}
41+
2442
public function viewAny(User $user): bool
2543
{
2644
/** @var ?Server $server */

0 commit comments

Comments
 (0)