Skip to content

Commit

Permalink
fix: change AddPolicies to batch insert
Browse files Browse the repository at this point in the history
Signed-off-by: tangyang9464 <tangyang9464@163.com>
  • Loading branch information
tangyang9464 committed Sep 9, 2021
1 parent ed2cfa4 commit 992ab30
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 6 additions & 9 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,15 +562,12 @@ func (a *Adapter) RemovePolicy(sec string, ptype string, rule []string) error {

// AddPolicies adds multiple policy rules to the storage.
func (a *Adapter) AddPolicies(sec string, ptype string, rules [][]string) error {
return a.db.Transaction(func(tx *gorm.DB) error {
for _, rule := range rules {
line := a.savePolicyLine(ptype, rule)
if err := tx.Create(&line).Error; err != nil {
return err
}
}
return nil
})
var lines []CasbinRule
for _, rule := range rules {
line := a.savePolicyLine(ptype, rule)
lines = append(lines, line)
}
return a.db.Create(&lines).Error
}

// RemovePolicies removes multiple policy rules from the storage.
Expand Down
9 changes: 9 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,3 +516,12 @@ func TestAdapters(t *testing.T) {
//testUpdatePolicy(t, a)
//testUpdatePolicies(t, a)
}

func TestAddPolicies(t *testing.T) {
a := initAdapter(t, "mysql", "root:@tcp(127.0.0.1:3306)/", "casbin", "casbin_rule")
e, _ := casbin.NewEnforcer("examples/rbac_model.conf", a)
e.AddPolicies([][]string{{"jack", "data1", "read"}, {"jack2", "data1", "read"}})
e.LoadPolicy()

testGetPolicy(t, e, [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"jack", "data1", "read"}, {"jack2", "data1", "read"}})
}

0 comments on commit 992ab30

Please sign in to comment.