|
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; |
| 12 | +use Closure; |
9 | 13 |
|
10 | 14 | /**
|
11 | 15 | * DatabaseAdapter.
|
12 | 16 | *
|
13 | 17 |
|
14 | 18 | */
|
15 |
| -class Adapter implements AdapterContract |
| 19 | +class Adapter implements AdapterContract, FilteredAdapter |
16 | 20 | {
|
17 | 21 | use AdapterHelper;
|
18 | 22 |
|
19 | 23 | protected $config;
|
20 | 24 |
|
| 25 | + protected $filtered; |
| 26 | + |
21 | 27 | protected $connection;
|
22 | 28 |
|
23 | 29 | public $casbinRuleTableName = 'casbin_rule';
|
24 | 30 |
|
| 31 | + public $rows = []; |
| 32 | + |
25 | 33 | public function __construct(array $config)
|
26 | 34 | {
|
27 | 35 | $this->config = $config;
|
| 36 | + $this->filtered = false; |
28 | 37 | $this->connection = (new Manager($config))->getConnection();
|
29 | 38 | $this->initTable();
|
30 | 39 | }
|
31 | 40 |
|
| 41 | + /** |
| 42 | + * Returns true if the loaded policy has been filtered. |
| 43 | + * |
| 44 | + * @return bool |
| 45 | + */ |
| 46 | + public function isFiltered(): bool |
| 47 | + { |
| 48 | + return $this->filtered; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Sets filtered parameter. |
| 53 | + * |
| 54 | + * @param bool $filtered |
| 55 | + */ |
| 56 | + public function setFiltered(bool $filtered): void |
| 57 | + { |
| 58 | + $this->filtered = $filtered; |
| 59 | + } |
| 60 | + |
32 | 61 | public static function newAdapter(array $config)
|
33 | 62 | {
|
34 | 63 | return new static($config);
|
@@ -154,4 +183,61 @@ public function removeFilteredPolicy(string $sec, string $ptype, int $fieldIndex
|
154 | 183 |
|
155 | 184 | $this->connection->execute($sql, $where);
|
156 | 185 | }
|
| 186 | + |
| 187 | + /** |
| 188 | + * Loads only policy rules that match the filter from storage. |
| 189 | + * |
| 190 | + * @param Model $model |
| 191 | + * @param mixed $filter |
| 192 | + * |
| 193 | + * @throws CasbinException |
| 194 | + */ |
| 195 | + public function loadFilteredPolicy(Model $model, $filter): void |
| 196 | + { |
| 197 | + // if $filter is empty, load all policies |
| 198 | + if (is_null($filter)) { |
| 199 | + $this->loadPolicy($model); |
| 200 | + return; |
| 201 | + } |
| 202 | + // the basic sql |
| 203 | + $sql = 'SELECT ptype, v0, v1, v2, v3, v4, v5 FROM '.$this->casbinRuleTableName . ' WHERE '; |
| 204 | + |
| 205 | + if ($filter instanceof Filter) { |
| 206 | + $type = ''; |
| 207 | + $filter = (array) $filter; |
| 208 | + // choose which ptype to use |
| 209 | + foreach($filter as $i => $v) { |
| 210 | + if (!empty($v)) { |
| 211 | + array_unshift($filter[$i], $i); |
| 212 | + $type = $i; |
| 213 | + break; |
| 214 | + } |
| 215 | + } |
| 216 | + $items = ['ptype', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5']; |
| 217 | + $temp = []; |
| 218 | + $values = []; |
| 219 | + foreach($items as $i => $item) { |
| 220 | + if (isset($filter[$type][$i]) && !empty($filter[$type][$i])) { |
| 221 | + array_push($temp, $item . '=:' . $item); |
| 222 | + $values[$item] = $filter[$type][$i]; |
| 223 | + } |
| 224 | + } |
| 225 | + $sql .= implode(' and ', $temp); |
| 226 | + $rows = $this->connection->query($sql, $values); |
| 227 | + } elseif (is_string($filter)) { |
| 228 | + $sql .= $filter; |
| 229 | + $rows = $this->connection->query($sql); |
| 230 | + } else if ($filter instanceof Closure) { |
| 231 | + $filter($this->connection, $sql, $this->rows); |
| 232 | + } else { |
| 233 | + throw new InvalidFilterTypeException('invalid filter type'); |
| 234 | + } |
| 235 | + |
| 236 | + $rows = $rows ?? $this->rows; |
| 237 | + foreach($rows as $row) { |
| 238 | + $line = implode(', ', $row); |
| 239 | + $this->loadPolicyLine($line, $model); |
| 240 | + } |
| 241 | + $this->filtered = true; |
| 242 | + } |
157 | 243 | }
|
0 commit comments