|
6 | 6 | use Casbin\Persist\Adapter as AdapterContract;
|
7 | 7 | use TechOne\Database\Manager;
|
8 | 8 | use Casbin\Persist\AdapterHelper;
|
| 9 | +use Casbin\Persist\FilteredAdapter; |
| 10 | +use Casbin\Persist\Adapters\Filter; |
| 11 | +use Casbin\Exceptions\InvalidFilterTypeException; |
9 | 12 |
|
10 | 13 | /**
|
11 | 14 | * DatabaseAdapter.
|
12 | 15 | *
|
13 | 16 |
|
14 | 17 | */
|
15 |
| -class Adapter implements AdapterContract |
| 18 | +class Adapter implements AdapterContract, FilteredAdapter |
16 | 19 | {
|
17 | 20 | use AdapterHelper;
|
18 | 21 |
|
19 | 22 | protected $config;
|
20 | 23 |
|
| 24 | + protected $filtered; |
| 25 | + |
21 | 26 | protected $connection;
|
22 | 27 |
|
23 | 28 | public $casbinRuleTableName = 'casbin_rule';
|
24 | 29 |
|
25 | 30 | public function __construct(array $config)
|
26 | 31 | {
|
27 | 32 | $this->config = $config;
|
| 33 | + $this->filtered = false; |
28 | 34 | $this->connection = (new Manager($config))->getConnection();
|
29 | 35 | $this->initTable();
|
30 | 36 | }
|
31 | 37 |
|
| 38 | + /** |
| 39 | + * Returns true if the loaded policy has been filtered. |
| 40 | + * |
| 41 | + * @return bool |
| 42 | + */ |
| 43 | + public function isFiltered(): bool |
| 44 | + { |
| 45 | + return $this->filtered; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Loads only policy rules that match the filter from storage. |
| 50 | + * |
| 51 | + * @param Model $model |
| 52 | + * @param mixed $filter |
| 53 | + * |
| 54 | + * @throws CasbinException |
| 55 | + */ |
| 56 | + public function loadFilteredPolicy(Model $model, $filter): void |
| 57 | + { |
| 58 | + // if $filter is empty, load all policies |
| 59 | + if (is_null($filter)) { |
| 60 | + $this->loadPolicy($model); |
| 61 | + return; |
| 62 | + } |
| 63 | + // validate $filter is a instance of Filter |
| 64 | + if (!$filter instanceof Filter) { |
| 65 | + throw new InvalidFilterTypeException('invalid filter type'); |
| 66 | + } |
| 67 | + $type = ''; |
| 68 | + $filter = (array) $filter; |
| 69 | + // choose which ptype to use |
| 70 | + foreach($filter as $i => $v) { |
| 71 | + if (!empty($v)) { |
| 72 | + array_unshift($filter[$i], $i); |
| 73 | + $type = $i; |
| 74 | + break; |
| 75 | + } |
| 76 | + } |
| 77 | + $sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName . ' WHERE '; |
| 78 | + $items = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5']; |
| 79 | + $temp = []; |
| 80 | + $values = []; |
| 81 | + foreach($items as $i => $item) { |
| 82 | + if (isset($filter[$type][$i]) && !empty($filter[$type][$i])) { |
| 83 | + array_push($temp, $item . '=:' . $item); |
| 84 | + $values[$item] = $filter[$type][$i]; |
| 85 | + } |
| 86 | + } |
| 87 | + $sql .= implode(' and ', $temp); |
| 88 | + $rows = $this->connection->query($sql, $values); |
| 89 | + foreach($rows as $row) { |
| 90 | + $line = implode(', ', $row); |
| 91 | + $this->loadPolicyLine($line, $model); |
| 92 | + } |
| 93 | + |
| 94 | + $this->filtered = true; |
| 95 | + } |
| 96 | + |
32 | 97 | public static function newAdapter(array $config)
|
33 | 98 | {
|
34 | 99 | return new static($config);
|
|
0 commit comments