@@ -44,6 +44,26 @@ public function __construct(Rule $model)
4444 $ this ->model = $ model ;
4545 }
4646
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+
4767 /**
4868 * savePolicyLine function.
4969 *
@@ -176,32 +196,51 @@ public function removePolicies(string $sec, string $ptype, array $rules): void
176196 }
177197
178198 /**
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
186205 */
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
188207 {
189208 $ count = 0 ;
209+ $ removedRules = [];
190210
191211 $ instance = $ this ->model ->where ('ptype ' , $ ptype );
192212 foreach (range (0 , 5 ) as $ value ) {
193213 if ($ fieldIndex <= $ value && $ value < $ fieldIndex + count ($ fieldValues )) {
194214 if ('' != $ fieldValues [$ value - $ fieldIndex ]) {
195- $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
215+ $ instance ->where ('v ' . strval ($ value ), $ fieldValues [$ value - $ fieldIndex ]);
196216 }
197217 }
198218 }
199219
200220 foreach ($ instance ->select () as $ model ) {
221+ $ item = $ model ->hidden (['id ' , 'ptype ' ])->toArray ();
222+ $ item = $ this ->filterRule ($ item );
223+ $ removedRules [] = $ item ;
201224 if ($ model ->cache ('tauthz ' )->delete ()) {
202225 ++$ count ;
203226 }
204227 }
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 );
205244 }
206245
207246 /**
@@ -259,41 +298,13 @@ public function updatePolicies(string $sec, string $ptype, array $oldRules, arra
259298 */
260299 public function updateFilteredPolicies (string $ sec , string $ ptype , array $ newPolicies , int $ fieldIndex , string ...$ fieldValues ): array
261300 {
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 );
293305 });
294306
295- // return deleted rules
296- return $ oldP ;
307+ return $ oldRules ;
297308 }
298309
299310 /**
0 commit comments