33namespace CasbinAdapter \Medoo ;
44
55use Casbin \Persist \Adapter as AdapterContract ;
6+ use Casbin \Persist \BatchAdapter as BatchAdapterContract ;
67use Casbin \Persist \AdapterHelper ;
78use Casbin \Model \Model ;
89use Medoo \Medoo ;
1213 *
13141415 */
15- class Adapter implements AdapterContract
16+ class Adapter implements AdapterContract, BatchAdapterContract
1617{
1718 use AdapterHelper;
1819
@@ -78,7 +79,7 @@ public function initTable()
7879 *
7980 * @param string $ptype
8081 * @param array $rule
81- *
82+ *
8283 * @return void
8384 */
8485 public function savePolicyLine (string $ ptype , array $ rule ): void
@@ -95,7 +96,7 @@ public function savePolicyLine(string $ptype, array $rule): void
9596 * loads all policy rules from the storage.
9697 *
9798 * @param Model $model
98- *
99+ *
99100 * @return void
100101 */
101102 public function loadPolicy (Model $ model ): void
@@ -145,6 +146,30 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
145146 $ this ->savePolicyLine ($ ptype , $ rule );
146147 }
147148
149+ /**
150+ * Adds a policy rules to the storage.
151+ * This is part of the Auto-Save feature.
152+ *
153+ * @param string $sec
154+ * @param string $ptype
155+ * @param string[][] $rules
156+ */
157+ public function addPolicies (string $ sec , string $ ptype , array $ rules ): void
158+ {
159+ $ cols = [];
160+ $ i = 0 ;
161+
162+ foreach ($ rules as $ rule ) {
163+ $ temp ['ptype ' ] = $ ptype ;
164+ foreach ($ rule as $ key => $ value ) {
165+ $ temp ['v ' . strval ($ key )] = $ value ;
166+ }
167+ $ cols [$ i ++] = $ temp ?? [];
168+ $ temp = [];
169+ }
170+ $ this ->database ->insert ($ this ->casbinRuleTableName , $ cols );
171+ }
172+
148173 /**
149174 * This is part of the Auto-Save feature.
150175 *
@@ -165,6 +190,23 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
165190 $ this ->database ->delete ($ this ->casbinRuleTableName , ['AND ' => $ where ]);
166191 }
167192
193+ /**
194+ * Removes policy rules from the storage.
195+ * This is part of the Auto-Save feature.
196+ *
197+ * @param string $sec
198+ * @param string $ptype
199+ * @param string[][] $rules
200+ */
201+ public function removePolicies (string $ sec , string $ ptype , array $ rules ): void
202+ {
203+ $ this ->database ->action (function () use ($ sec , $ ptype , $ rules ) {
204+ foreach ($ rules as $ rule ) {
205+ $ this ->removePolicy ($ sec , $ ptype , $ rule );
206+ }
207+ });
208+ }
209+
168210 /**
169211 * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
170212 * This is part of the Auto-Save feature.
0 commit comments