-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbacPolicyRequest.php
51 lines (44 loc) · 1.4 KB
/
AbacPolicyRequest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace zennit\ABAC\Http\Requests;
use Illuminate\Validation\Rule;
use zennit\ABAC\Enums\PolicyMethod;
class AbacPolicyRequest extends Request
{
protected function getRules(): array
{
return [
'method' => ['required', Rule::enum(PolicyMethod::class)],
'resource' => [
'required',
'string',
'regex:/^App\\\\Models(\\\\[A-Z][A-Za-z0-9_]*)+$/',
],
'chains' => [
'array',
'max:1',
function ($attribute, $value, $fail) {
if (is_array($value) && count($value) > 0) {
foreach ($value as $chain) {
$this->validateChainStructure($chain, $fail);
}
}
},
],
];
}
protected function validateChainStructure($chain, $fail): void
{
$childCount = 0;
// Count chains
if (isset($chain['chains']) && is_array($chain['chains'])) {
$childCount += count($chain['chains']);
}
// Count checks
if (isset($chain['checks']) && is_array($chain['checks'])) {
$childCount += count($chain['checks']);
}
if ($childCount > 2) {
$fail('Each chain can have a maximum of 2 children (chains or checks combined)');
}
}
}