Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,3 +455,10 @@ func (a *Adapter) filterQuery(session *xorm.Session, filter Filter) *xorm.Sessio

return session
}

// UpdatePolicy update oldRule to newPolicy permanently
func (a *Adapter) UpdatePolicy(sec string, ptype string, oldRule, newPolicy []string) error {
oRule := a.genPolicyLine(ptype, oldRule)
_, err := a.engine.Update(a.genPolicyLine(ptype, newPolicy), oRule)
return err
}
31 changes: 31 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,34 @@ func testAddPolicies(t *testing.T, driverName string, dataSourceName string, dbS
testGetPolicy(t, e, [][]string{{"max", "data2", "read"}, {"max", "data1", "write"}})
}

func testUpdatePolicies(t *testing.T, driverName string, dataSourceName string, dbSpecified ...bool) {
// Initialize some policy in DB.
initPolicy(t, driverName, dataSourceName, dbSpecified...)
// Note: you don't need to look at the above code
// if you already have a working DB with policy inside.

// Now the DB has policy, so we can provide a normal use case.
// Create an adapter and an enforcer.
// NewEnforcer() will load the policy automatically.
a, _ := NewAdapter(driverName, dataSourceName, dbSpecified...)
e, _ := casbin.NewEnforcer("examples/rbac_model.conf")

// Now set the adapter
e.SetAdapter(a)

var err error
logErr := func(action string) {
if err != nil {
t.Fatalf("test action[%s] failed, err: %v", action, err)
}
}

err = a.UpdatePolicy("p", "p", []string{"bob", "data2", "write"}, []string{"alice", "data2", "write"})
logErr("UpdatePolicy")

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

func TestAdapters(t *testing.T) {
// You can also use the following way to use an existing DB "abc":
// testSaveLoad(t, "mysql", "root:@tcp(127.0.0.1:3306)/abc", true)
Expand All @@ -284,4 +312,7 @@ func TestAdapters(t *testing.T) {

testRemovePolicies(t, "mysql", "root:@tcp(127.0.0.1:3306)/")
testRemovePolicies(t, "postgres", "user=postgres host=127.0.0.1 port=5432 sslmode=disable")

testUpdatePolicies(t, "mysql", "root:@tcp(127.0.0.1:3306)/")
testUpdatePolicies(t, "postgres", "user=postgres host=127.0.0.1 port=5432 sslmode=disable")
}