@@ -61,6 +61,26 @@ public function setFiltered(bool $filtered): void
61
61
$ this ->filtered = $ filtered ;
62
62
}
63
63
64
+ /**
65
+ * Filter the rule.
66
+ *
67
+ * @param array $rule
68
+ * @return array
69
+ */
70
+ public function filterRule (array $ rule ): array
71
+ {
72
+ $ rule = array_values ($ rule );
73
+
74
+ $ i = count ($ rule ) - 1 ;
75
+ for (; $ i >= 0 ; $ i --) {
76
+ if ($ rule [$ i ] != '' && !is_null ($ rule [$ i ])) {
77
+ break ;
78
+ }
79
+ }
80
+
81
+ return array_slice ($ rule , 0 , $ i + 1 );
82
+ }
83
+
64
84
public static function newAdapter (array $ config )
65
85
{
66
86
return new static ($ config );
@@ -195,16 +215,16 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
195
215
}
196
216
197
217
/**
198
- * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
199
- * This is part of the Auto-Save feature.
200
- *
201
218
* @param string $sec
202
219
* @param string $ptype
203
- * @param int $fieldIndex
204
- * @param string ...$fieldValues
220
+ * @param int $fieldIndex
221
+ * @param string|null ...$fieldValues
222
+ * @return array
223
+ * @throws Throwable
205
224
*/
206
- public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , string ...$ fieldValues ): void
225
+ public function _removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , ? string ...$ fieldValues ): array
207
226
{
227
+ $ removedRules = [];
208
228
$ where ['ptype ' ] = $ ptype ;
209
229
$ condition [] = 'ptype = :ptype ' ;
210
230
foreach (range (0 , 5 ) as $ value ) {
@@ -216,9 +236,35 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
216
236
}
217
237
}
218
238
219
- $ sql = ' DELETE FROM ' . $ this ->casbinRuleTableName . ' WHERE ' . implode (' AND ' , $ condition );
239
+ $ deleteSql = " DELETE FROM { $ this ->casbinRuleTableName } WHERE " . implode (' AND ' , $ condition );
220
240
221
- $ this ->connection ->execute ($ sql , $ where );
241
+ $ selectSql = "SELECT * FROM {$ this ->casbinRuleTableName } WHERE " . implode (' AND ' , $ condition );
242
+
243
+ $ oldP = $ this ->connection ->query ($ selectSql , $ where );
244
+ foreach ($ oldP as &$ item ) {
245
+ unset($ item ['ptype ' ]);
246
+ unset($ item ['id ' ]);
247
+ $ item = $ this ->filterRule ($ item );
248
+ $ removedRules [] = $ item ;
249
+ }
250
+
251
+ $ this ->connection ->execute ($ deleteSql , $ where );
252
+ return $ removedRules ;
253
+ }
254
+
255
+ /**
256
+ * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
257
+ * This is part of the Auto-Save feature.
258
+ *
259
+ * @param string $sec
260
+ * @param string $ptype
261
+ * @param int $fieldIndex
262
+ * @param string ...$fieldValues
263
+ * @throws Exception|Throwable
264
+ */
265
+ public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , string ...$ fieldValues ): void
266
+ {
267
+ $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
222
268
}
223
269
224
270
/**
@@ -330,66 +376,27 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
330
376
}
331
377
332
378
/**
333
- * UpdateFilteredPolicies deletes old rules and adds new rules.
334
- *
335
379
* @param string $sec
336
380
* @param string $ptype
337
- * @param array $newPolicies
338
- * @param integer $fieldIndex
381
+ * @param array $newRules
382
+ * @param int $fieldIndex
339
383
* @param string ...$fieldValues
340
384
* @return array
385
+ * @throws Throwable
341
386
*/
342
- public function updateFilteredPolicies (string $ sec , string $ ptype , array $ newPolicies , int $ fieldIndex , string ...$ fieldValues ): array
387
+ public function updateFilteredPolicies (string $ sec , string $ ptype , array $ newRules , int $ fieldIndex , ? string ...$ fieldValues ): array
343
388
{
344
- $ deleteWhere ['ptype ' ] = $ ptype ;
345
- $ deleteCondition [] = 'ptype = :ptype ' ;
346
- foreach ($ fieldValues as $ value ) {
347
- $ key = $ fieldIndex ++;
348
- if (!is_null ($ value ) && $ value !== '' ) {
349
- $ placeholder = "v " . strval ($ key );
350
- $ deleteWhere ['v ' . strval ($ key )] = $ value ;
351
- $ deleteCondition [] = 'v ' . strval ($ key ) . ' = : ' . $ placeholder ;
352
- }
353
- }
354
- $ deleteSql = "DELETE FROM {$ this ->casbinRuleTableName } WHERE " . implode (' AND ' , $ deleteCondition );
355
-
356
- $ selectSql = "SELECT * FROM {$ this ->casbinRuleTableName } WHERE " . implode (' AND ' , $ deleteCondition );
357
- $ oldP = $ this ->connection ->query ($ selectSql , $ deleteWhere );
358
- foreach ($ oldP as &$ item ) {
359
- $ item = array_filter ($ item , function ($ value ) {
360
- return !is_null ($ value ) && $ value !== '' ;
361
- });
362
- unset($ item ['ptype ' ]);
363
- unset($ item ['id ' ]);
364
- }
365
-
366
- $ columns = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
367
- $ values = [];
368
- $ sets = [];
369
- $ columnsCount = count ($ columns );
370
- foreach ($ newPolicies as $ newPolicy ) {
371
- array_unshift ($ newPolicy , $ ptype );
372
- $ values = array_merge ($ values , array_pad ($ newPolicy , $ columnsCount , null ));
373
- $ sets [] = array_pad ([], $ columnsCount , '? ' );
374
- }
375
- $ valuesStr = implode (', ' , array_map (function ($ set ) {
376
- return '( ' . implode (', ' , $ set ) . ') ' ;
377
- }, $ sets ));
378
- $ insertSql = 'INSERT INTO ' . $ this ->casbinRuleTableName . ' ( ' . implode (', ' , $ columns ) . ') ' . ' VALUES ' . $ valuesStr ;
379
-
380
- // start transaction
389
+ $ oldRules = [];
381
390
$ this ->connection ->getPdo ()->beginTransaction ();
382
391
try {
383
- // delete old data
384
- $ this ->connection ->execute ($ deleteSql , $ deleteWhere );
385
- // insert new data
386
- $ this ->connection ->execute ($ insertSql , $ values );
392
+ $ oldRules = $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
393
+ $ this ->addPolicies ($ sec , $ ptype , $ newRules );
387
394
$ this ->connection ->getPdo ()->commit ();
388
395
} catch (Throwable $ e ) {
389
396
$ this ->connection ->getPdo ()->rollback ();
390
397
throw $ e ;
391
398
}
392
399
393
- return $ oldP ;
400
+ return $ oldRules ;
394
401
}
395
402
}
0 commit comments