5
5
use tauthz \model \Rule ;
6
6
use tauthz \cache \CacheHandlerContract ;
7
7
use Casbin \Model \Model ;
8
- use Casbin \Persist \Adapter ;
9
- use Casbin \Persist \AdapterHelper ;
10
- use Casbin \Persist \UpdatableAdapter ;
11
- use Casbin \Persist \BatchAdapter ;
12
- use Casbin \Persist \FilteredAdapter ;
8
+ use Casbin \Persist \{Adapter , AdapterHelper , UpdatableAdapter , BatchAdapter , FilteredAdapter };
13
9
use Casbin \Persist \Adapters \Filter ;
14
10
use Casbin \Exceptions \InvalidFilterTypeException ;
15
11
use tauthz \traits \Configurable ;
@@ -27,21 +23,21 @@ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter, Filter
27
23
/**
28
24
* @var bool
29
25
*/
30
- private $ filtered = false ;
26
+ private bool $ filtered = false ;
31
27
32
28
/**
33
29
* Rules model.
34
30
*
35
31
* @var Rule
36
32
*/
37
- protected $ model ;
33
+ protected Rule $ model ;
38
34
39
35
/**
40
36
* Cache Handler.
41
37
*
42
38
* @var CacheHandlerContract
43
39
*/
44
- protected $ cacheHandler ;
40
+ protected CacheHandlerContract $ cacheHandler ;
45
41
46
42
/**
47
43
* the DatabaseAdapter constructor.
@@ -84,12 +80,13 @@ public function filterRule(array $rule): array
84
80
*
85
81
* @return void
86
82
*/
87
- public function savePolicyLine ($ ptype , array $ rule )
83
+ public function savePolicyLine (string $ ptype , array $ rule ): void
88
84
{
89
85
$ col ['ptype ' ] = $ ptype ;
90
86
foreach ($ rule as $ key => $ value ) {
91
- $ col ['v ' . strval ($ key ). '' ] = $ value ;
87
+ $ col ['v ' . strval ($ key ) . '' ] = $ value ;
92
88
}
89
+
93
90
$ this ->cacheHandler ->cachePolicies ($ this ->model )->insert ($ col );
94
91
}
95
92
@@ -100,7 +97,8 @@ public function savePolicyLine($ptype, array $rule)
100
97
*/
101
98
public function loadPolicy (Model $ model ): void
102
99
{
103
- $ rows = $ this ->cacheHandler ->cachePolicies ($ this ->model )->field (['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ])->select ()->toArray ();
100
+ $ rows = $ this ->cacheHandler ->cachePolicies ($ this ->model )->field (['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ])
101
+ ->select ()->toArray ();
104
102
foreach ($ rows as $ row ) {
105
103
$ this ->loadPolicyArray ($ this ->filterRule ($ row ), $ model );
106
104
}
@@ -160,6 +158,7 @@ public function addPolicies(string $sec, string $ptype, array $rules): void
160
158
$ cols [$ i ++] = $ temp ;
161
159
$ temp = [];
162
160
}
161
+
163
162
$ this ->cacheHandler ->cachePolicies ($ this ->model )->insertAll ($ cols );
164
163
}
165
164
@@ -177,7 +176,7 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
177
176
$ instance = $ this ->model ->where ('ptype ' , $ ptype );
178
177
179
178
foreach ($ rule as $ key => $ value ) {
180
- $ instance ->where ('v ' . strval ($ key ), $ value );
179
+ $ instance ->where ('v ' . strval ($ key ), $ value );
181
180
}
182
181
183
182
foreach ($ instance ->select () as $ model ) {
@@ -234,7 +233,7 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
234
233
++$ count ;
235
234
}
236
235
}
237
-
236
+
238
237
return $ removedRules ;
239
238
}
240
239
@@ -271,7 +270,7 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
271
270
272
271
foreach ($ newPolicy as $ key => $ value ) {
273
272
$ column = 'v ' . strval ($ key );
274
- $ instance ->$ column = $ value ;
273
+ $ instance ->{ $ column} = $ value ;
275
274
}
276
275
277
276
$ instance ->save ();
@@ -360,14 +359,12 @@ public function loadFilteredPolicy(Model $model, $filter): void
360
359
}
361
360
$ rows = $ instance ->select ()->hidden (['id ' ])->toArray ();
362
361
foreach ($ rows as $ row ) {
363
- $ row = array_filter ($ row , function ($ value ) {
364
- return !is_null ($ value ) && $ value !== '' ;
365
- });
366
- $ line = implode (', ' , array_filter ($ row , function ($ val ) {
367
- return '' != $ val && !is_null ($ val );
368
- }));
362
+ $ row = array_filter ($ row , fn ($ value ) => !is_null ($ value ) && $ value !== '' );
363
+ $ line = implode (', ' , array_filter ($ row , fn ($ val ) => '' != $ val && !is_null ($ val )));
364
+
369
365
$ this ->loadPolicyLine (trim ($ line ), $ model );
370
366
}
367
+
371
368
$ this ->setFiltered (true );
372
369
}
373
370
}
0 commit comments