policy:="((student AND CS) OR (teacher AND xidian)) OR male"
attributes := []string{"student", "xidian", "CS", "male"}
如图,policy为包含与或门的访问策略树,attributes是属性集合,如何判断属性是否符合访问控制树呢?有没有相关的算法思想呢?当然有代码就更好了,最好是golang。急,有偿!
很明显在此例中是匹配策略树的。
policy:="((student AND CS) OR (teacher AND xidian)) OR male"
attributes := []string{"student", "xidian", "CS", "male"}
如图,policy为包含与或门的访问策略树,attributes是属性集合,如何判断属性是否符合访问控制树呢?有没有相关的算法思想呢?当然有代码就更好了,最好是golang。急,有偿!
很明显在此例中是匹配策略树的。
搞了一整上午,终于搞出来个Go版本的。
package main
import (
"fmt"
"regexp"
)
type node struct {
option string
str string
left *node
right *node
}
func judgeTrue(root *node, maps map[string]bool) bool {
if root.option != "" {
if root.option == "AND" {
return judgeTrue(root.left, maps) && judgeTrue(root.right, maps)
} else {
return judgeTrue(root.left, maps) || judgeTrue(root.right, maps)
}
} else {
_, ok := maps[root.str]
return ok
}
}
func buildDecisionTree(policy string) *node {
ra := `^\((.*)\){1}\s+(AND|OR){1}\s+\((.*)\){1}$`
rb := `^([^\(\)]*){1}\s+(AND|OR){1}\s+\((.*)\){1}$`
rc := `^\((.*)\){1}\s+(AND|OR){1}\s+([^\(\)]*){1}$`
rd := `^([^\(\)]*){1}\s+(AND|OR){1}\s+([^\(\)]*){1}$`
rlist := []string{ra, rb, rc, rd}
var i int
var flag bool = false
var left, option, right string
for i = 0; i < 4; i++ {
regTemp := regexp.MustCompile(rlist[i])
result := regTemp.FindAllStringSubmatch(policy, -1)
if len(result) > 0 {
left = regTemp.ReplaceAllString(policy, "$1")
option = regTemp.ReplaceAllString(policy, "$2")
right = regTemp.ReplaceAllString(policy, "$3")
flag = true
}
}
var root *node = &node{}
if flag{
root.left = buildDecisionTree(left)
root.option = option
root.right = buildDecisionTree(right)
}else{
root.str = policy
}
return root
}
func getMaps(inputList []string) map[string]bool{
var maps map[string]bool = make(map[string]bool)
for index:=range(inputList){
maps[ inputList[index] ] = true
}
return maps
}
func main() {
var policy string = "((student AND CS) OR (teacher AND xidian)) OR male"
root:=buildDecisionTree(policy)
attributes := []string{"student", "xidian", "CS", "male"}
maps:=getMaps(attributes)
ans:=judgeTrue(root, maps)
fmt.Println(ans)
}