73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
package ldap
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/ps78674/goldap/message"
|
|
)
|
|
|
|
func (s *server) buildBleveQuery(f message.Filter) (string, bool, error) {
|
|
s.l.Trace("Building search expression", "type", fmt.Sprintf("%T", f), "filter", fmt.Sprintf("%#v", f))
|
|
var err error
|
|
var etmp string
|
|
var expr []string
|
|
var exprAnd bool = false
|
|
switch f := f.(type) {
|
|
case message.FilterEqualityMatch:
|
|
etmp, err = mapToBleveStringQuery(string(f.AttributeDesc()), "=", string(f.AssertionValue()))
|
|
expr = append(expr, etmp)
|
|
case message.FilterOr:
|
|
for _, subf := range f {
|
|
s, _, err := s.buildBleveQuery(subf)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
expr = append(expr, s)
|
|
}
|
|
case message.FilterAnd:
|
|
for _, subf := range f {
|
|
s, _, err := s.buildBleveQuery(subf)
|
|
if err != nil {
|
|
return "", false, err
|
|
}
|
|
expr = append(expr, s)
|
|
}
|
|
exprAnd = true
|
|
case message.FilterPresent:
|
|
etmp, err = mapToBleveStringQuery(string(f), "=", "*")
|
|
expr = append(expr, etmp)
|
|
default:
|
|
s.l.Warn("Unsupported search filter", "filter", fmt.Sprintf("%#v", f))
|
|
err = errors.New("unsupported search filter")
|
|
}
|
|
return strings.Join(expr, " "), exprAnd, err
|
|
}
|
|
|
|
func mapToBleveStringQuery(attr, op, val string) (string, error) {
|
|
var predicate, operator string
|
|
|
|
switch attr {
|
|
case "uid":
|
|
predicate = "ID"
|
|
case "cn":
|
|
predicate = "Name"
|
|
case "member":
|
|
predicate = ""
|
|
op = ""
|
|
return val, nil
|
|
default:
|
|
return "", errors.New("search attribute is unsupported")
|
|
}
|
|
|
|
switch op {
|
|
case "=":
|
|
operator = ":"
|
|
default:
|
|
return "", errors.New("search comparison is unsupported")
|
|
}
|
|
|
|
return predicate + operator + val, nil
|
|
}
|