...
1
21
22 package fosite
23
24 import "strings"
25
26
27 type ScopeStrategy func(haystack []string, needle string) bool
28
29 func HierarchicScopeStrategy(haystack []string, needle string) bool {
30 for _, this := range haystack {
31
32 if this == needle {
33 return true
34 }
35
36
37 if len(this) > len(needle) {
38 continue
39 }
40
41 needles := strings.Split(needle, ".")
42 haystack := strings.Split(this, ".")
43 haystackLen := len(haystack) - 1
44 for k, needle := range needles {
45 if haystackLen < k {
46 return true
47 }
48
49 current := haystack[k]
50 if current != needle {
51 break
52 }
53 }
54 }
55
56 return false
57 }
58
59 func ExactScopeStrategy(haystack []string, needle string) bool {
60 for _, this := range haystack {
61 if needle == this {
62 return true
63 }
64 }
65
66 return false
67 }
68
69 func WildcardScopeStrategy(matchers []string, needle string) bool {
70 needleParts := strings.Split(needle, ".")
71 for _, matcher := range matchers {
72 matcherParts := strings.Split(matcher, ".")
73
74 if len(matcherParts) > len(needleParts) {
75 continue
76 }
77
78 var noteq bool
79 for k, c := range strings.Split(matcher, ".") {
80
81 if k == len(matcherParts)-1 && len(matcherParts) != len(needleParts) {
82 if c != "*" {
83 noteq = true
84 break
85 }
86 }
87
88 if c == "*" && len(needleParts[k]) > 0 {
89
90 continue
91 } else if c != needleParts[k] {
92 noteq = true
93 break
94 }
95 }
96
97 if !noteq {
98 return true
99 }
100 }
101
102 return false
103 }
104
View as plain text