Skip to content

Commit

Permalink
fix: GetAllObjects has wrong result in RBAC with domains mode
Browse files Browse the repository at this point in the history
- add GetValuesForFieldInPolicyAllTypesByName function to adynamically get fieldIndex for each ptype
- add test for getting objects (subjects and actions are also included) in RBAC with domains mode
  • Loading branch information
truc0 committed Jun 12, 2024
1 parent e97e354 commit 30d1ba3
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions constant/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package constant

const (
ActionIndex = "act"
DomainIndex = "dom"
SubjectIndex = "sub"
ObjectIndex = "obj"
Expand Down
25 changes: 19 additions & 6 deletions management_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,51 @@ import (
"fmt"
"strings"

"github.com/casbin/casbin/v2/constant"
"github.com/casbin/casbin/v2/util"
"github.com/casbin/govaluate"
)

// GetAllSubjects gets the list of subjects that show up in the current policy.
func (e *Enforcer) GetAllSubjects() ([]string, error) {
return e.model.GetValuesForFieldInPolicyAllTypes("p", 0)
return e.model.GetValuesForFieldInPolicyAllTypesByName("p", constant.SubjectIndex)
}

// GetAllNamedSubjects gets the list of subjects that show up in the current named policy.
func (e *Enforcer) GetAllNamedSubjects(ptype string) ([]string, error) {
return e.model.GetValuesForFieldInPolicy("p", ptype, 0)
fieldIndex, err := e.model.GetFieldIndex(ptype, constant.SubjectIndex)
if err != nil {
return nil, err
}
return e.model.GetValuesForFieldInPolicy("p", ptype, fieldIndex)
}

// GetAllObjects gets the list of objects that show up in the current policy.
func (e *Enforcer) GetAllObjects() ([]string, error) {
return e.model.GetValuesForFieldInPolicyAllTypes("p", 1)
return e.model.GetValuesForFieldInPolicyAllTypesByName("p", constant.ObjectIndex)
}

// GetAllNamedObjects gets the list of objects that show up in the current named policy.
func (e *Enforcer) GetAllNamedObjects(ptype string) ([]string, error) {
return e.model.GetValuesForFieldInPolicy("p", ptype, 1)
fieldIndex, err := e.model.GetFieldIndex(ptype, constant.ObjectIndex)
if err != nil {
return nil, err
}
return e.model.GetValuesForFieldInPolicy("p", ptype, fieldIndex)
}

// GetAllActions gets the list of actions that show up in the current policy.
func (e *Enforcer) GetAllActions() ([]string, error) {
return e.model.GetValuesForFieldInPolicyAllTypes("p", 2)
return e.model.GetValuesForFieldInPolicyAllTypesByName("p", constant.ActionIndex)
}

// GetAllNamedActions gets the list of actions that show up in the current named policy.
func (e *Enforcer) GetAllNamedActions(ptype string) ([]string, error) {
return e.model.GetValuesForFieldInPolicy("p", ptype, 2)
fieldIndex, err := e.model.GetFieldIndex(ptype, constant.ActionIndex)
if err != nil {
return nil, err
}
return e.model.GetValuesForFieldInPolicy("p", ptype, fieldIndex)
}

// GetAllRoles gets the list of roles that show up in the current policy.
Expand Down
9 changes: 9 additions & 0 deletions management_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ func TestGetList(t *testing.T) {
testStringList(t, "Roles", e.GetAllRoles, []string{"data2_admin"})
}

func TestGetListWithDomains(t *testing.T) {
e, _ := NewEnforcer("examples/rbac_with_domains_model.conf", "examples/rbac_with_domains_policy.csv")

testStringList(t, "Subjects", e.GetAllSubjects, []string{"admin"})
testStringList(t, "Objects", e.GetAllObjects, []string{"data1", "data2"})
testStringList(t, "Actions", e.GetAllActions, []string{"read", "write"})
testStringList(t, "Roles", e.GetAllRoles, []string{"admin"})
}

func testGetPolicy(t *testing.T, e *Enforcer, res [][]string) {
t.Helper()
myRes, err := e.GetPolicy()
Expand Down
22 changes: 22 additions & 0 deletions model/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -458,3 +458,25 @@ func (model Model) GetValuesForFieldInPolicyAllTypes(sec string, fieldIndex int)

return values, nil
}

// GetValuesForFieldInPolicyAllTypesByName gets all values for a field for all rules in a policy of all ptypes, duplicated values are removed.
func (model Model) GetValuesForFieldInPolicyAllTypesByName(sec string, field string) ([]string, error) {
values := []string{}

for ptype := range model[sec] {
// GetFieldIndex will return (-1, err) if field is not found, ignore it
index, err := model.GetFieldIndex(ptype, field)
if err != nil {
continue
}
v, err := model.GetValuesForFieldInPolicy(sec, ptype, index)
if err != nil {
return nil, err
}
values = append(values, v...)
}

util.ArrayRemoveDuplicates(&values)

return values, nil
}

0 comments on commit 30d1ba3

Please sign in to comment.