@@ -119,10 +119,7 @@ public function loadPolicy(Model $model): void
119
119
$ rows = $ this ->connection ->query ('SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName .'' );
120
120
121
121
foreach ($ rows as $ row ) {
122
- $ line = implode (', ' , array_filter ($ row , function ($ val ) {
123
- return '' != $ val && !is_null ($ val );
124
- }));
125
- $ this ->loadPolicyLine (trim ($ line ), $ model );
122
+ $ this ->loadPolicyArray ($ this ->filterRule ($ row ), $ model );
126
123
}
127
124
}
128
125
@@ -183,11 +180,11 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
183
180
{
184
181
$ this ->connection ->getPdo ()->beginTransaction ();
185
182
try {
186
- foreach ($ rules as $ rule ) {
183
+ foreach ($ rules as $ rule ) {
187
184
$ this ->removePolicy ($ sec , $ ptype , $ rule );
188
185
}
189
186
$ this ->connection ->getPdo ()->commit ();
190
- } catch (Throwable $ e ){
187
+ } catch (Throwable $ e ) {
191
188
$ this ->connection ->getPdo ()->rollback ();
192
189
throw $ e ;
193
190
}
@@ -252,16 +249,16 @@ public function _removeFilteredPolicy(string $sec, string $ptype, int $fieldInde
252
249
return $ removedRules ;
253
250
}
254
251
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
- */
252
+ /**
253
+ * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
254
+ * This is part of the Auto-Save feature.
255
+ *
256
+ * @param string $sec
257
+ * @param string $ptype
258
+ * @param int $fieldIndex
259
+ * @param string ...$fieldValues
260
+ * @throws Exception|Throwable
261
+ */
265
262
public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , string ...$ fieldValues ): void
266
263
{
267
264
$ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
@@ -288,14 +285,14 @@ public function loadFilteredPolicy(Model $model, $filter): void
288
285
$ filter = explode ('= ' , $ filter );
289
286
$ sql .= "$ filter [0 ] = : {$ filter [0 ]}" ;
290
287
$ bind [$ filter [0 ]] = $ filter [1 ];
291
- } else if ($ filter instanceof Filter) {
292
- foreach ($ filter ->p as $ k => $ v ) {
288
+ } elseif ($ filter instanceof Filter) {
289
+ foreach ($ filter ->p as $ k => $ v ) {
293
290
$ where [] = $ v . ' = : ' . $ v ;
294
291
$ bind [$ v ] = $ filter ->g [$ k ];
295
292
}
296
293
$ where = implode (' AND ' , $ where );
297
294
$ sql .= $ where ;
298
- } else if ($ filter instanceof Closure) {
295
+ } elseif ($ filter instanceof Closure) {
299
296
$ where = '' ;
300
297
$ filter ($ where );
301
298
$ where = str_replace (' ' , '' , $ where );
@@ -308,8 +305,10 @@ public function loadFilteredPolicy(Model $model, $filter): void
308
305
}
309
306
310
307
$ rows = $ this ->connection ->query ($ sql , $ bind );
311
- foreach ($ rows as $ row ) {
312
- $ row = array_filter ($ row , function ($ value ) { return !is_null ($ value ) && $ value !== '' ; });
308
+ foreach ($ rows as $ row ) {
309
+ $ row = array_filter ($ row , function ($ value ) {
310
+ return !is_null ($ value ) && $ value !== '' ;
311
+ });
313
312
unset($ row ['id ' ]);
314
313
$ line = implode (', ' , array_filter ($ row , function ($ val ) {
315
314
return '' != $ val && !is_null ($ val );
@@ -334,21 +333,21 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
334
333
$ where ['ptype ' ] = $ ptype ;
335
334
$ condition [] = 'ptype = :ptype ' ;
336
335
337
- foreach ($ oldRule as $ key => $ value ) {
336
+ foreach ($ oldRule as $ key => $ value ) {
338
337
$ placeholder = "w " . strval ($ key );
339
338
$ where ['w ' . strval ($ key )] = $ value ;
340
339
$ condition [] = 'v ' . strval ($ key ) . ' = : ' . $ placeholder ;
341
340
}
342
341
343
342
$ update = [];
344
- foreach ($ newPolicy as $ key => $ value ) {
343
+ foreach ($ newPolicy as $ key => $ value ) {
345
344
$ placeholder = "s " . strval ($ key );
346
345
$ updateValue ["$ placeholder " ] = $ value ;
347
346
$ update [] = 'v ' . strval ($ key ) . ' = : ' . $ placeholder ;
348
347
}
349
348
350
349
$ sql = "UPDATE {$ this ->casbinRuleTableName } SET " . implode (', ' , $ update ) . " WHERE " . implode (' AND ' , $ condition );
351
-
350
+
352
351
$ this ->connection ->execute ($ sql , array_merge ($ updateValue , $ where ));
353
352
}
354
353
0 commit comments