| 
7 | 7 | use Lauthz\Models\Rule;  | 
8 | 8 | use Lauthz\Contracts\DatabaseAdapter as DatabaseAdapterContract;  | 
9 | 9 | use Lauthz\Contracts\BatchDatabaseAdapter as BatchDatabaseAdapterContract;  | 
10 |  | -use Lauthz\Contracts\UpdatableDatabaseAdapter as UpdatableDatabaseAdapterContract;   | 
 | 10 | +use Lauthz\Contracts\UpdatableDatabaseAdapter as UpdatableDatabaseAdapterContract;  | 
 | 11 | +use Lauthz\Contracts\FilteredDatabaseAdapter as FilteredDatabaseAdapterContract;  | 
 | 12 | +use Casbin\Persist\Adapters\Filter;  | 
11 | 13 | use Casbin\Model\Model;  | 
12 | 14 | use Casbin\Persist\AdapterHelper;  | 
13 | 15 | use DateTime;  | 
 | 16 | +use Casbin\Exceptions\InvalidFilterTypeException;  | 
14 | 17 | /**  | 
15 | 18 |  * DatabaseAdapter.  | 
16 | 19 |  *  | 
17 | 20 |  | 
18 | 21 |  */  | 
19 |  | -class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterContract, UpdatableDatabaseAdapterContract  | 
 | 22 | +class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterContract, UpdatableDatabaseAdapterContract, FilteredDatabaseAdapterContract  | 
20 | 23 | {  | 
21 | 24 |     use AdapterHelper;  | 
22 | 25 | 
 
  | 
 | 26 | +    /**  | 
 | 27 | +     * @var bool  | 
 | 28 | +     */  | 
 | 29 | +    private $filtered = false;  | 
 | 30 | + | 
23 | 31 |     /**  | 
24 | 32 |      * Rules eloquent model.  | 
25 | 33 |      *  | 
@@ -232,4 +240,57 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $  | 
232 | 240 |         }  | 
233 | 241 |         $instance->update($update);  | 
234 | 242 |     }  | 
 | 243 | + | 
 | 244 | +    /**  | 
 | 245 | +     * Loads only policy rules that match the filter.  | 
 | 246 | +     *  | 
 | 247 | +     * @param Model $model  | 
 | 248 | +     * @param mixed $filter  | 
 | 249 | +     */  | 
 | 250 | +    public function loadFilteredPolicy(Model $model, $filter): void  | 
 | 251 | +    {  | 
 | 252 | +        $instance = $this->eloquent;  | 
 | 253 | + | 
 | 254 | +        if (is_string($filter)) {  | 
 | 255 | +            $instance = $instance->whereRaw($filter);  | 
 | 256 | +        } else if ($filter instanceof Filter) {  | 
 | 257 | +            foreach($filter->p as $k => $v) {  | 
 | 258 | +                $where[$v] = $filter->g[$k];  | 
 | 259 | +                $instance = $instance->where($v, $filter->g[$k]);  | 
 | 260 | +            }  | 
 | 261 | +        } else if ($filter instanceof \Closure) {  | 
 | 262 | +            $instance = $instance->where($filter);  | 
 | 263 | +        } else {  | 
 | 264 | +            throw new InvalidFilterTypeException('invalid filter type');  | 
 | 265 | +        }  | 
 | 266 | +        $rows = $instance->get()->makeHidden(['created_at','updated_at', 'id'])->toArray();  | 
 | 267 | +        foreach ($rows as $row) {  | 
 | 268 | +            $row = array_filter($row, function($value) { return !is_null($value) && $value !== ''; });  | 
 | 269 | +            $line = implode(', ', array_filter($row, function ($val) {  | 
 | 270 | +                return '' != $val && !is_null($val);  | 
 | 271 | +            }));  | 
 | 272 | +            $this->loadPolicyLine(trim($line), $model);  | 
 | 273 | +        }  | 
 | 274 | +        $this->setFiltered(true);  | 
 | 275 | +    }  | 
 | 276 | + | 
 | 277 | +    /**  | 
 | 278 | +     * Returns true if the loaded policy has been filtered.  | 
 | 279 | +     *  | 
 | 280 | +     * @return bool  | 
 | 281 | +     */  | 
 | 282 | +    public function isFiltered(): bool  | 
 | 283 | +    {  | 
 | 284 | +        return $this->filtered;  | 
 | 285 | +    }  | 
 | 286 | + | 
 | 287 | +    /**  | 
 | 288 | +     * Sets filtered parameter.  | 
 | 289 | +     *  | 
 | 290 | +     * @param bool $filtered  | 
 | 291 | +     */  | 
 | 292 | +    public function setFiltered(bool $filtered): void  | 
 | 293 | +    {  | 
 | 294 | +        $this->filtered = $filtered;  | 
 | 295 | +    }  | 
235 | 296 | }  | 
0 commit comments