33namespace CasbinAdapter \LaminasDb ;
44
55use Casbin \Persist \Adapter as AdapterContract ;
6+ use Casbin \Persist \BatchAdapter as BatchAdapterContract ;
67use Casbin \Persist \AdapterHelper ;
78use Laminas \Db \Adapter \AdapterInterface as LaminasDbAdapterInterface ;
89use Laminas \Db \Adapter \Adapter as LaminasDbAdapter ;
1617 *
17181819 */
19- class Adapter implements AdapterContract
20+ class Adapter implements AdapterContract, BatchAdapterContract
2021{
2122 use AdapterHelper;
2223
@@ -25,6 +26,11 @@ class Adapter implements AdapterContract
2526 */
2627 protected $ tableGateway ;
2728
29+ /**
30+ * @var LaminasDbAdapterInterface
31+ */
32+ protected $ dbAdapter ;
33+
2834 /**
2935 * default table name.
3036 *
@@ -144,6 +150,35 @@ public function addPolicy($sec, $ptype, $rule): void
144150 $ this ->savePolicyLine ($ ptype , $ rule );
145151 }
146152
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+
147182 /**
148183 * This is part of the Auto-Save feature.
149184 *
@@ -161,6 +196,23 @@ public function removePolicy($sec, $ptype, $rule): void
161196 $ this ->tableGateway ->delete ($ where );
162197 }
163198
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+
164216 /**
165217 * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
166218 * This is part of the Auto-Save feature.
0 commit comments