Skip to content

Commit

Permalink
Merge pull request #24 from closetool/master
Browse files Browse the repository at this point in the history
fix: update action change all rows(#23)
  • Loading branch information
hsluoyz authored Mar 3, 2021
2 parents 799c2bd + 4329087 commit 91d507a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
10 changes: 5 additions & 5 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,13 @@ func (a *Adapter) updatePolicies(oldLines, newLines []*CasbinRule) error {

for i, line := range oldLines {
newLines[i].ID = line.ID
_, err = tx.Model(newLines[i]).WherePK().Update()
if err != nil {
tx.Rollback()
return err
}
}

_, err = tx.Model(&newLines).WherePK().Update()
if err != nil {
tx.Rollback()
return err
}
if err = tx.Commit(); err != nil {
return err
}
Expand Down
29 changes: 27 additions & 2 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,35 @@ func (s *AdapterTestSuite) TestUpdatePolicy() {
err = s.e.SavePolicy()
s.Require().NoError(err)

_, err = s.e.UpdatePolicies([][]string{{"alice", "data1", "read"}}, [][]string{{"bob", "data1", "read"}})
_, err = s.e.UpdatePolicies([][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}}, [][]string{{"bob", "data1", "read"}, {"alice", "data2", "write"}})
s.Require().NoError(err)

s.assertPolicy(s.e.GetPolicy(), [][]string{{"bob", "data1", "read"}, {"bob", "data2", "write"}, {"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}})
err = s.e.LoadPolicy()
s.Require().NoError(err)

s.assertPolicy(s.e.GetPolicy(), [][]string{{"data2_admin", "data2", "read"}, {"data2_admin", "data2", "write"}, {"bob", "data1", "read"}, {"alice", "data2", "write"}})
}

func (s *AdapterTestSuite) TestUpdatePolicyWithLoadFilteredPolicy() {
var err error
s.e, err = casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
s.Require().NoError(err)

s.e.SetAdapter(s.a)

err = s.e.SavePolicy()
s.Require().NoError(err)

err = s.e.LoadFilteredPolicy(&Filter{P: []string{"data2_admin"}})
s.Require().NoError(err)

_, err = s.e.UpdatePolicies(s.e.GetPolicy(), [][]string{{"bob", "data2", "read"}, {"alice", "data2", "write"}})
s.Require().NoError(err)

err = s.e.LoadPolicy()
s.Require().NoError(err)

s.assertPolicy(s.e.GetPolicy(), [][]string{{"alice", "data1", "read"}, {"bob", "data2", "write"}, {"bob", "data2", "read"}, {"alice", "data2", "write"}})
}

func TestAdapterTestSuite(t *testing.T) {
Expand Down

0 comments on commit 91d507a

Please sign in to comment.