@@ -375,47 +375,47 @@ func (e *ContextEnforcer) UpdateNamedGroupingPoliciesCtx(ctx context.Context, pt
375375
376376// SelfAddPolicyCtx adds a policy rule to the current policy with context.
377377func (e * ContextEnforcer ) SelfAddPolicyCtx (ctx context.Context , sec string , ptype string , rule []string ) (bool , error ) {
378- return e .addPolicyWithoutNotifyCtx (ctx , sec , ptype , rule )
378+ return e .addPolicyWithoutNotifyCtx (ctx , nil , sec , ptype , rule )
379379}
380380
381381// SelfAddPoliciesCtx adds policy rules to the current policy with context.
382382func (e * ContextEnforcer ) SelfAddPoliciesCtx (ctx context.Context , sec string , ptype string , rules [][]string ) (bool , error ) {
383- return e .addPoliciesWithoutNotifyCtx (ctx , sec , ptype , rules , false )
383+ return e .addPoliciesWithoutNotifyCtx (ctx , nil , sec , ptype , rules , false )
384384}
385385
386386func (e * ContextEnforcer ) SelfAddPoliciesExCtx (ctx context.Context , sec string , ptype string , rules [][]string ) (bool , error ) {
387- return e .addPoliciesWithoutNotifyCtx (ctx , sec , ptype , rules , true )
387+ return e .addPoliciesWithoutNotifyCtx (ctx , nil , sec , ptype , rules , true )
388388}
389389
390390// SelfRemovePolicyCtx removes a policy rule from the current policy with context.
391391func (e * ContextEnforcer ) SelfRemovePolicyCtx (ctx context.Context , sec string , ptype string , rule []string ) (bool , error ) {
392- return e .removePolicyWithoutNotifyCtx (ctx , sec , ptype , rule )
392+ return e .removePolicyWithoutNotifyCtx (ctx , nil , sec , ptype , rule )
393393}
394394
395395// SelfRemovePoliciesCtx removes policy rules from the current policy with context.
396396func (e * ContextEnforcer ) SelfRemovePoliciesCtx (ctx context.Context , sec string , ptype string , rules [][]string ) (bool , error ) {
397- return e .removePoliciesWithoutNotifyCtx (ctx , sec , ptype , rules )
397+ return e .removePoliciesWithoutNotifyCtx (ctx , nil , sec , ptype , rules )
398398}
399399
400400// SelfRemoveFilteredPolicyCtx removes policy rules that match the filter from the current policy with context.
401401func (e * ContextEnforcer ) SelfRemoveFilteredPolicyCtx (ctx context.Context , sec string , ptype string , fieldIndex int , fieldValues ... string ) (bool , error ) {
402- return e .removeFilteredPolicyWithoutNotifyCtx (ctx , sec , ptype , fieldIndex , fieldValues )
402+ return e .removeFilteredPolicyWithoutNotifyCtx (ctx , nil , sec , ptype , fieldIndex , fieldValues )
403403}
404404
405405// SelfUpdatePolicyCtx updates a policy rule in the current policy with context.
406406func (e * ContextEnforcer ) SelfUpdatePolicyCtx (ctx context.Context , sec string , ptype string , oldRule , newRule []string ) (bool , error ) {
407- return e .updatePolicyWithoutNotifyCtx (ctx , sec , ptype , oldRule , newRule )
407+ return e .updatePolicyWithoutNotifyCtx (ctx , nil , sec , ptype , oldRule , newRule )
408408}
409409
410410// SelfUpdatePoliciesCtx updates policy rules in the current policy with context.
411411func (e * ContextEnforcer ) SelfUpdatePoliciesCtx (ctx context.Context , sec string , ptype string , oldRules , newRules [][]string ) (bool , error ) {
412- return e .updatePoliciesWithoutNotifyCtx (ctx , sec , ptype , oldRules , newRules )
412+ return e .updatePoliciesWithoutNotifyCtx (ctx , nil , sec , ptype , oldRules , newRules )
413413}
414414
415415// Internal API methods with context support
416416
417417// addPolicyWithoutNotifyCtx adds a rule to the current policy with context.
418- func (e * ContextEnforcer ) addPolicyWithoutNotifyCtx (ctx context.Context , sec string , ptype string , rule []string ) (bool , error ) {
418+ func (e * ContextEnforcer ) addPolicyWithoutNotifyCtx (ctx context.Context , shouldPersist func () bool , sec string , ptype string , rule []string ) (bool , error ) {
419419 if e .dispatcher != nil && e .autoNotifyDispatcher {
420420 return true , e .dispatcher .AddPolicies (sec , ptype , [][]string {rule })
421421 }
@@ -425,7 +425,7 @@ func (e *ContextEnforcer) addPolicyWithoutNotifyCtx(ctx context.Context, sec str
425425 return false , err
426426 }
427427
428- if e . shouldPersist () {
428+ if shouldPersist != nil && shouldPersist () {
429429 if err = e .adapterCtx .AddPolicyCtx (ctx , sec , ptype , rule ); err != nil {
430430 if err .Error () != notImplemented {
431431 return false , err
@@ -449,7 +449,7 @@ func (e *ContextEnforcer) addPolicyWithoutNotifyCtx(ctx context.Context, sec str
449449}
450450
451451// addPoliciesWithoutNotifyCtx adds rules to the current policy with context.
452- func (e * ContextEnforcer ) addPoliciesWithoutNotifyCtx (ctx context.Context , sec string , ptype string , rules [][]string , autoRemoveRepeat bool ) (bool , error ) {
452+ func (e * ContextEnforcer ) addPoliciesWithoutNotifyCtx (ctx context.Context , shouldPersist func () bool , sec string , ptype string , rules [][]string , autoRemoveRepeat bool ) (bool , error ) {
453453 if e .dispatcher != nil && e .autoNotifyDispatcher {
454454 return true , e .dispatcher .AddPolicies (sec , ptype , rules )
455455 }
@@ -461,7 +461,7 @@ func (e *ContextEnforcer) addPoliciesWithoutNotifyCtx(ctx context.Context, sec s
461461 }
462462 }
463463
464- if e . shouldPersist () {
464+ if shouldPersist != nil && shouldPersist () {
465465 if err := e .adapterCtx .(persist.ContextBatchAdapter ).AddPoliciesCtx (ctx , sec , ptype , rules ); err != nil {
466466 if err .Error () != notImplemented {
467467 return false , err
@@ -490,12 +490,12 @@ func (e *ContextEnforcer) addPoliciesWithoutNotifyCtx(ctx context.Context, sec s
490490}
491491
492492// removePolicyWithoutNotifyCtx removes a rule from the current policy with context.
493- func (e * ContextEnforcer ) removePolicyWithoutNotifyCtx (ctx context.Context , sec string , ptype string , rule []string ) (bool , error ) {
493+ func (e * ContextEnforcer ) removePolicyWithoutNotifyCtx (ctx context.Context , shouldPersist func () bool , sec string , ptype string , rule []string ) (bool , error ) {
494494 if e .dispatcher != nil && e .autoNotifyDispatcher {
495495 return true , e .dispatcher .RemovePolicies (sec , ptype , [][]string {rule })
496496 }
497497
498- if e . shouldPersist () {
498+ if shouldPersist != nil && shouldPersist () {
499499 if err := e .adapterCtx .RemovePolicyCtx (ctx , sec , ptype , rule ); err != nil {
500500 if err .Error () != notImplemented {
501501 return false , err
@@ -519,7 +519,7 @@ func (e *ContextEnforcer) removePolicyWithoutNotifyCtx(ctx context.Context, sec
519519}
520520
521521// removePoliciesWithoutNotifyCtx removes rules from the current policy with context.
522- func (e * ContextEnforcer ) removePoliciesWithoutNotifyCtx (ctx context.Context , sec string , ptype string , rules [][]string ) (bool , error ) {
522+ func (e * ContextEnforcer ) removePoliciesWithoutNotifyCtx (ctx context.Context , shouldPersist func () bool , sec string , ptype string , rules [][]string ) (bool , error ) {
523523 if hasPolicies , err := e .model .HasPolicies (sec , ptype , rules ); ! hasPolicies || err != nil {
524524 return hasPolicies , err
525525 }
@@ -528,7 +528,7 @@ func (e *ContextEnforcer) removePoliciesWithoutNotifyCtx(ctx context.Context, se
528528 return true , e .dispatcher .RemovePolicies (sec , ptype , rules )
529529 }
530530
531- if e . shouldPersist () {
531+ if shouldPersist != nil && shouldPersist () {
532532 if err := e .adapterCtx .(persist.ContextBatchAdapter ).RemovePoliciesCtx (ctx , sec , ptype , rules ); err != nil {
533533 if err .Error () != notImplemented {
534534 return false , err
@@ -551,7 +551,7 @@ func (e *ContextEnforcer) removePoliciesWithoutNotifyCtx(ctx context.Context, se
551551}
552552
553553// removeFilteredPolicyWithoutNotifyCtx removes policy rules that match the filter from the current policy with context.
554- func (e * ContextEnforcer ) removeFilteredPolicyWithoutNotifyCtx (ctx context.Context , sec string , ptype string , fieldIndex int , fieldValues []string ) (bool , error ) {
554+ func (e * ContextEnforcer ) removeFilteredPolicyWithoutNotifyCtx (ctx context.Context , shouldPersist func () bool , sec string , ptype string , fieldIndex int , fieldValues []string ) (bool , error ) {
555555 if len (fieldValues ) == 0 {
556556 return false , Err .ErrInvalidFieldValuesParameter
557557 }
@@ -560,7 +560,7 @@ func (e *ContextEnforcer) removeFilteredPolicyWithoutNotifyCtx(ctx context.Conte
560560 return true , e .dispatcher .RemoveFilteredPolicy (sec , ptype , fieldIndex , fieldValues ... )
561561 }
562562
563- if e . shouldPersist () {
563+ if shouldPersist != nil && shouldPersist () {
564564 if err := e .adapterCtx .RemoveFilteredPolicyCtx (ctx , sec , ptype , fieldIndex , fieldValues ... ); err != nil {
565565 if err .Error () != notImplemented {
566566 return false , err
@@ -584,12 +584,12 @@ func (e *ContextEnforcer) removeFilteredPolicyWithoutNotifyCtx(ctx context.Conte
584584}
585585
586586// updatePolicyWithoutNotifyCtx updates a policy rule in the current policy with context.
587- func (e * ContextEnforcer ) updatePolicyWithoutNotifyCtx (ctx context.Context , sec string , ptype string , oldRule , newRule []string ) (bool , error ) {
587+ func (e * ContextEnforcer ) updatePolicyWithoutNotifyCtx (ctx context.Context , shouldPersist func () bool , sec string , ptype string , oldRule , newRule []string ) (bool , error ) {
588588 if e .dispatcher != nil && e .autoNotifyDispatcher {
589589 return true , e .dispatcher .UpdatePolicy (sec , ptype , oldRule , newRule )
590590 }
591591
592- if e . shouldPersist () {
592+ if shouldPersist != nil && shouldPersist () {
593593 if err := e .adapterCtx .(persist.ContextUpdatableAdapter ).UpdatePolicyCtx (ctx , sec , ptype , oldRule , newRule ); err != nil {
594594 if err .Error () != notImplemented {
595595 return false , err
@@ -615,7 +615,7 @@ func (e *ContextEnforcer) updatePolicyWithoutNotifyCtx(ctx context.Context, sec
615615 return ruleUpdated , nil
616616}
617617
618- func (e * ContextEnforcer ) updatePoliciesWithoutNotifyCtx (ctx context.Context , sec string , ptype string , oldRules [][]string , newRules [][]string ) (bool , error ) {
618+ func (e * ContextEnforcer ) updatePoliciesWithoutNotifyCtx (ctx context.Context , shouldPersist func () bool , sec string , ptype string , oldRules [][]string , newRules [][]string ) (bool , error ) {
619619 if len (newRules ) != len (oldRules ) {
620620 return false , fmt .Errorf ("the length of oldRules should be equal to the length of newRules, but got the length of oldRules is %d, the length of newRules is %d" , len (oldRules ), len (newRules ))
621621 }
@@ -624,7 +624,7 @@ func (e *ContextEnforcer) updatePoliciesWithoutNotifyCtx(ctx context.Context, se
624624 return true , e .dispatcher .UpdatePolicies (sec , ptype , oldRules , newRules )
625625 }
626626
627- if e . shouldPersist () {
627+ if shouldPersist != nil && shouldPersist () {
628628 if err := e .adapterCtx .(persist.ContextUpdatableAdapter ).UpdatePoliciesCtx (ctx , sec , ptype , oldRules , newRules ); err != nil {
629629 if err .Error () != notImplemented {
630630 return false , err
@@ -652,7 +652,7 @@ func (e *ContextEnforcer) updatePoliciesWithoutNotifyCtx(ctx context.Context, se
652652}
653653
654654func (e * ContextEnforcer ) addPolicyCtx (ctx context.Context , sec string , ptype string , rule []string ) (bool , error ) {
655- ok , err := e .addPolicyWithoutNotifyCtx (ctx , sec , ptype , rule )
655+ ok , err := e .addPolicyWithoutNotifyCtx (ctx , e . shouldPersist , sec , ptype , rule )
656656 if ! ok || err != nil {
657657 return ok , err
658658 }
@@ -671,7 +671,7 @@ func (e *ContextEnforcer) addPolicyCtx(ctx context.Context, sec string, ptype st
671671}
672672
673673func (e * ContextEnforcer ) addPoliciesCtx (ctx context.Context , sec string , ptype string , rules [][]string , autoRemoveRepeat bool ) (bool , error ) {
674- ok , err := e .addPoliciesWithoutNotifyCtx (ctx , sec , ptype , rules , autoRemoveRepeat )
674+ ok , err := e .addPoliciesWithoutNotifyCtx (ctx , e . shouldPersist , sec , ptype , rules , autoRemoveRepeat )
675675 if ! ok || err != nil {
676676 return ok , err
677677 }
@@ -690,7 +690,7 @@ func (e *ContextEnforcer) addPoliciesCtx(ctx context.Context, sec string, ptype
690690}
691691
692692func (e * ContextEnforcer ) updatePolicyCtx (ctx context.Context , sec string , ptype string , oldRule []string , newRule []string ) (bool , error ) {
693- ok , err := e .updatePolicyWithoutNotifyCtx (ctx , sec , ptype , oldRule , newRule )
693+ ok , err := e .updatePolicyWithoutNotifyCtx (ctx , e . shouldPersist , sec , ptype , oldRule , newRule )
694694 if ! ok || err != nil {
695695 return ok , err
696696 }
@@ -709,7 +709,7 @@ func (e *ContextEnforcer) updatePolicyCtx(ctx context.Context, sec string, ptype
709709}
710710
711711func (e * ContextEnforcer ) updatePoliciesCtx (ctx context.Context , sec string , ptype string , oldRules [][]string , newRules [][]string ) (bool , error ) {
712- ok , err := e .updatePoliciesWithoutNotifyCtx (ctx , sec , ptype , oldRules , newRules )
712+ ok , err := e .updatePoliciesWithoutNotifyCtx (ctx , e . shouldPersist , sec , ptype , oldRules , newRules )
713713 if ! ok || err != nil {
714714 return ok , err
715715 }
@@ -728,7 +728,7 @@ func (e *ContextEnforcer) updatePoliciesCtx(ctx context.Context, sec string, pty
728728}
729729
730730func (e * ContextEnforcer ) removePolicyCtx (ctx context.Context , sec string , ptype string , rule []string ) (bool , error ) {
731- ok , err := e .removePolicyWithoutNotifyCtx (ctx , sec , ptype , rule )
731+ ok , err := e .removePolicyWithoutNotifyCtx (ctx , e . shouldPersist , sec , ptype , rule )
732732 if ! ok || err != nil {
733733 return ok , err
734734 }
@@ -747,7 +747,7 @@ func (e *ContextEnforcer) removePolicyCtx(ctx context.Context, sec string, ptype
747747}
748748
749749func (e * ContextEnforcer ) removePoliciesCtx (ctx context.Context , sec string , ptype string , rules [][]string ) (bool , error ) {
750- ok , err := e .removePoliciesWithoutNotifyCtx (ctx , sec , ptype , rules )
750+ ok , err := e .removePoliciesWithoutNotifyCtx (ctx , e . shouldPersist , sec , ptype , rules )
751751 if ! ok || err != nil {
752752 return ok , err
753753 }
@@ -767,7 +767,7 @@ func (e *ContextEnforcer) removePoliciesCtx(ctx context.Context, sec string, pty
767767
768768// removeFilteredPolicy removes rules based on field filters from the current policy.
769769func (e * ContextEnforcer ) removeFilteredPolicyCtx (ctx context.Context , sec string , ptype string , fieldIndex int , fieldValues []string ) (bool , error ) {
770- ok , err := e .removeFilteredPolicyWithoutNotifyCtx (ctx , sec , ptype , fieldIndex , fieldValues )
770+ ok , err := e .removeFilteredPolicyWithoutNotifyCtx (ctx , e . shouldPersist , sec , ptype , fieldIndex , fieldValues )
771771 if ! ok || err != nil {
772772 return ok , err
773773 }
0 commit comments