|
6 | 6 | use Casbin\Persist\Adapter as AdapterContract;
|
7 | 7 | use Casbin\Persist\BatchAdapter as BatchAdapterContract;
|
8 | 8 | use Casbin\Persist\UpdatableAdapter as UpdatableAdapterContract;
|
| 9 | +use Casbin\Persist\FilteredAdapter as FilteredAdapterContract; |
| 10 | +use Casbin\Model\Model; |
| 11 | +use Casbin\Persist\Adapters\Filter; |
| 12 | +use Casbin\Exceptions\InvalidFilterTypeException; |
9 | 13 | use Casbin\Persist\AdapterHelper;
|
10 | 14 | use Cake\ORM\TableRegistry;
|
11 | 15 | use CasbinAdapter\Cake\Model\Table\CasbinRuleTable;
|
|
15 | 19 | *
|
16 | 20 |
|
17 | 21 | */
|
18 |
| -class Adapter implements AdapterContract, BatchAdapterContract, UpdatableAdapterContract |
| 22 | +class Adapter implements AdapterContract, BatchAdapterContract, UpdatableAdapterContract, FilteredAdapterContract |
19 | 23 | {
|
20 | 24 | use AdapterHelper;
|
21 | 25 |
|
22 | 26 | protected $table;
|
23 | 27 |
|
| 28 | + /** |
| 29 | + * @var bool |
| 30 | + */ |
| 31 | + private $filtered = false; |
| 32 | + |
24 | 33 | public function __construct()
|
25 | 34 | {
|
26 | 35 | $this->table = $this->getTable();
|
@@ -159,6 +168,64 @@ public function updatePolicy(string $sec, string $ptype, array $oldRule, array $
|
159 | 168 | $this->table->save($first);
|
160 | 169 | }
|
161 | 170 |
|
| 171 | + /** |
| 172 | + * Loads only policy rules that match the filter. |
| 173 | + * |
| 174 | + * @param Model $model |
| 175 | + * @param mixed $filter |
| 176 | + */ |
| 177 | + public function loadFilteredPolicy(Model $model, $filter): void |
| 178 | + { |
| 179 | + $entity = $this->table->find(); |
| 180 | + |
| 181 | + if (is_string($filter)) { |
| 182 | + $entity = $entity->epilog('WHERE ' . $filter); |
| 183 | + } elseif ($filter instanceof Filter) { |
| 184 | + foreach ($filter->p as $k => $v) { |
| 185 | + $where[$v] = $filter->g[$k]; |
| 186 | + $entity = $entity->where([$v => $filter->g[$k]]); |
| 187 | + } |
| 188 | + } elseif ($filter instanceof \Closure) { |
| 189 | + $entity = $entity->where($filter); |
| 190 | + } else { |
| 191 | + throw new InvalidFilterTypeException('invalid filter type'); |
| 192 | + } |
| 193 | + $rows = $entity->all(); |
| 194 | + |
| 195 | + foreach ($rows as $row) { |
| 196 | + unset($row->id); |
| 197 | + $row = $row->toArray(); |
| 198 | + $row = array_filter($row, function ($value) { |
| 199 | + return !is_null($value) && $value !== ''; |
| 200 | + }); |
| 201 | + $line = implode(', ', array_filter($row, function ($val) { |
| 202 | + return '' != $val && !is_null($val); |
| 203 | + })); |
| 204 | + $this->loadPolicyLine(trim($line), $model); |
| 205 | + } |
| 206 | + $this->setFiltered(true); |
| 207 | + } |
| 208 | + |
| 209 | + /** |
| 210 | + * Returns true if the loaded policy has been filtered. |
| 211 | + * |
| 212 | + * @return bool |
| 213 | + */ |
| 214 | + public function isFiltered(): bool |
| 215 | + { |
| 216 | + return $this->filtered; |
| 217 | + } |
| 218 | + |
| 219 | + /** |
| 220 | + * Sets filtered parameter. |
| 221 | + * |
| 222 | + * @param bool $filtered |
| 223 | + */ |
| 224 | + public function setFiltered(bool $filtered): void |
| 225 | + { |
| 226 | + $this->filtered = $filtered; |
| 227 | + } |
| 228 | + |
162 | 229 | protected function getTable()
|
163 | 230 | {
|
164 | 231 | return TableRegistry::getTableLocator()->get('CasbinRule', [
|
|
0 commit comments