|
9 | 9 | use Casbin\Persist\FilteredAdapter;
|
10 | 10 | use Casbin\Persist\Adapters\Filter;
|
11 | 11 | use Casbin\Exceptions\InvalidFilterTypeException;
|
| 12 | +use Casbin\Persist\BatchAdapter; |
12 | 13 | use Closure;
|
| 14 | +use Throwable; |
13 | 15 |
|
14 | 16 | /**
|
15 | 17 | * DatabaseAdapter.
|
16 | 18 | *
|
17 | 19 |
|
18 | 20 | */
|
19 |
| -class Adapter implements AdapterContract, FilteredAdapter |
| 21 | +class Adapter implements AdapterContract, FilteredAdapter, BatchAdapter |
20 | 22 | {
|
21 | 23 | use AdapterHelper;
|
22 | 24 |
|
@@ -136,6 +138,38 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
|
136 | 138 | $this->savePolicyLine($ptype, $rule);
|
137 | 139 | }
|
138 | 140 |
|
| 141 | + public function addPolicies(string $sec, string $ptype, array $rules): void |
| 142 | + { |
| 143 | + $table = $this->casbinRuleTableName; |
| 144 | + $columns = ['p_type', 'v0', 'v1', 'v2', 'v3', 'v4', 'v5']; |
| 145 | + $values = []; |
| 146 | + $sets = []; |
| 147 | + $columnsCount = count($columns); |
| 148 | + foreach ($rules as $rule) { |
| 149 | + $values = array_merge($values, array_pad($rule, $columnsCount, null)); |
| 150 | + $sets[] = array_pad([], $columnsCount, '?'); |
| 151 | + } |
| 152 | + $valuesStr = implode(', ', array_map(function ($set) { |
| 153 | + return '(' . implode(', ', $set) . ')'; |
| 154 | + }, $sets)); |
| 155 | + $sql = 'INSERT INTO ' . $table . ' (' . implode(', ', $columns) . ')' . |
| 156 | + ' VALUES' . $valuesStr; |
| 157 | + } |
| 158 | + |
| 159 | + public function removePolicies(string $sec, string $ptype, array $rules): void |
| 160 | + { |
| 161 | + $this->connection->getPdo()->beginTransaction(); |
| 162 | + try { |
| 163 | + foreach($rules as $rule) { |
| 164 | + $this->removePolicy($sec, $ptype, $rule); |
| 165 | + } |
| 166 | + $this->connection->getPdo()->commit(); |
| 167 | + } catch (Throwable $e){ |
| 168 | + $this->connection->getPdo()->rollback(); |
| 169 | + throw $e; |
| 170 | + } |
| 171 | + } |
| 172 | + |
139 | 173 | /**
|
140 | 174 | * This is part of the Auto-Save feature.
|
141 | 175 | *
|
|
0 commit comments