Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add checks fieldValues to remove filtered policy #549

Merged
merged 1 commit into from Jul 31, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions errors/rbac_errors.go
Expand Up @@ -18,8 +18,9 @@ import "errors"

// Global errors for rbac defined here
var (
ERR_NAME_NOT_FOUND = errors.New("error: name does not exist")
ERR_DOMAIN_PARAMETER = errors.New("error: domain should be 1 parameter")
ERR_NAMES12_NOT_FOUND = errors.New("error: name1 or name2 does not exist")
ERR_USE_DOMAIN_PARAMETER = errors.New("error: useDomain should be 1 parameter")
ERR_NAME_NOT_FOUND = errors.New("error: name does not exist")
ERR_DOMAIN_PARAMETER = errors.New("error: domain should be 1 parameter")
ERR_NAMES12_NOT_FOUND = errors.New("error: name1 or name2 does not exist")
ERR_USE_DOMAIN_PARAMETER = errors.New("error: useDomain should be 1 parameter")
INVALID_FIELDVAULES_PARAMETER = errors.New("fieldValues requires at least one parameter")
)
5 changes: 5 additions & 0 deletions internal_api.go
Expand Up @@ -15,6 +15,7 @@
package casbin

import (
Err "github.com/casbin/casbin/v2/errors"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
)
Expand Down Expand Up @@ -170,6 +171,10 @@ func (e *Enforcer) removePolicies(sec string, ptype string, rules [][]string) (b

// removeFilteredPolicy removes rules based on field filters from the current policy.
func (e *Enforcer) removeFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) (bool, error) {
if len(fieldValues) == 0 {
return false, Err.INVALID_FIELDVAULES_PARAMETER
}

if e.shouldPersist() {
if err := e.adapter.RemoveFilteredPolicy(sec, ptype, fieldIndex, fieldValues...); err != nil {
if err.Error() != notImplemented {
Expand Down
7 changes: 6 additions & 1 deletion model/policy.go
Expand Up @@ -179,6 +179,11 @@ func (model Model) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int
var effects [][]string
res := false
firstIndex := -1

if len(fieldValues) == 0 {
return false, effects
}

for index, rule := range model[sec][ptype].Policy {
matched := true
for i, fieldValue := range fieldValues {
Expand All @@ -200,8 +205,8 @@ func (model Model) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int
}
}

model[sec][ptype].Policy = tmp
if firstIndex != -1 {
model[sec][ptype].Policy = tmp
for i := firstIndex; i < len(model[sec][ptype].Policy); i++ {
model[sec][ptype].PolicyMap[strings.Join(model[sec][ptype].Policy[i], DefaultSep)] = i
}
Expand Down