-
Notifications
You must be signed in to change notification settings - Fork 4
/
filter.go
77 lines (60 loc) · 1.86 KB
/
filter.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
package ldap
import (
"github.com/stesla/ldap/asn1"
)
type Filter interface{}
func And(filters ...Filter) Filter {
return asn1.OptionValue{Opts: "tag:0,set", Value: filters}
}
func Or(filters ...Filter) Filter {
return asn1.OptionValue{Opts: "tag:1,set", Value: filters}
}
func Not(filter Filter) Filter {
return asn1.OptionValue{Opts: "tag:2", Value: filter}
}
type attributeValueAssertion struct {
Attribute, Value []byte
}
func Equals(attribute, value string) Filter {
val := attributeValueAssertion{[]byte(attribute), []byte(value)}
return asn1.OptionValue{Opts: "tag:3", Value: val}
}
type substring asn1.OptionValue
type substringFilter struct {
Attribute []byte
Substrings []asn1.OptionValue
}
func makeSubstring(tag, val string) substring {
return substring(asn1.OptionValue{Opts: "tag:" + tag, Value: []byte(val)})
}
func InitialSubstring(val string) substring {
return makeSubstring("0", val)
}
func AnySubstring(val string) substring {
return makeSubstring("1", val)
}
func FinalSubstring(val string) substring {
return makeSubstring("2", val)
}
func Substring(attribute string, substrings ...substring) Filter {
optionValues := make([]asn1.OptionValue, len(substrings))
for i, s := range substrings {
optionValues[i] = asn1.OptionValue(s)
}
val := substringFilter{[]byte(attribute), optionValues}
return asn1.OptionValue{Opts: "tag:4", Value: val}
}
func Present(attribute string) Filter {
return asn1.OptionValue{Opts: "tag:7", Value: []byte(attribute)}
}
type matchingRuleAssertion struct {
MatchingRule []byte `asn1:"tag:1,optional"`
Type []byte `asn1:"tag:2,optional"`
MatchValue []byte `asn1:"tag:3"`
DnAttributes bool `asn1:"tag:4"`
}
func Matches(rule, attribute, value string) Filter {
val := matchingRuleAssertion{
[]byte(rule), []byte(attribute), []byte(value), false}
return asn1.OptionValue{Opts: "tag:9", Value: val}
}