MCPcopy
hub / github.com/apache/casbin / AddNamedPolicy

Method AddNamedPolicy

transaction.go:81–112  ·  view source on GitHub ↗

AddNamedPolicy adds a named policy within the transaction. The policy is buffered and will be applied when the transaction is committed.

(ptype string, params ...interface{})

Source from the content-addressed store, hash-verified

79// AddNamedPolicy adds a named policy within the transaction.
80// The policy is buffered and will be applied when the transaction is committed.
81func (tx *Transaction) AddNamedPolicy(ptype string, params ...interface{}) (bool, error) {
82 tx.mutex.Lock()
83 defer tx.mutex.Unlock()
84
85 if err := tx.checkTransactionStatus(); err != nil {
86 return false, err
87 }
88
89 rule := tx.buildRuleFromParams(params...)
90
91 // Check if policy already exists in the buffered model.
92 bufferedModel, err := tx.buffer.ApplyOperationsToModel(tx.buffer.GetModelSnapshot())
93 if err != nil {
94 return false, err
95 }
96
97 hasPolicy, err := bufferedModel.HasPolicy("p", ptype, rule)
98 if hasPolicy || err != nil {
99 return false, err
100 }
101
102 // Add operation to buffer.
103 op := persist.PolicyOperation{
104 Type: persist.OperationAdd,
105 Section: "p",
106 PolicyType: ptype,
107 Rules: [][]string{rule},
108 }
109 tx.buffer.AddOperation(op)
110
111 return true, nil
112}
113
114// AddPolicies adds multiple policies within the transaction.
115func (tx *Transaction) AddPolicies(rules [][]string) (bool, error) {

Callers 1

AddPolicyMethod · 0.95

Calls 6

buildRuleFromParamsMethod · 0.95
GetModelSnapshotMethod · 0.80
AddOperationMethod · 0.80
HasPolicyMethod · 0.65

Tested by

no test coverage detected