|
3 | 3 | namespace AuroraWebSoftware\AAuth\Scopes;
|
4 | 4 |
|
5 | 5 | use AuroraWebSoftware\AAuth\Facades\AAuth;
|
| 6 | +use AuroraWebSoftware\AAuth\Interfaces\AAuthABACModelInterface; |
6 | 7 | use AuroraWebSoftware\AAuth\Utils\ABACUtil;
|
7 | 8 | use Exception;
|
8 | 9 | use Illuminate\Database\Eloquent\Builder;
|
9 | 10 | use Illuminate\Database\Eloquent\Model;
|
10 | 11 | use Illuminate\Database\Eloquent\Scope;
|
11 | 12 |
|
| 13 | +/** |
| 14 | + * @template TModelClass of Model |
| 15 | + */ |
12 | 16 | class AAuthABACModelScope implements Scope
|
13 | 17 | {
|
14 | 18 | /**
|
| 19 | + * @param Builder<TModelClass> $builder |
| 20 | + * @param Model $model |
| 21 | + * @param mixed $rules |
| 22 | + * @param string $parentOperator |
| 23 | + * @return void |
15 | 24 | * @throws Exception
|
16 | 25 | */
|
17 |
| - public function apply(Builder $builder, Model $model, $rules = false, $parentOperator = '&&') |
| 26 | + public function apply(Builder $builder, Model $model, mixed $rules = false, string $parentOperator = '&&'): void |
18 | 27 | {
|
19 | 28 | if ($rules === false) {
|
| 29 | + /** |
| 30 | + * @var AAuthABACModelInterface $model |
| 31 | + * |
| 32 | + * PHPStan analysis does not return any errors, but it underlines the ABACRules method because it somehow |
| 33 | + * does not see it, even though it is defined in the facade. |
| 34 | + * @phpstan-ignore-next-line |
| 35 | + */ |
20 | 36 | $rules = AAuth::ABACRules($model::getModelType()) ?? [];
|
| 37 | + |
| 38 | + /** |
| 39 | + * @var array $rules |
| 40 | + */ |
21 | 41 | ABACUtil::validateAbacRuleArray($rules);
|
22 | 42 |
|
23 |
| - return $builder->where( |
24 |
| - function ($query) use ($rules, $model) { |
25 |
| - $this->apply($query, $model, $rules); |
26 |
| - } |
27 |
| - ); |
28 |
| - } |
| 43 | + $builder->where(function ($query) use ($rules, $model) { |
| 44 | + /** |
| 45 | + * @var Model $model |
| 46 | + */ |
| 47 | + $this->apply($query, $model, $rules); |
| 48 | + }); |
| 49 | + } else { |
| 50 | + $logicalOperators = ["&&","||"]; |
29 | 51 |
|
30 |
| - // todo refactor gerekebilir |
31 |
| - foreach ($rules as $key => $rule) { |
32 |
| - if ($key == '&&') { |
33 |
| - if ($parentOperator == '||') { |
34 |
| - $builder->orWhere( |
35 |
| - function ($query) use ($rule, $model) { |
36 |
| - $this->apply($query, $model, $rule); |
37 |
| - } |
38 |
| - ); |
39 |
| - } else { |
40 |
| - $builder->where( |
41 |
| - function ($query) use ($rule, $model) { |
42 |
| - $this->apply($query, $model, $rule); |
43 |
| - } |
44 |
| - ); |
45 |
| - } |
46 |
| - } elseif ($key == '||') { |
47 |
| - if ($parentOperator == '||') { |
48 |
| - $builder->orWhere( |
49 |
| - function ($query) use ($rule, $model) { |
50 |
| - $this->apply($query, $model, $rule, '||'); |
51 |
| - } |
52 |
| - ); |
53 |
| - } else { |
54 |
| - $builder->where( |
55 |
| - function ($query) use ($rule, $model) { |
56 |
| - $this->apply($query, $model, $rule, '||'); |
57 |
| - } |
58 |
| - ); |
59 |
| - } |
60 |
| - } else { |
61 |
| - $operator = array_key_first($rule); |
62 |
| - if ($parentOperator == '||') { |
63 |
| - $builder->orWhere( |
64 |
| - $rule[array_key_first($rule)]['attribute'], |
65 |
| - $operator, |
66 |
| - $rule[array_key_first($rule)]['value'] |
67 |
| - ); |
| 52 | + foreach ($rules as $rule) { |
| 53 | + $firstKey = array_key_first($rule); |
| 54 | + $abacRule = $rule[$firstKey]; |
| 55 | + |
| 56 | + if (in_array($firstKey, $logicalOperators)) { |
| 57 | + $this->applyLogicalOperator($builder, $abacRule, $model, $firstKey, $parentOperator); |
68 | 58 | } else {
|
69 |
| - $builder->where( |
70 |
| - $rule[array_key_first($rule)]['attribute'], |
71 |
| - $operator, |
72 |
| - $rule[array_key_first($rule)]['value'] |
73 |
| - ); |
| 59 | + $this->applyConditionalOperator($builder, $rule, $parentOperator); |
74 | 60 | }
|
75 | 61 | }
|
76 | 62 | }
|
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Apply logical operator (&& or ||) to the query builder. |
| 67 | + * |
| 68 | + * @param Builder<TModelClass> $builder |
| 69 | + * @param array $abacRule |
| 70 | + * @param Model $model |
| 71 | + * @param string $logicalOperator |
| 72 | + * @param string $parentOperator |
| 73 | + * |
| 74 | + * @return void |
| 75 | + * @throws Exception |
| 76 | + */ |
| 77 | + protected function applyLogicalOperator(Builder $builder, array $abacRule, Model $model, string $logicalOperator, string $parentOperator): void |
| 78 | + { |
| 79 | + $queryMethod = $parentOperator == '&&' ? 'where' : 'orWhere'; |
77 | 80 |
|
78 |
| - // todo refactor gerekebilir |
79 |
| - /* |
80 |
| - foreach ($rules as $key => $rule) { |
81 |
| - if ($key == '&&') { |
82 |
| - foreach ($rule as $subkey => $subrule) { |
83 |
| - $suboperator = array_key_first($subrule); |
| 81 | + $builder->{$queryMethod}(function ($query) use ($abacRule, $model, $logicalOperator) { |
| 82 | + $this->apply($query, $model, $abacRule, $logicalOperator); |
| 83 | + }); |
| 84 | + } |
84 | 85 |
|
85 |
| - if ($suboperator == '&&' or $suboperator == '||') { |
86 |
| - $builder->where( |
87 |
| - function ($query) use ($subrule, $model) { |
88 |
| - $this->apply($query, $model, $subrule); |
89 |
| - } |
90 |
| - ); |
91 |
| - } else { |
92 |
| - $builder->where( |
93 |
| - $subrule[array_key_first($subrule)]['attribute'], |
94 |
| - $suboperator, |
95 |
| - $subrule[array_key_first($subrule)]['value'] |
96 |
| - ); |
97 |
| - } |
98 |
| - } |
99 |
| - } elseif ($key == '||') { |
100 |
| - foreach ($rule as $subkey => $subrule) { |
101 |
| - $suboperator = array_key_first($subrule); |
102 |
| - if ($suboperator == '&&' || $suboperator == '||') { |
103 |
| - $builder->where( |
104 |
| - function ($query) use ($subrule, $model) { |
105 |
| - $this->apply($query, $model, $subrule); |
106 |
| - } |
107 |
| - ); |
108 |
| - } else { |
109 |
| - $builder->orWhere( |
110 |
| - $subrule[array_key_first($subrule)]['attribute'], |
111 |
| - $suboperator, |
112 |
| - $subrule[array_key_first($subrule)]['value'] |
113 |
| - ); |
114 |
| - } |
115 |
| - } |
116 |
| - } |
117 |
| - } |
118 |
| - */ |
| 86 | + /** |
| 87 | + * Apply conditional operator to the query builder. |
| 88 | + * |
| 89 | + * @param Builder<TModelClass> $builder |
| 90 | + * @param array $rule |
| 91 | + * @param string $parentOperator |
| 92 | + * |
| 93 | + * @return void |
| 94 | + */ |
| 95 | + protected function applyConditionalOperator(Builder $builder, array $rule, string $parentOperator): void |
| 96 | + { |
| 97 | + $operator = array_key_first($rule); |
| 98 | + |
| 99 | + $queryMethod = $parentOperator == '||' ? 'orWhere' : 'where'; |
| 100 | + |
| 101 | + $builder->{$queryMethod}( |
| 102 | + $rule[$operator]['attribute'], |
| 103 | + $operator, |
| 104 | + $rule[$operator]['value'] |
| 105 | + ); |
119 | 106 | }
|
120 | 107 | }
|
0 commit comments