6
6
7
7
use Lauthz \Models \Rule ;
8
8
use Lauthz \Contracts \DatabaseAdapter as DatabaseAdapterContract ;
9
+ use Lauthz \Contracts \BatchDatabaseAdapter as BatchDatabaseAdapterContract ;
9
10
use Casbin \Model \Model ;
10
11
use Casbin \Persist \AdapterHelper ;
11
-
12
+ use DateTime ;
12
13
/**
13
14
* DatabaseAdapter.
14
15
*
15
16
* @author techlee@qq.com
16
17
*/
17
- class DatabaseAdapter implements DatabaseAdapterContract
18
+ class DatabaseAdapter implements DatabaseAdapterContract, BatchDatabaseAdapterContract
18
19
{
19
20
use AdapterHelper;
20
21
@@ -101,6 +102,32 @@ 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 ['p_type ' ] = $ ptype ;
120
+ $ temp ['created_at ' ] = new DateTime ();
121
+ $ temp ['updated_at ' ] = $ temp ['created_at ' ];
122
+ foreach ($ rule as $ key => $ value ) {
123
+ $ temp ['v ' .strval ($ key )] = $ value ;
124
+ }
125
+ $ cols [$ i ++] = $ temp ?? [];
126
+ $ temp = [];
127
+ }
128
+ $ this ->eloquent ->insert ($ cols );
129
+ }
130
+
104
131
/**
105
132
* This is part of the Auto-Save feature.
106
133
*
@@ -125,6 +152,33 @@ public function removePolicy(string $sec, string $ptype, array $rule): void
125
152
}
126
153
}
127
154
155
+ /**
156
+ * Removes policy rules from the storage.
157
+ * This is part of the Auto-Save feature.
158
+ *
159
+ * @param string $sec
160
+ * @param string $ptype
161
+ * @param string[][] $rules
162
+ */
163
+ public function removePolicies (string $ sec , string $ ptype , array $ rules ): void
164
+ {
165
+ $ count = 0 ;
166
+ $ instance = $ this ->eloquent ->where ('p_type ' , $ ptype );
167
+ foreach ($ rules as $ rule )
168
+ {
169
+ foreach ($ rule as $ key => $ value ) {
170
+ $ keys [] = 'v ' .strval ($ key );
171
+ $ con ['v ' .strval ($ key )][] = $ value ;
172
+ }
173
+ }
174
+ $ keys = array_unique ($ keys );
175
+ foreach ($ keys as $ key ){
176
+ $ instance ->whereIn ($ key , $ con [$ key ]);
177
+ }
178
+ $ num = $ instance ->delete ();
179
+ $ count += $ num ;
180
+ }
181
+
128
182
/**
129
183
* RemoveFilteredPolicy removes policy rules that match the filter from the storage.
130
184
* This is part of the Auto-Save feature.
0 commit comments