@@ -229,51 +229,47 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
229
229
*/
230
230
public function loadFilteredPolicy (Model $ model , $ filter ): void
231
231
{
232
- // if $filter is empty, load all policies
233
- if (is_null ($ filter )) {
234
- $ this ->loadPolicy ($ model );
235
- return ;
236
- }
237
232
// the basic sql
238
233
$ sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM ' .$ this ->casbinRuleTableName . ' WHERE ' ;
239
-
240
- if ($ filter instanceof Filter) {
241
- $ type = '' ;
242
- $ filter = (array ) $ filter ;
243
- // choose which ptype to use
244
- foreach ($ filter as $ i => $ v ) {
245
- if (!empty ($ v )) {
246
- array_unshift ($ filter [$ i ], $ i );
247
- $ type = $ i ;
248
- break ;
249
- }
250
- }
251
- $ items = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
252
- $ temp = [];
253
- $ values = [];
254
- foreach ($ items as $ i => $ item ) {
255
- if (isset ($ filter [$ type ][$ i ]) && !empty ($ filter [$ type ][$ i ])) {
256
- array_push ($ temp , $ item . '=: ' . $ item );
257
- $ values [$ item ] = $ filter [$ type ][$ i ];
258
- }
234
+
235
+ $ bind = [];
236
+
237
+ if (is_string ($ filter )) {
238
+ $ filter = str_replace (' ' , '' , $ filter );
239
+ $ filter = str_replace ('\'' , '' , $ filter );
240
+ $ filter = explode ('= ' , $ filter );
241
+ $ sql .= "$ filter [0 ] = : {$ filter [0 ]}" ;
242
+ $ bind [$ filter [0 ]] = $ filter [1 ];
243
+ } else if ($ filter instanceof Filter) {
244
+ foreach ($ filter ->p as $ k => $ v ) {
245
+ $ where [] = $ v . ' = : ' . $ v ;
246
+ $ bind [$ v ] = $ filter ->g [$ k ];
259
247
}
260
- $ sql .= implode (' and ' , $ temp );
261
- $ rows = $ this ->connection ->query ($ sql , $ values );
262
- } elseif (is_string ($ filter )) {
263
- $ sql .= $ filter ;
264
- $ rows = $ this ->connection ->query ($ sql );
248
+ $ where = implode (' AND ' , $ where );
249
+ $ sql .= $ where ;
265
250
} else if ($ filter instanceof Closure) {
266
- $ filter ($ this ->connection , $ sql , $ this ->rows );
251
+ $ where = '' ;
252
+ $ filter ($ where );
253
+ $ where = str_replace (' ' , '' , $ where );
254
+ $ where = str_replace ('\'' , '' , $ where );
255
+ $ where = explode ('= ' , $ where );
256
+ $ sql .= "$ where [0 ] = : {$ where [0 ]}" ;
257
+ $ bind [$ where [0 ]] = $ where [1 ];
267
258
} else {
268
259
throw new InvalidFilterTypeException ('invalid filter type ' );
269
260
}
270
261
271
- $ rows = $ rows ?? $ this ->rows ;
262
+ $ rows = $ this ->connection -> query ( $ sql , $ bind ) ;
272
263
foreach ($ rows as $ row ) {
273
- $ line = implode (', ' , $ row );
274
- $ this ->loadPolicyLine ($ line , $ model );
264
+ $ row = array_filter ($ row , function ($ value ) { return !is_null ($ value ) && $ value !== '' ; });
265
+ unset($ row ['id ' ]);
266
+ $ line = implode (', ' , array_filter ($ row , function ($ val ) {
267
+ return '' != $ val && !is_null ($ val );
268
+ }));
269
+ $ this ->loadPolicyLine (trim ($ line ), $ model );
275
270
}
276
- $ this ->filtered = true ;
271
+
272
+ $ this ->setFiltered (true );
277
273
}
278
274
279
275
/**
0 commit comments