7
7
use Casbin \Persist \Adapter ;
8
8
use Casbin \Persist \AdapterHelper ;
9
9
use Casbin \Persist \UpdatableAdapter ;
10
+ use Casbin \Persist \BatchAdapter ;
10
11
use think \facade \Db ;
11
12
12
13
/**
13
14
* DatabaseAdapter.
14
15
*
15
16
16
17
*/
17
- class DatabaseAdapter implements Adapter, UpdatableAdapter
18
+ class DatabaseAdapter implements Adapter, UpdatableAdapter, BatchAdapter
18
19
{
19
20
use AdapterHelper;
20
21
@@ -101,6 +102,30 @@ public function addPolicy(string $sec, string $ptype, array $rule): void
101
102
$ this ->savePolicyLine ($ ptype , $ rule );
102
103
}
103
104
105
+ /**
106
+ * Adds a policy rules to the storage.
107
+ * This is part of the Auto-Save feature.
108
+ *
109
+ * @param string $sec
110
+ * @param string $ptype
111
+ * @param string[][] $rules
112
+ */
113
+ public function addPolicies (string $ sec , string $ ptype , array $ rules ): void
114
+ {
115
+ $ cols = [];
116
+ $ i = 0 ;
117
+
118
+ foreach ($ rules as $ rule ) {
119
+ $ temp ['ptype ' ] = $ ptype ;
120
+ foreach ($ rule as $ key => $ value ) {
121
+ $ temp ['v ' . strval ($ key )] = $ value ;
122
+ }
123
+ $ cols [$ i ++] = $ temp ;
124
+ $ temp = [];
125
+ }
126
+ $ this ->model ->cache ('tauthz ' )->insertAll ($ cols );
127
+ }
128
+
104
129
/**
105
130
* This is part of the Auto-Save feature.
106
131
*
@@ -125,6 +150,23 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
125
150
}
126
151
}
127
152
153
+ /**
154
+ * Removes policy rules from 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 removePolicies (string $ sec , string $ ptype , array $ rules ): void
162
+ {
163
+ Db::transaction (function () use ($ sec , $ ptype , $ rules ) {
164
+ foreach ($ rules as $ rule ) {
165
+ $ this ->removePolicy ($ sec , $ ptype , $ rule );
166
+ }
167
+ });
168
+ }
169
+
128
170
/**
129
171
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
130
172
* This is part of the Auto-Save feature.
0 commit comments