[CalendarServer-changes] [13867] twext/trunk/twext/who

source_changes at macosforge.org source_changes at macosforge.org
Tue Aug 12 12:57:18 PDT 2014


Revision: 13867
          http://trac.calendarserver.org//changeset/13867
Author:   sagen at apple.com
Date:     2014-08-12 12:57:18 -0700 (Tue, 12 Aug 2014)
Log Message:
-----------
Adds ExistsExpression (true if an attribute has a value) and BooleanExpression (true if the associated directory attribute has a specific value)

Modified Paths:
--------------
    twext/trunk/twext/who/expression.py
    twext/trunk/twext/who/ldap/_service.py
    twext/trunk/twext/who/ldap/_util.py
    twext/trunk/twext/who/ldap/test/test_service.py
    twext/trunk/twext/who/ldap/test/test_util.py
    twext/trunk/twext/who/opendirectory/test/test_service.py

Modified: twext/trunk/twext/who/expression.py
===================================================================
--- twext/trunk/twext/who/expression.py	2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/expression.py	2014-08-12 19:57:18 UTC (rev 13867)
@@ -21,8 +21,9 @@
 
 __all__ = [
     "Operand",
+    "BooleanExpression",
     "CompoundExpression",
-
+    "ExistsExpression",
     "MatchType",
     "MatchFlags",
     "MatchExpression",
@@ -334,3 +335,71 @@
         return predicate(match(
             normalize(value), normalize(self.fieldValue)
         ))
+
+
+class ExistsExpression(object):
+    """
+    Query for the existence a given field.
+
+    @ivar fieldName: A L{NamedConstant} specifying the field.
+    """
+
+    def __init__(self, fieldName):
+        if not isinstance(fieldName, NamedConstant):
+            raise TypeError(
+                "Field name {name} in exists expression is not a NamedConstant."
+                .format(name=fieldName)
+            )
+
+        self.fieldName = fieldName
+
+
+    def __repr__(self):
+        return (
+            "<{self.__class__.__name__}: {fieldName!r} "
+            .format(
+                self=self,
+                fieldName=describe(self.fieldName),
+            )
+        )
+
+
+    def __eq__(self, other):
+        if isinstance(other, ExistsExpression):
+            return (self.fieldName is other.fieldName)
+        else:
+            return NotImplemented
+
+
+class BooleanExpression(object):
+    """
+    Query for the "True" value of a given field.
+
+    @ivar fieldName: A L{NamedConstant} specifying the field.
+    """
+
+    def __init__(self, fieldName):
+        if not isinstance(fieldName, NamedConstant):
+            raise TypeError(
+                "Field name {name} in boolean expression is not a NamedConstant."
+                .format(name=fieldName)
+            )
+
+        self.fieldName = fieldName
+
+
+    def __repr__(self):
+        return (
+            "<{self.__class__.__name__}: {fieldName!r} "
+            .format(
+                self=self,
+                fieldName=describe(self.fieldName),
+            )
+        )
+
+
+    def __eq__(self, other):
+        if isinstance(other, BooleanExpression):
+            return (self.fieldName is other.fieldName)
+        else:
+            return NotImplemented

Modified: twext/trunk/twext/who/ldap/_service.py
===================================================================
--- twext/trunk/twext/who/ldap/_service.py	2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/_service.py	2014-08-12 19:57:18 UTC (rev 13867)
@@ -47,12 +47,14 @@
     DirectoryService as BaseDirectoryService,
     DirectoryRecord as BaseDirectoryRecord,
 )
-from ..expression import MatchExpression
+from ..expression import MatchExpression, ExistsExpression, BooleanExpression
 from ..util import ConstantsContainer
 from ._constants import LDAPAttribute, LDAPObjectClass
 from ._util import (
     ldapQueryStringFromMatchExpression,
     ldapQueryStringFromCompoundExpression,
+    ldapQueryStringFromBooleanExpression,
+    ldapQueryStringFromExistsExpression,
 )
 from zope.interface import implementer
 
@@ -573,7 +575,12 @@
                 recordTypes = self.recordTypes()
 
             for recordType in recordTypes:
-                rdn = self._recordTypeSchemas[recordType].relativeDN
+                try:
+                    rdn = self._recordTypeSchemas[recordType].relativeDN
+                except KeyError:
+                    # Skip this unknown record type
+                    continue
+
                 rdn = (
                     ldap.dn.str2dn(rdn.lower()) +
                     ldap.dn.str2dn(self._baseDN.lower())
@@ -600,7 +607,7 @@
                     raise LDAPQueryError("Unable to perform query", e)
 
                 except ldap.NO_SUCH_OBJECT as e:
-                    self.log.warn("RDN {rdn} does not exist, skipping", rdn=rdn)
+                    # self.log.warn("RDN {rdn} does not exist, skipping", rdn=rdn)
                     continue
 
                 records.extend(
@@ -628,13 +635,19 @@
 
             self.log.debug("Performing LDAP DN query: {dn}", dn=dn)
 
-            reply = connection.search_s(
-                dn,
-                ldap.SCOPE_SUBTREE,
-                "(objectClass=*)",
-                attrlist=self._attributesToFetch
-            )
-            records = self._recordsFromReply(reply)
+            try:
+                reply = connection.search_s(
+                    dn,
+                    ldap.SCOPE_SUBTREE,
+                    "(objectClass=*)",
+                    attrlist=self._attributesToFetch
+                )
+                records = self._recordsFromReply(reply)
+            except ldap.NO_SUCH_OBJECT:
+                records = []
+            except ldap.INVALID_DN_SYNTAX:
+                self.log.warn("Invalid LDAP DN syntax: '{dn}'", dn=dn)
+                records = []
 
         if len(records):
             return records[0]
@@ -705,6 +718,21 @@
                         else:
                             fields[fieldName] = newValues[0]
 
+                    elif valueType is bool:
+                        if not isinstance(values, list):
+                            values = [values]
+                        if "=" in attribute:
+                            attribute, trueValue = attribute.split("=")
+                        else:
+                            trueValue = "true"
+
+                        for value in values:
+                            if value == trueValue:
+                                fields[fieldName] = True
+                                break
+                        else:
+                            fields[fieldName] = False
+
                     else:
                         raise LDAPConfigurationError(
                             "Unknown value type {0} for field {1}".format(
@@ -741,6 +769,24 @@
                 queryString, recordTypes=recordTypes
             )
 
+        elif isinstance(expression, ExistsExpression):
+            queryString = ldapQueryStringFromExistsExpression(
+                expression,
+                self._fieldNameToAttributesMap, self._recordTypeSchemas
+            )
+            return self._recordsFromQueryString(
+                queryString, recordTypes=recordTypes
+            )
+
+        elif isinstance(expression, BooleanExpression):
+            queryString = ldapQueryStringFromBooleanExpression(
+                expression,
+                self._fieldNameToAttributesMap, self._recordTypeSchemas
+            )
+            return self._recordsFromQueryString(
+                queryString, recordTypes=recordTypes
+            )
+
         return BaseDirectoryService.recordsFromNonCompoundExpression(
             self, expression, records=records
         )
@@ -762,9 +808,13 @@
 
 
     def recordsWithRecordType(self, recordType):
-        return self.recordsWithFieldValue(
-            BaseFieldName.uid, u"*", recordTypes=[recordType]
+        queryString = ldapQueryStringFromExistsExpression(
+            ExistsExpression(self.fieldName.uid),
+            self._fieldNameToAttributesMap, self._recordTypeSchemas
         )
+        return self._recordsFromQueryString(
+            queryString, recordTypes=[recordType]
+        )
 
 
     # def updateRecords(self, records, create=False):
@@ -813,7 +863,16 @@
         return self.service._authenticateUsernamePassword(self.dn, password)
 
 
+def normalizeDNstr(dnStr):
+    """
+    Convert to lowercase and remove extra whitespace
+    @param dnStr: dn
+    @type dnStr: C{str}
+    @return: normalized dn C{str}
+    """
+    return ' '.join(ldap.dn.dn2str(ldap.dn.str2dn(dnStr.lower())).split())
 
+
 def reverseDict(source):
     new = {}
 

Modified: twext/trunk/twext/who/ldap/_util.py
===================================================================
--- twext/trunk/twext/who/ldap/_util.py	2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/_util.py	2014-08-12 19:57:18 UTC (rev 13867)
@@ -17,8 +17,8 @@
 
 from ..idirectory import QueryNotSupportedError, FieldName
 from ..expression import (
-    CompoundExpression, Operand,
-    MatchExpression, MatchFlags,
+    CompoundExpression, ExistsExpression, MatchExpression,
+    MatchFlags, Operand, BooleanExpression
 )
 from ._constants import LDAPOperand, LDAPMatchType, LDAPMatchFlags
 
@@ -154,7 +154,102 @@
     raise AssertionError("We shouldn't be here.")
 
 
+def ldapQueryStringFromExistsExpression(
+    expression, fieldNameToAttributesMap, recordTypeSchemas
+):
+    """
+    Generates an LDAP query string from an exists expression.
 
+    @param expression: An exists expression.
+    @type expression: L{ExistsExpression}
+
+    @param fieldNameToAttributesMap: A mapping from field names to native LDAP
+        attribute names.
+    @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
+        of L{unicode} values.
+
+    @param recordTypeSchemas: Schema information for record types.
+    @type recordTypeSchemas: mapping from L{NamedConstant} to
+        L{RecordTypeSchema}
+
+    @return: An LDAP query string.
+    @rtype: L{unicode}
+
+    @raises QueryNotSupportedError: if the expresion references an unknown
+        field name (meaning a field name not in C{fieldNameToAttributeMap}).
+    """
+
+    fieldName = expression.fieldName
+
+    try:
+        attributes = fieldNameToAttributesMap[fieldName]
+    except KeyError:
+        raise QueryNotSupportedError(
+            "Unmapped field name: {0}".format(expression.fieldName)
+        )
+
+    queryStrings = [
+        u"({attribute}=*)".format(attribute=attribute)
+        for attribute in attributes
+    ]
+
+    operand = LDAPOperand.OR.value
+    return ldapQueryStringFromQueryStrings(operand, queryStrings)
+
+
+def ldapQueryStringFromBooleanExpression(
+    expression, fieldNameToAttributesMap, recordTypeSchemas
+):
+    """
+    Generates an LDAP query string from a boolean expression.
+
+    @param expression: An boolean expression.
+    @type expression: L{BooleanExpression}
+
+    @param fieldNameToAttributesMap: A mapping from field names to native LDAP
+        attribute names.
+    @type fieldNameToAttributesMap: L{dict} with L{FieldName} keys and sequence
+        of L{unicode} values.
+
+    @param recordTypeSchemas: Schema information for record types.
+    @type recordTypeSchemas: mapping from L{NamedConstant} to
+        L{RecordTypeSchema}
+
+    @return: An LDAP query string.
+    @rtype: L{unicode}
+
+    @raises QueryNotSupportedError: if the expresion references an unknown
+        field name (meaning a field name not in C{fieldNameToAttributeMap}).
+    """
+
+    fieldName = expression.fieldName
+
+    try:
+        attributes = fieldNameToAttributesMap[fieldName]
+    except KeyError:
+        raise QueryNotSupportedError(
+            "Unmapped field name: {0}".format(expression.fieldName)
+        )
+
+    queryStrings = []
+    for attribute in attributes:
+        if "=" in attribute:
+            attribute, trueValue = attribute.split("=")
+        else:
+            trueValue = "true"
+
+        queryStrings.append(
+            u"({attribute}={trueValue})".format(
+                attribute=attribute,
+                trueValue=trueValue
+            )
+        )
+
+
+    operand = LDAPOperand.OR.value
+    return ldapQueryStringFromQueryStrings(operand, queryStrings)
+
+
 def ldapQueryStringFromCompoundExpression(
     expression, fieldNameToAttributesMap, recordTypeSchemas
 ):
@@ -227,6 +322,16 @@
             expression, fieldNameToAttributesMap, recordTypeSchemas
         )
 
+    if isinstance(expression, BooleanExpression):
+        return ldapQueryStringFromBooleanExpression(
+            expression, fieldNameToAttributesMap, recordTypeSchemas
+        )
+
+    if isinstance(expression, ExistsExpression):
+        return ldapQueryStringFromExistsExpression(
+            expression, fieldNameToAttributesMap, recordTypeSchemas
+        )
+
     if isinstance(expression, CompoundExpression):
         return ldapQueryStringFromCompoundExpression(
             expression, fieldNameToAttributesMap, recordTypeSchemas
@@ -243,8 +348,7 @@
 
     ord(u"("): u"\\28",
     ord(u")"): u"\\29",
-    # Question: shouldn't we not be quoting * because that's how you specify wildcards?
-    # ord(u"*"): u"\\2A",
+    ord(u"*"): u"\\2A",
 
     ord(u"<"): u"\\3C",
     ord(u"="): u"\\3D",

Modified: twext/trunk/twext/who/ldap/test/test_service.py
===================================================================
--- twext/trunk/twext/who/ldap/test/test_service.py	2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/test/test_service.py	2014-08-12 19:57:18 UTC (rev 13867)
@@ -57,11 +57,10 @@
 from ...test.test_xml import (
     xmlService,
     BaseTest as XMLBaseTest, QueryMixIn,
-    DirectoryServiceConvenienceTestMixIn
-    as BaseDirectoryServiceConvenienceTestMixIn,
+    DirectoryServiceConvenienceTestMixIn,
     DirectoryServiceRealmTestMixIn,
     DirectoryServiceQueryTestMixIn as BaseDirectoryServiceQueryTestMixIn,
-    DirectoryServiceMutableTestMixIn as BaseDirectoryServiceMutableTestMixIn,
+    DirectoryServiceMutableTestMixIn as BaseDirectoryServiceMutableTestMixIn
 )
 
 
@@ -153,21 +152,6 @@
 
 
 
-class DirectoryServiceConvenienceTestMixIn(
-    BaseDirectoryServiceConvenienceTestMixIn
-):
-    def test_recordsWithRecordType_unknown(self):
-        pass
-        # service = self.service()
-
-        # self.assertRaises(
-        #     QueryNotSupportedError,
-        #     service.recordsWithRecordType, UnknownConstant.unknown
-        # )
-    test_recordsWithRecordType_unknown.todo = "After this test runs, other tests fail, need to investigate"
-
-
-
 class DirectoryServiceQueryTestMixIn(BaseDirectoryServiceQueryTestMixIn):
     def test_queryNot(self):
         return BaseDirectoryServiceQueryTestMixIn.test_queryNot(self)
@@ -268,6 +252,7 @@
         self.assertFalse(connection.tls_enabled)
 
 
+    @inlineCallbacks
     def test_connect_withUsernamePassword_invalid(self):
         """
         Connect with UsernamePassword credentials.
@@ -277,7 +262,12 @@
             u"__password__"
         )
         service = self.service(credentials=credentials)
-        self.assertFailure(service._connect(), LDAPBindAuthError)
+        try:
+            yield service._connect()
+        except LDAPBindAuthError:
+            pass
+        else:
+            self.fail("Should have raised LDAPBindAuthError")
 
 
     @inlineCallbacks

Modified: twext/trunk/twext/who/ldap/test/test_util.py
===================================================================
--- twext/trunk/twext/who/ldap/test/test_util.py	2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/ldap/test/test_util.py	2014-08-12 19:57:18 UTC (rev 13867)
@@ -22,7 +22,8 @@
 
 from ...idirectory import QueryNotSupportedError
 from ...expression import (
-    CompoundExpression, Operand, MatchExpression, MatchType, MatchFlags
+    CompoundExpression, ExistsExpression, MatchExpression, BooleanExpression,
+    Operand, MatchType, MatchFlags
 )
 from ...test.test_xml import UnknownConstant
 from .._constants import LDAPOperand
@@ -31,17 +32,29 @@
 )
 from .._util import (
     ldapQueryStringFromQueryStrings,
+    ldapQueryStringFromBooleanExpression,
+    ldapQueryStringFromCompoundExpression,
+    ldapQueryStringFromExistsExpression,
     ldapQueryStringFromMatchExpression,
-    ldapQueryStringFromCompoundExpression,
     ldapQueryStringFromExpression,
 )
 from ...idirectory import FieldName as BaseFieldName
+from twisted.python.constants import Names, NamedConstant
 
-
 TEST_FIELDNAME_MAP = dict(DEFAULT_FIELDNAME_ATTRIBUTE_MAP)
 TEST_FIELDNAME_MAP[BaseFieldName.uid] = (u"__who_uid__",)
 
 
+class TestFieldName(Names):
+    isAwesome = NamedConstant()
+    isAwesome.description = u"is awesome"
+    isAwesome.valueType = bool
+
+    isCool = NamedConstant()
+    isCool.description = u"is cool"
+    isCool.valueType = bool
+
+
 class LDAPQueryTestCase(unittest.TestCase):
     """
     Tests for LDAP query generation.
@@ -81,9 +94,7 @@
                 c,
                 RecordTypeSchema(
                     relativeDN=NotImplemented,  # Don't expect this to be used
-                    attributes=(
-                        (u"recordTypeAttribute", c.name),
-                    )
+                    attributes=((u"recordTypeAttribute", c.name),)
                 )
             )
             for c in service.recordType.iterconstants()
@@ -122,6 +133,55 @@
         )
 
 
+    def test_queryStringFromExistsExpression(self):
+        """
+        Exists expressions produce the correct (attribute=*) string.
+        """
+        service = self.service()
+
+        expression = ExistsExpression(service.fieldName.shortNames)
+        queryString = ldapQueryStringFromExistsExpression(
+            expression,
+            self.fieldNameMap(service),
+            self.recordTypeSchemas(service),
+        )
+        expected = u"(shortNames=*)"
+        self.assertEquals(queryString, expected)
+
+
+    def test_queryStringFromBooleanExpression(self):
+        """
+        If a field is a boolean type and the fieldNameToAttributesMap
+        value for the field has an equals sign, the portion to the right
+        of the equals sign is the value that represents True.  Make sure
+        the query string we generate includes that value.
+        """
+        service = self.service()
+
+        testFieldNameMap = {
+            TestFieldName.isAwesome: ("awesome=totally",),
+            TestFieldName.isCool: ("cool",),
+        }
+
+        expression = BooleanExpression(TestFieldName.isAwesome)
+        queryString = ldapQueryStringFromBooleanExpression(
+            expression,
+            testFieldNameMap,
+            self.recordTypeSchemas(service),
+        )
+        expected = u"(awesome=totally)"
+        self.assertEquals(queryString, expected)
+
+        expression = BooleanExpression(TestFieldName.isCool)
+        queryString = ldapQueryStringFromBooleanExpression(
+            expression,
+            testFieldNameMap,
+            self.recordTypeSchemas(service),
+        )
+        expected = u"(cool=true)"
+        self.assertEquals(queryString, expected)
+
+
     def test_queryStringFromMatchExpression_matchTypes(self):
         """
         Match expressions with each match type produces the correct
@@ -216,7 +276,7 @@
         expected = u"({attribute}={expected})".format(
             attribute=u"fullNames",
             expected=(
-                u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29* "
+                u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29\\2A "
                 "\\7E\\7E \\3E\\3D\\3C \\7E\\7E \\26\\7C \\00!!"
             )
         )

Modified: twext/trunk/twext/who/opendirectory/test/test_service.py
===================================================================
--- twext/trunk/twext/who/opendirectory/test/test_service.py	2014-08-11 21:31:01 UTC (rev 13866)
+++ twext/trunk/twext/who/opendirectory/test/test_service.py	2014-08-12 19:57:18 UTC (rev 13867)
@@ -185,7 +185,7 @@
             u"({attribute}={expected})".format(
                 attribute=ODAttribute.fullName.value,
                 expected=(
-                    u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29* "
+                    u"\\5Cxyzzy: a\\2Fb\\2F\\28c\\29\\2A "
                     "\\7E\\7E \\3E\\3D\\3C \\7E\\7E \\26\\7C \\00!!"
                 )
             )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140812/e88d1e1d/attachment-0001.html>


More information about the calendarserver-changes mailing list