2020-11-08 18:45:07 -06:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/ps78674/goldap/message"
|
|
|
|
)
|
|
|
|
|
2024-07-04 10:07:25 -05:00
|
|
|
func (s *server) buildBleveQuery(f message.Filter) (string, bool, error) {
|
2022-08-27 15:51:03 -05:00
|
|
|
s.l.Trace("Building search expression", "type", fmt.Sprintf("%T", f), "filter", fmt.Sprintf("%#v", f))
|
2020-11-08 18:45:07 -06:00
|
|
|
var err error
|
|
|
|
var etmp string
|
|
|
|
var expr []string
|
2024-07-04 10:07:25 -05:00
|
|
|
var exprAnd bool = false
|
2020-11-08 18:45:07 -06:00
|
|
|
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 {
|
2024-07-04 10:07:25 -05:00
|
|
|
s, _, err := s.buildBleveQuery(subf)
|
2020-11-08 18:45:07 -06:00
|
|
|
if err != nil {
|
2024-07-04 10:07:25 -05:00
|
|
|
return "", false, err
|
2020-11-08 18:45:07 -06:00
|
|
|
}
|
|
|
|
expr = append(expr, s)
|
|
|
|
}
|
2024-07-04 10:07:25 -05:00
|
|
|
case message.FilterAnd:
|
|
|
|
for _, subf := range f {
|
|
|
|
s, _, err := s.buildBleveQuery(subf)
|
|
|
|
if err != nil {
|
|
|
|
return "", false, err
|
|
|
|
}
|
|
|
|
expr = append(expr, s)
|
|
|
|
}
|
|
|
|
exprAnd = true
|
2022-08-27 15:51:03 -05:00
|
|
|
case message.FilterPresent:
|
|
|
|
etmp, err = mapToBleveStringQuery(string(f), "=", "*")
|
|
|
|
expr = append(expr, etmp)
|
2020-11-08 18:45:07 -06:00
|
|
|
default:
|
|
|
|
s.l.Warn("Unsupported search filter", "filter", fmt.Sprintf("%#v", f))
|
|
|
|
err = errors.New("unsupported search filter")
|
|
|
|
}
|
2024-07-04 10:07:25 -05:00
|
|
|
return strings.Join(expr, " "), exprAnd, err
|
2020-11-08 18:45:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func mapToBleveStringQuery(attr, op, val string) (string, error) {
|
|
|
|
var predicate, operator string
|
|
|
|
|
|
|
|
switch attr {
|
|
|
|
case "uid":
|
|
|
|
predicate = "ID"
|
|
|
|
case "cn":
|
|
|
|
predicate = "Name"
|
2024-07-04 10:07:25 -05:00
|
|
|
case "member":
|
2024-07-05 16:05:57 -05:00
|
|
|
return val, nil
|
|
|
|
case "memberOf":
|
2024-07-04 10:07:25 -05:00
|
|
|
return val, nil
|
2020-11-08 18:45:07 -06:00
|
|
|
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
|
|
|
|
}
|