cadoles-profile/ldap/search.go

66 lines
1.3 KiB
Go
Raw Permalink Normal View History

2019-05-13 09:19:33 +02:00
package ldap
import (
"fmt"
ldap "gopkg.in/ldap.v3"
)
type SearchOptions struct {
BaseDN string
Scope int
DerefAliases int
SizeLimit int
TimeLimit int
TypesOnly bool
Attributes []string
Controls []ldap.Control
}
type SearchOptionFunc func(opts *SearchOptions)
func WithBaseDN(dn string) SearchOptionFunc {
return func(opts *SearchOptions) {
opts.BaseDN = dn
}
}
func WithSizeLimit(sizeLimit int) SearchOptionFunc {
return func(opts *SearchOptions) {
opts.SizeLimit = sizeLimit
}
}
func WithAttributes(attributes ...string) SearchOptionFunc {
return func(opts *SearchOptions) {
opts.Attributes = attributes
}
}
func WithScope(scope int) SearchOptionFunc {
return func(opts *SearchOptions) {
opts.Scope = scope
}
}
func EscapeFilter(pattern string, values ...string) string {
escapedValues := make([]interface{}, len(values))
for i, v := range values {
escapedValues[i] = ldap.EscapeFilter(v)
}
return fmt.Sprintf(pattern, escapedValues...)
}
func defaultSearchOptions() *SearchOptions {
return &SearchOptions{
BaseDN: "",
Scope: ldap.ScopeSingleLevel,
DerefAliases: ldap.NeverDerefAliases,
SizeLimit: 0,
TimeLimit: 0,
TypesOnly: false,
Attributes: make([]string, 0),
Controls: make([]ldap.Control, 0),
}
}