3
3
namespace CasbinAdapter \LaminasDb ;
4
4
5
5
use Casbin \Persist \Adapter as AdapterContract ;
6
+ use Casbin \Persist \BatchAdapter as BatchAdapterContract ;
6
7
use Casbin \Persist \AdapterHelper ;
7
8
use Laminas \Db \Adapter \AdapterInterface as LaminasDbAdapterInterface ;
8
9
use Laminas \Db \Adapter \Adapter as LaminasDbAdapter ;
16
17
*
17
18
18
19
*/
19
- class Adapter implements AdapterContract
20
+ class Adapter implements AdapterContract, BatchAdapterContract
20
21
{
21
22
use AdapterHelper;
22
23
@@ -25,6 +26,11 @@ class Adapter implements AdapterContract
25
26
*/
26
27
protected $ tableGateway ;
27
28
29
+ /**
30
+ * @var LaminasDbAdapterInterface
31
+ */
32
+ protected $ dbAdapter ;
33
+
28
34
/**
29
35
* default table name.
30
36
*
@@ -144,6 +150,35 @@ public function addPolicy($sec, $ptype, $rule): void
144
150
$ this ->savePolicyLine ($ ptype , $ rule );
145
151
}
146
152
153
+ /**
154
+ * Adds a policy rules to the storage.
155
+ * This is part of the Auto-Save feature.
156
+ *
157
+ * @param string $sec
158
+ * @param string $ptype
159
+ * @param string[][] $rules
160
+ */
161
+ public function addPolicies (string $ sec , string $ ptype , array $ rules ): void
162
+ {
163
+ $ columns = ['ptype ' , 'v0 ' , 'v1 ' , 'v2 ' , 'v3 ' , 'v4 ' , 'v5 ' ];
164
+ $ values = [];
165
+ $ sets = [];
166
+ $ columnsCount = count ($ columns );
167
+ foreach ($ rules as $ rule ) {
168
+ array_unshift ($ rule , $ ptype );
169
+ $ values = array_merge ($ values , array_pad ($ rule , $ columnsCount , null ));
170
+ $ sets [] = array_pad ([], $ columnsCount , '? ' );
171
+ }
172
+ $ valuesStr = implode (', ' , array_map (function ($ set ) {
173
+ return '( ' . implode (', ' , $ set ) . ') ' ;
174
+ }, $ sets ));
175
+ $ sql = 'INSERT INTO ' . $ this ->casbinRuleTableName . ' ( ' . implode (', ' , $ columns ) . ') ' . ' VALUES ' . $ valuesStr ;
176
+
177
+ $ driver = $ this ->tableGateway ->adapter ->getDriver ();
178
+ $ statement = $ driver ->createStatement ($ sql );
179
+ $ result = $ statement ->execute ($ values );
180
+ }
181
+
147
182
/**
148
183
* This is part of the Auto-Save feature.
149
184
*
@@ -161,6 +196,23 @@ public function removePolicy($sec, $ptype, $rule): void
161
196
$ this ->tableGateway ->delete ($ where );
162
197
}
163
198
199
+ /**
200
+ * Removes policy rules from the storage.
201
+ * This is part of the Auto-Save feature.
202
+ *
203
+ * @param string $sec
204
+ * @param string $ptype
205
+ * @param string[][] $rules
206
+ */
207
+ public function removePolicies (string $ sec , string $ ptype , array $ rules ): void
208
+ {
209
+ $ this ->tableGateway ->adapter ->getDriver ()->getConnection ()->beginTransaction (function () use ($ sec , $ ptype , $ rules ) {
210
+ foreach ($ rules as $ rule ) {
211
+ $ this ->removePolicy ($ sec , $ ptype , $ rule );
212
+ }
213
+ });
214
+ }
215
+
164
216
/**
165
217
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
166
218
* This is part of the Auto-Save feature.
0 commit comments