<commit_before>package casbin

import (
	"github.com/hsluoyz/casbin/util"
	"log"
)

// Initialize the roles in RBAC.
func (model Model) BuildRoleLinks() {
	for _, ast := range model["g"] {
		ast.buildRoleLinks()
	}
}

// Print the policy.
func (model Model) PrintPolicy() {
	log.Print("Policy:")
	for key, ast := range model["p"] {
		log.Print(key, ": ", ast.Value, ": ", ast.Policy)
	}

	for key, ast := range model["g"] {
		log.Print(key, ": ", ast.Value, ": ", ast.Policy)
	}
}

// Clear all current policy.
func (model Model) ClearPolicy() {
	for _, ast := range model["p"] {
		ast.Policy = nil
	}

	for _, ast := range model["g"] {
		ast.Policy = nil
	}
}

// Get all rules in a policy.
func (model Model) GetPolicy(sec string, ptype string) [][]string {
	return model[sec][ptype].Policy
}

// Get rules based on a field filter from a policy.
func (model Model) GetFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValue string) [][]string {
	res := [][]string{}

	for _, v := range model[sec][ptype].Policy {
		if v[fieldIndex] == fieldValue {
			res = append(res, v)
		}
	}

	return res
}

// Determine whether a model has the specified policy rule.
func (model Model) HasPolicy(sec string, ptype string, policy []string) bool {
	for _, rule := range model[sec][ptype].Policy {
		if util.ArrayEquals(policy, rule) {
			return true
		}
	}

	return false
}

// Add a policy rule to the model.
func (model Model) AddPolicy(sec string, ptype string, policy []string) bool {
	if !model.HasPolicy(sec, ptype, policy) {
		model[sec][ptype].Policy = append(model[sec][ptype].Policy, policy)
		return true
	} else {
		return false
	}
}

// Remove a policy rule from the model.
func (model Model) RemovePolicy(sec string, ptype string, policy []string) bool {
	for i, rule := range model[sec][ptype].Policy {
		if util.ArrayEquals(policy, rule) {
			model[sec][ptype].Policy = append(model[sec][ptype].Policy[:i], model[sec][ptype].Policy[i+1:]...)
			return true
		}
	}

	return false
}

// Remove policy rules based on a field filter from the model.
func (model Model) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValue string) bool {
	res := false
	for i := range model[sec][ptype].Policy {
		if model[sec][ptype].Policy[i][fieldIndex] == fieldValue {
			model[sec][ptype].Policy = append(model[sec][ptype].Policy[:i], model[sec][ptype].Policy[i+1:]...)
			i -= 1
			res = true
		}
	}

	return res
}

// Get all values for a field for all rules in a policy, duplicated values are removed.
func (model Model) GetValuesForFieldInPolicy(sec string, ptype string, fieldIndex int) []string {
	users := []string{}

	for _, rule := range model[sec][ptype].Policy {
		users = append(users, rule[fieldIndex])
	}

	util.ArrayRemoveDuplicates(&users)
	// sort.Strings(users)

	return users
}<commit_msg>Fix the bug in removing policy with a filter function.<commit_after>package casbin

import (
	"github.com/hsluoyz/casbin/util"
	"log"
)

// Initialize the roles in RBAC.
func (model Model) BuildRoleLinks() {
	for _, ast := range model["g"] {
		ast.buildRoleLinks()
	}
}

// Print the policy.
func (model Model) PrintPolicy() {
	log.Print("Policy:")
	for key, ast := range model["p"] {
		log.Print(key, ": ", ast.Value, ": ", ast.Policy)
	}

	for key, ast := range model["g"] {
		log.Print(key, ": ", ast.Value, ": ", ast.Policy)
	}
}

// Clear all current policy.
func (model Model) ClearPolicy() {
	for _, ast := range model["p"] {
		ast.Policy = nil
	}

	for _, ast := range model["g"] {
		ast.Policy = nil
	}
}

// Get all rules in a policy.
func (model Model) GetPolicy(sec string, ptype string) [][]string {
	return model[sec][ptype].Policy
}

// Get rules based on a field filter from a policy.
func (model Model) GetFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValue string) [][]string {
	res := [][]string{}

	for _, v := range model[sec][ptype].Policy {
		if v[fieldIndex] == fieldValue {
			res = append(res, v)
		}
	}

	return res
}

// Determine whether a model has the specified policy rule.
func (model Model) HasPolicy(sec string, ptype string, policy []string) bool {
	for _, rule := range model[sec][ptype].Policy {
		if util.ArrayEquals(policy, rule) {
			return true
		}
	}

	return false
}

// Add a policy rule to the model.
func (model Model) AddPolicy(sec string, ptype string, policy []string) bool {
	if !model.HasPolicy(sec, ptype, policy) {
		model[sec][ptype].Policy = append(model[sec][ptype].Policy, policy)
		return true
	} else {
		return false
	}
}

// Remove a policy rule from the model.
func (model Model) RemovePolicy(sec string, ptype string, policy []string) bool {
	for i, rule := range model[sec][ptype].Policy {
		if util.ArrayEquals(policy, rule) {
			model[sec][ptype].Policy = append(model[sec][ptype].Policy[:i], model[sec][ptype].Policy[i+1:]...)
			return true
		}
	}

	return false
}

// Remove policy rules based on a field filter from the model.
func (model Model) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValue string) bool {
	tmp := [][]string{}
	res := false
	for _, rule := range model[sec][ptype].Policy {
		if rule[fieldIndex] != fieldValue {
			tmp = append(tmp, rule)
		} else {
			res = true
		}
	}

	model[sec][ptype].Policy = tmp
	return res
}

// Get all values for a field for all rules in a policy, duplicated values are removed.
func (model Model) GetValuesForFieldInPolicy(sec string, ptype string, fieldIndex int) []string {
	users := []string{}

	for _, rule := range model[sec][ptype].Policy {
		users = append(users, rule[fieldIndex])
	}

	util.ArrayRemoveDuplicates(&users)
	// sort.Strings(users)

	return users
}
{'fraction_non_alphanumeric': 0.14848859819692417, 'fraction_numerical': 0.0007070885628424961, 'mean_word_length': 3.877586206896552, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6973394', 'n_tokens_mistral': 1969, 'n_tokens_neox': 1874, 'n_words': 584}