def _compileSimpleFilter(attribute, operator, value): searchFilter = Filter() if operator == ':=': # extensibleMatch match = RE_EX_ATTRIBUTE_1.match( attribute) or RE_EX_ATTRIBUTE_2.match(attribute) if not match: raise LDAPFilterInvalidException( "invalid filter attribute: '%s'" % attribute) attribute, dn, matchingRule = match.groups() if attribute: searchFilter['extensibleMatch']['type'] = attribute if dn: searchFilter['extensibleMatch']['dnAttributes'] = bool(dn) if matchingRule: searchFilter['extensibleMatch']['matchingRule'] = matchingRule searchFilter['extensibleMatch']['matchValue'] = value else: if not RE_ATTRIBUTE.match(attribute): raise LDAPFilterInvalidException( "invalid filter attribute: '%s'" % attribute) if value == '*' and operator == '=': # present searchFilter['present'] = attribute elif '*' in value and operator == '=': # substring assertions = value.split('*') choice = searchFilter['substrings'][ 'substrings'].getComponentType() substrings = [] if assertions[0]: substrings.append(choice.clone().setComponentByName( 'initial', assertions[0])) for assertion in assertions[1:-1]: substrings.append(choice.clone().setComponentByName( 'any', assertion)) if assertions[-1]: substrings.append(choice.clone().setComponentByName( 'final', assertions[-1])) searchFilter['substrings']['type'] = attribute searchFilter['substrings']['substrings'].setComponents( *substrings) elif '*' not in value: # simple if operator == '=': searchFilter['equalityMatch'].setComponents( attribute, value) elif operator == '~=': searchFilter['approxMatch'].setComponents(attribute, value) elif operator == '>=': searchFilter['greaterOrEqual'].setComponents( attribute, value) elif operator == '<=': searchFilter['lessOrEqual'].setComponents(attribute, value) else: raise LDAPFilterInvalidException("invalid filter '(%s%s%s)'" % (attribute, operator, value)) return searchFilter
def _compileCompositeFilter(operator, filters): searchFilter = Filter() if operator == '!': if len(filters) != 1: raise LDAPFilterInvalidException("'not' filter must have exactly one element") searchFilter['not'].setComponents(*filters) elif operator == '&': if len(filters) == 0: raise LDAPFilterInvalidException("'and' filter must have at least one element") searchFilter['and'].setComponents(*filters) elif operator == '|': if len(filters) == 0: raise LDAPFilterInvalidException("'or' filter must have at least one element") searchFilter['or'].setComponents(*filters) return searchFilter
def _compileCompositeFilter(operator, filters): searchFilter = Filter() if operator == u'!': if len(filters) != 1: raise LDAPFilterInvalidException("'not' filter must have exactly one element") choice = Not().setComponentByName('notFilter', filters[0]) searchFilter.setComponentByName('not', choice, verifyConstraints=False) elif operator == u'&': if len(filters) == 0: raise LDAPFilterInvalidException("'and' filter must have at least one element") choice = And().setComponents(*filters) searchFilter.setComponentByName('and', choice) elif operator == u'|': if len(filters) == 0: raise LDAPFilterInvalidException("'or' filter must have at least one element") choice = Or().setComponents(*filters) searchFilter.setComponentByName('or', choice) return searchFilter
def _compileCompositeFilter(operator, filters): searchFilter = Filter() if operator == u'!': if len(filters) != 1: raise LDAPFilterInvalidException( "'not' filter must have exactly one element") choice = Not().setComponentByName('notFilter', filters[0]) searchFilter.setComponentByName('not', choice, verifyConstraints=False) elif operator == u'&': if len(filters) == 0: raise LDAPFilterInvalidException( "'and' filter must have at least one element") choice = And().setComponents(*filters) searchFilter.setComponentByName('and', choice) elif operator == u'|': if len(filters) == 0: raise LDAPFilterInvalidException( "'or' filter must have at least one element") choice = Or().setComponents(*filters) searchFilter.setComponentByName('or', choice) return searchFilter
def _compileSimpleFilter(attribute, operator, value): searchFilter = Filter() if operator == u':=': # extensibleMatch match = RE_EX_ATTRIBUTE_1.match(attribute) or RE_EX_ATTRIBUTE_2.match(attribute) if not match: raise LDAPFilterInvalidException("invalid filter attribute: '{0}'".format(attribute)) attribute, dn, matchingRule = match.groups() choice = MatchingRuleAssertion() if attribute: choice.setComponentByName('type', attribute) choice.setComponentByName('dnAttributes', bool(dn)) if matchingRule: choice.setComponentByName('matchingRule', matchingRule) choice.setComponentByName('matchValue', value) searchFilter.setComponentByName('extensibleMatch', choice) else: if not RE_ATTRIBUTE.match(attribute): raise LDAPFilterInvalidException("invalid filter attribute: '{0}'".format(attribute)) if value == u'*' and operator == u'=': # present choice = Present(attribute) searchFilter.setComponentByName('present', choice) elif u'*' in value and operator == u'=': # substring components = [] assertions = value.split(u'*') initial = assertions[0] if initial: components.append(SubString().setComponentByName('initial', initial)) for assertion in assertions[1:-1]: components.append(SubString().setComponentByName('any', assertion)) final = assertions[-1] if final: components.append(SubString().setComponentByName('final', final)) subStrings = SubStrings().setComponents(*components) choice = SubstringFilter().setComponents(attribute, subStrings) searchFilter.setComponentByName('substrings', choice) elif u'*' not in value: # simple if operator == u'=': choice = EqualityMatch().setComponents(attribute, value) searchFilter.setComponentByName('equalityMatch', choice) elif operator == u'~=': choice = ApproxMatch().setComponents(attribute, value) searchFilter.setComponentByName('approxMatch', choice) elif operator == u'>=': choice = GreaterOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('greaterOrEqual', choice) elif operator == u'<=': choice = LessOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('lessOrEqual', choice) else: raise LDAPFilterInvalidException("invalid filter '({0}{1}{2})'".format(attribute, operator, value)) return searchFilter
def _compileSimpleFilter(attribute, operator, value): searchFilter = Filter() if operator == u':=': # extensibleMatch match = RE_EX_ATTRIBUTE_1.match( attribute) or RE_EX_ATTRIBUTE_2.match(attribute) if not match: raise LDAPFilterInvalidException( "invalid filter attribute: '{0}'".format(attribute)) attribute, dn, matchingRule = match.groups() choice = MatchingRuleAssertion() if attribute: choice.setComponentByName('type', attribute) choice.setComponentByName('dnAttributes', bool(dn)) if matchingRule: choice.setComponentByName('matchingRule', matchingRule) choice.setComponentByName('matchValue', value) searchFilter.setComponentByName('extensibleMatch', choice) else: if not RE_ATTRIBUTE.match(attribute): raise LDAPFilterInvalidException( "invalid filter attribute: '{0}'".format(attribute)) if value == u'*' and operator == u'=': # present choice = Present(attribute) searchFilter.setComponentByName('present', choice) elif u'*' in value and operator == u'=': # substring components = [] assertions = value.split(u'*') initial = assertions[0] if initial: components.append(SubString().setComponentByName( 'initial', initial)) for assertion in assertions[1:-1]: if not assertion: raise LDAPFilterInvalidException( "consecutive '*' in filter assertion") components.append(SubString().setComponentByName( 'any', assertion)) final = assertions[-1] if final: components.append(SubString().setComponentByName( 'final', final)) subStrings = SubStrings().setComponents(*components) choice = SubstringFilter().setComponents(attribute, subStrings) searchFilter.setComponentByName('substrings', choice) elif u'*' not in value: # simple if operator == u'=': choice = EqualityMatch().setComponents(attribute, value) searchFilter.setComponentByName('equalityMatch', choice) elif operator == u'~=': choice = ApproxMatch().setComponents(attribute, value) searchFilter.setComponentByName('approxMatch', choice) elif operator == u'>=': choice = GreaterOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('greaterOrEqual', choice) elif operator == u'<=': choice = LessOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('lessOrEqual', choice) return searchFilter
def _compileSimpleFilter(attribute, operator, value): # ToDo: extensibleMatch searchFilter = Filter() if value == u'*' and operator == u'=': # present choice = Present(attribute) searchFilter.setComponentByName('present', choice) elif u'*' in value and operator == u'=': # substring components = [] assertions = value.split(u'*') initial = assertions[0] if initial: components.append(SubString().setComponentByName('initial', initial)) for assertion in assertions[1:-1]: if not assertion: raise LDAPInvalidFilterException("consecutive '*' in filter") components.append(SubString().setComponentByName('any', assertion)) final = assertions[-1] if final: components.append(SubString().setComponentByName('final', final)) subStrings = SubStrings().setComponents(*components) choice = SubstringFilter().setComponents(attribute, subStrings) searchFilter.setComponentByName('substrings', choice) elif u'*' not in value: # simple if operator == u'=': choice = EqualityMatch().setComponents(attribute, value) searchFilter.setComponentByName('equalityMatch', choice) elif operator == u'~=': choice = ApproxMatch().setComponents(attribute, value) searchFilter.setComponentByName('approxMatch', choice) elif operator == u'>=': choice = GreaterOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('greaterOrEqual', choice) elif operator == u'<=': choice = LessOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('lessOrEqual', choice) return searchFilter
def _compileSimpleFilter(attribute, operator, value): # ToDo: extensibleMatch searchFilter = Filter() if value == u'*' and operator == u'=': # present choice = Present(attribute) searchFilter.setComponentByName('present', choice) elif u'*' in value and operator == u'=': # substring components = [] assertions = value.split(u'*') initial = assertions[0] if initial: components.append(SubString().setComponentByName( 'initial', initial)) for assertion in assertions[1:-1]: if not assertion: raise LDAPInvalidFilterException( "consecutive '*' in filter") components.append(SubString().setComponentByName( 'any', assertion)) final = assertions[-1] if final: components.append(SubString().setComponentByName( 'final', final)) subStrings = SubStrings().setComponents(*components) choice = SubstringFilter().setComponents(attribute, subStrings) searchFilter.setComponentByName('substrings', choice) elif u'*' not in value: # simple if operator == u'=': choice = EqualityMatch().setComponents(attribute, value) searchFilter.setComponentByName('equalityMatch', choice) elif operator == u'~=': choice = ApproxMatch().setComponents(attribute, value) searchFilter.setComponentByName('approxMatch', choice) elif operator == u'>=': choice = GreaterOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('greaterOrEqual', choice) elif operator == u'<=': choice = LessOrEqual().setComponents(attribute, value) searchFilter.setComponentByName('lessOrEqual', choice) return searchFilter