@@ -44,6 +44,26 @@ public function __construct(Rule $model)
44
44
$ this ->model = $ model ;
45
45
}
46
46
47
+ /**
48
+ * Filter the rule.
49
+ *
50
+ * @param array $rule
51
+ * @return array
52
+ */
53
+ public function filterRule (array $ rule ): array
54
+ {
55
+ $ rule = array_values ($ rule );
56
+
57
+ $ i = count ($ rule ) - 1 ;
58
+ for (; $ i >= 0 ; $ i --) {
59
+ if ($ rule [$ i ] != '' && !is_null ($ rule [$ i ])) {
60
+ break ;
61
+ }
62
+ }
63
+
64
+ return array_slice ($ rule , 0 , $ i + 1 );
65
+ }
66
+
47
67
/**
48
68
* savePolicyLine function.
49
69
*
@@ -176,32 +196,51 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
176
196
}
177
197
178
198
/**
179
- * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
180
- * This is part of the Auto-Save feature.
181
- *
182
- * @param string $sec
183
- * @param string $ptype
184
- * @param int $fieldIndex
185
- * @param string ...$fieldValues
199
+ * @param string $sec
200
+ * @param string $ptype
201
+ * @param int $fieldIndex
202
+ * @param string|null ...$fieldValues
203
+ * @return array
204
+ * @throws Throwable
186
205
*/
187
- public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , string ...$ fieldValues ): void
206
+ public function _removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , ? string ...$ fieldValues ): array
188
207
{
189
208
$ count = 0 ;
209
+ $ removedRules = [];
190
210
191
211
$ instance = $ this ->model ->where ('ptype ' , $ ptype );
192
212
foreach (range (0 , 5 ) as $ value ) {
193
213
if ($ fieldIndex <= $ value && $ value < $ fieldIndex + count ($ fieldValues )) {
194
214
if ('' != $ fieldValues [$ value - $ fieldIndex ]) {
195
- $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
215
+ $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
196
216
}
197
217
}
198
218
}
199
219
200
220
foreach ($ instance ->select () as $ model ) {
221
+ $ item = $ model ->hidden (['id ' , 'ptype ' ])->toArray ();
222
+ $ item = $ this ->filterRule ($ item );
223
+ $ removedRules [] = $ item ;
201
224
if ($ model ->cache ('tauthz ' )->delete ()) {
202
225
++$ count ;
203
226
}
204
227
}
228
+
229
+ return $ removedRules ;
230
+ }
231
+
232
+ /**
233
+ * RemoveFilteredPolicy removes policy rules that match the filter from the storage.
234
+ * This is part of the Auto-Save feature.
235
+ *
236
+ * @param string $sec
237
+ * @param string $ptype
238
+ * @param int $fieldIndex
239
+ * @param string|null ...$fieldValues
240
+ */
241
+ public function removeFilteredPolicy (string $ sec , string $ ptype , int $ fieldIndex , ?string ...$ fieldValues ): void
242
+ {
243
+ $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
205
244
}
206
245
207
246
/**
@@ -259,41 +298,13 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
259
298
*/
260
299
public function updateFilteredPolicies (string $ sec , string $ ptype , array $ newPolicies , int $ fieldIndex , string ...$ fieldValues ): array
261
300
{
262
- $ where ['ptype ' ] = $ ptype ;
263
- foreach ($ fieldValues as $ fieldValue ) {
264
- $ suffix = $ fieldIndex ++;
265
- if (!is_null ($ fieldValue ) && $ fieldValue !== '' ) {
266
- $ where ['v ' . $ suffix ] = $ fieldValue ;
267
- }
268
- }
269
-
270
- $ newP = [];
271
- $ oldP = [];
272
- foreach ($ newPolicies as $ newRule ) {
273
- $ col ['ptype ' ] = $ ptype ;
274
- foreach ($ newRule as $ key => $ value ) {
275
- $ col ['v ' . strval ($ key )] = $ value ;
276
- }
277
- $ newP [] = $ col ;
278
- }
279
-
280
- DB ::transaction (function () use ($ newP , $ where , &$ oldP ) {
281
- $ oldRules = $ this ->model ->where ($ where );
282
- $ oldP = $ oldRules ->select ()->hidden (['id ' ])->toArray ();
283
-
284
- foreach ($ oldP as &$ item ) {
285
- $ item = array_filter ($ item , function ($ value ) {
286
- return !is_null ($ value ) && $ value !== '' ;
287
- });
288
- unset($ item ['ptype ' ]);
289
- }
290
-
291
- $ oldRules ->delete ();
292
- $ this ->model ->insertAll ($ newP );
301
+ $ oldRules = [];
302
+ DB ::transaction (function () use ($ sec , $ ptype , $ fieldIndex , $ fieldValues , $ newPolicies , &$ oldRules ) {
303
+ $ oldRules = $ this ->_removeFilteredPolicy ($ sec , $ ptype , $ fieldIndex , ...$ fieldValues );
304
+ $ this ->addPolicies ($ sec , $ ptype , $ newPolicies );
293
305
});
294
306
295
- // return deleted rules
296
- return $ oldP ;
307
+ return $ oldRules ;
297
308
}
298
309
299
310
/**
0 commit comments