Skip to content

Commit

Permalink
don't return an error if redis key does not exist
Browse files Browse the repository at this point in the history
Returning an error causes casbin enforcer to panic while loading policies
  • Loading branch information
Ron van der Wijngaard committed Dec 21, 2018
1 parent d89b4f1 commit 5048457
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,12 @@ func loadPolicyLine(line CasbinRule, model model.Model) {
// LoadPolicy loads policy from database.
func (a *Adapter) LoadPolicy(model model.Model) error {
text, err := redis.String(a.conn.Do("GET", a.key))
if err == redis.ErrNil {
return nil
}
if err != nil {
return err
}

var lines []CasbinRule
err = json.Unmarshal([]byte(text), &lines)
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func TestAdapter(t *testing.T) {
e := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")

a := NewAdapter("tcp", "127.0.0.1:6379")
t.Run("Read the policies from an empty redis", func(t *testing.T) {
if err := a.LoadPolicy(e.GetModel()); err != nil {
t.Error("Should not return an error")
}
})

// This is a trick to save the current policy to the DB.
// We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
// The current policy means the policy in the Casbin enforcer (aka in memory).
Expand Down

0 comments on commit 5048457

Please sign in to comment.