-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathldapfilter.go
91 lines (77 loc) · 1.78 KB
/
ldapfilter.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package ldapfilter
type Input = map[string][]interface{}
type Filter interface {
Match(input Input) bool
Append(filter Filter)
}
// AndFilter filters entries by and operation
type AndFilter struct {
Type string
Children []Filter
}
// NewAndFilter inititalizes a new AndFilter
func NewAndFilter() *AndFilter {
return &AndFilter{Type: "and"}
}
// Match matches entry input
func (f *AndFilter) Match(input Input) bool {
for _, filter := range f.Children {
if !filter.Match(input) {
return false
}
}
return true
}
// Append appends a new filter
func (f *AndFilter) Append(filter Filter) {
f.Children = append(f.Children, filter)
}
// OrFilter filters entries by or operation
type OrFilter struct {
Type string
Children []Filter
}
// NewOrFilter inititalizes a new OrFilter
func NewOrFilter() *OrFilter {
return &OrFilter{Type: "or"}
}
// Match matches entry input
func (f *OrFilter) Match(input Input) bool {
if len(f.Children) == 0 {
return true
}
for _, filter := range f.Children {
if filter.Match(input) {
return true
}
}
return false
}
// Append appends a new filter
func (f *OrFilter) Append(filter Filter) {
f.Children = append(f.Children, filter)
}
// EqualityFilter filters entries by equality
type EqualityFilter struct {
Type string
Key string
Value string
}
// NewEqualityFilter inititalizes a new EqualityFilter
func NewEqualityFilter() *EqualityFilter {
return &EqualityFilter{Type: "equality"}
}
// Match matches entry input
func (f *EqualityFilter) Match(input Input) bool {
if values, ok := input[f.Key]; ok {
for _, value := range values {
if value == f.Value {
return true
}
}
}
return false
}
// Append does not appand anything here,
// because EqualityFilter is already a leaf
func (f *EqualityFilter) Append(filter Filter) {}