1515package internal
1616
1717import (
18+ "github.com/casbin/casbin/v3/api"
1819 "github.com/casbin/casbin/v3/errors"
1920 "github.com/casbin/casbin/v3/model"
2021 "github.com/casbin/casbin/v3/persist"
2122 "github.com/casbin/casbin/v3/rbac"
2223)
2324
24- // PolicyManager is the policy manager for model and adapter
25- type PolicyManager interface {
26- AddPolicy (sec string , ptype string , rule []string , shouldPersist bool ) (bool , error )
27- AddPolicies (sec string , ptype string , rules [][]string , shouldPersist bool ) (bool , [][]string , error )
28- RemovePolicy (sec string , ptype string , rule []string , shouldPersist bool ) (bool , error )
29- RemovePolicies (sec string , ptype string , rules [][]string , shouldPersist bool ) (bool , [][]string , error )
30- RemoveFilteredPolicy (sec string , ptype string , shouldPersist bool , fieldIndex int , fieldValues ... string ) (bool , [][]string , error )
31- }
25+ var _ api.PolicyManager = & policyManager {}
3226
3327type policyManager struct {
34- model model.Model
35- adapter persist.Adapter
36- rm rbac.RoleManager
28+ model model.Model
29+ adapter persist.Adapter
30+ rm rbac.RoleManager
31+ shouldPersist func () bool
3732}
3833
3934const (
4035 notImplemented = "not implemented"
4136)
4237
4338// NewPolicyManager is the constructor for PolicyManager
44- func NewPolicyManager (model model.Model , adapter persist.Adapter , rm rbac.RoleManager ) PolicyManager {
39+ func NewPolicyManager (model model.Model , adapter persist.Adapter , rm rbac.RoleManager , shouldPersist func () bool ) api. PolicyManager {
4540 return & policyManager {
46- model : model ,
47- adapter : adapter ,
48- rm : rm ,
49- }
50- }
51-
52- // AddPolicy adds a rule to model and adapter.
53- func (p * policyManager ) AddPolicy (sec string , ptype string , rule []string , shouldPersist bool ) (bool , error ) {
54- if p .model .HasPolicy (sec , ptype , rule ) {
55- return false , nil
56- }
57-
58- if shouldPersist {
59- if err := p .adapter .AddPolicy (sec , ptype , rule ); err != nil {
60- if err .Error () != notImplemented {
61- return false , err
62- }
63- }
64- }
65-
66- p .model .AddPolicy (sec , ptype , rule )
67-
68- if sec == "g" {
69- err := p .model .BuildIncrementalRoleLinks (p .rm , model .PolicyAdd , "g" , ptype , [][]string {rule })
70- if err != nil {
71- return true , err
72- }
41+ model : model ,
42+ adapter : adapter ,
43+ rm : rm ,
44+ shouldPersist : shouldPersist ,
7345 }
74-
75- return true , nil
7646}
7747
7848// AddPolicies adds rules to model and adapter.
79- func (p * policyManager ) AddPolicies (sec string , ptype string , rules [][]string , shouldPersist bool ) (bool , [][]string , error ) {
80- rules = p .model .RemoveExistPolicy (sec , ptype , rules )
49+ func (p * policyManager ) AddPolicies (sec string , ptype string , rules [][]string ) ([][]string , error ) {
50+ rules = p .model .FilterNotExistPolicy (sec , ptype , rules )
8151 if len (rules ) == 0 {
82- return true , nil , nil
52+ return nil , nil
8353 }
8454
85- if shouldPersist {
55+ if p . shouldPersist () {
8656 if err := p .adapter .(persist.BatchAdapter ).AddPolicies (sec , ptype , rules ); err != nil {
8757 if err .Error () != notImplemented {
88- return false , nil , err
58+ return nil , err
8959 }
9060 }
9161 }
@@ -95,93 +65,67 @@ func (p *policyManager) AddPolicies(sec string, ptype string, rules [][]string,
9565 if sec == "g" {
9666 err := p .model .BuildIncrementalRoleLinks (p .rm , model .PolicyAdd , "g" , ptype , rules )
9767 if err != nil {
98- return true , rules , err
99- }
100- }
101-
102- return true , rules , nil
103- }
104-
105- // RemovePolicy removes a rule from model and adapter.
106- func (p * policyManager ) RemovePolicy (sec string , ptype string , rule []string , shouldPersist bool ) (bool , error ) {
107- if shouldPersist {
108- if err := p .adapter .RemovePolicy (sec , ptype , rule ); err != nil {
109- if err .Error () != notImplemented {
110- return false , err
111- }
112- }
113- }
114-
115- ruleRemoved := p .model .RemovePolicy (sec , ptype , rule )
116- if ! ruleRemoved {
117- return ruleRemoved , nil
118- }
119-
120- if sec == "g" {
121- err := p .model .BuildIncrementalRoleLinks (p .rm , model .PolicyRemove , "g" , ptype , [][]string {rule })
122- if err != nil {
123- return ruleRemoved , err
68+ return rules , err
12469 }
12570 }
12671
127- return ruleRemoved , nil
72+ return rules , nil
12873}
12974
13075// RemovePolicies removes rules from model and adapter.
131- func (p * policyManager ) RemovePolicies (sec string , ptype string , rules [][]string , shouldPersist bool ) (bool , [][]string , error ) {
132- rules = p .model .RemoveNotExistPolicy (sec , ptype , rules )
76+ func (p * policyManager ) RemovePolicies (sec string , ptype string , rules [][]string ) ([][]string , error ) {
13377 if len (rules ) == 0 {
134- return true , nil , nil
78+ return nil , nil
13579 }
13680
137- if shouldPersist {
81+ if p . shouldPersist () {
13882 if err := p .adapter .(persist.BatchAdapter ).RemovePolicies (sec , ptype , rules ); err != nil {
13983 if err .Error () != notImplemented {
140- return false , nil , err
84+ return nil , err
14185 }
14286 }
14387 }
14488
14589 rulesRemoved := p .model .RemovePolicies (sec , ptype , rules )
14690 if ! rulesRemoved {
147- return rulesRemoved , rules , nil
91+ return rules , nil
14892 }
14993
15094 if sec == "g" {
15195 err := p .model .BuildIncrementalRoleLinks (p .rm , model .PolicyRemove , "g" , ptype , rules )
15296 if err != nil {
153- return rulesRemoved , rules , err
97+ return rules , err
15498 }
15599 }
156100
157- return rulesRemoved , rules , nil
101+ return rules , nil
158102}
159103
160104// RemoveFilteredPolicy removes rules based on field filters from model and adapter.
161- func (p * policyManager ) RemoveFilteredPolicy (sec string , ptype string , shouldPersist bool , fieldIndex int , fieldValues ... string ) (bool , [][]string , error ) {
105+ func (p * policyManager ) RemoveFilteredPolicy (sec string , ptype string , fieldIndex int , fieldValues ... string ) ([][]string , error ) {
162106 if len (fieldValues ) == 0 {
163- return false , nil , errors .INVALID_FIELDVAULES_PARAMETER
107+ return nil , errors .INVALID_FIELDVAULES_PARAMETER
164108 }
165109
166- if shouldPersist {
110+ if p . shouldPersist () {
167111 if err := p .adapter .RemoveFilteredPolicy (sec , ptype , fieldIndex , fieldValues ... ); err != nil {
168112 if err .Error () != notImplemented {
169- return false , nil , err
113+ return nil , err
170114 }
171115 }
172116 }
173117
174118 ruleRemoved , effects := p .model .RemoveFilteredPolicy (sec , ptype , fieldIndex , fieldValues ... )
175119 if ! ruleRemoved {
176- return ruleRemoved , effects , nil
120+ return effects , nil
177121 }
178122
179123 if sec == "g" {
180124 err := p .model .BuildIncrementalRoleLinks (p .rm , model .PolicyRemove , "g" , ptype , effects )
181125 if err != nil {
182- return ruleRemoved , effects , err
126+ return effects , err
183127 }
184128 }
185129
186- return ruleRemoved , effects , nil
130+ return effects , nil
187131}
0 commit comments