forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
scope.go
60 lines (53 loc) · 1.23 KB
/
scope.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package scope
import (
"sort"
"strings"
)
// Add takes two sets of scopes, and returns a combined sorted set of scopes
func Add(has []string, new []string) []string {
sorted := sortAndCopy(has)
for _, s := range new {
i := sort.SearchStrings(sorted, s)
if i == len(sorted) {
sorted = append(sorted, s)
} else if sorted[i] != s {
sorted = append(sorted, "")
copy(sorted[i+1:], sorted[i:])
sorted[i] = s
}
}
return sorted
}
func Split(scope string) []string {
scope = strings.TrimSpace(scope)
if scope == "" {
return []string{}
}
return strings.Split(scope, " ")
}
func Join(scopes []string) string {
return strings.Join(scopes, " ")
}
func Covers(has, requested []string) bool {
// no scopes allows all access, so requesting an empty list is NOT covered by a list with anything in it
if len(requested) == 0 && len(has) > 0 {
return false
}
has, requested = sortAndCopy(has), sortAndCopy(requested)
NextRequested:
for i := range requested {
for j := range has {
if has[j] == requested[i] {
continue NextRequested
}
}
return false
}
return true
}
func sortAndCopy(arr []string) []string {
newArr := make([]string, len(arr))
copy(newArr, arr)
sort.Sort(sort.StringSlice(newArr))
return newArr
}