[CalendarServer-changes] [14100] twext/trunk/twext/who/ldap

source_changes at macosforge.org source_changes at macosforge.org
Thu Oct 23 15:40:08 PDT 2014


Revision: 14100
          http://trac.calendarserver.org//changeset/14100
Author:   sagen at apple.com
Date:     2014-10-23 15:40:08 -0700 (Thu, 23 Oct 2014)
Log Message:
-----------
Fix boolean and multi-choice fields for LDAP

Modified Paths:
--------------
    twext/trunk/twext/who/ldap/_service.py
    twext/trunk/twext/who/ldap/test/test_service.py

Modified: twext/trunk/twext/who/ldap/_service.py
===================================================================
--- twext/trunk/twext/who/ldap/_service.py	2014-10-23 16:58:03 UTC (rev 14099)
+++ twext/trunk/twext/who/ldap/_service.py	2014-10-23 22:40:08 UTC (rev 14100)
@@ -302,7 +302,9 @@
             for attribute in attributes:
                 if ":" in attribute:
                     attribute, ignored = attribute.split(":", 1)
-                self._attributeToFieldNameMap[attribute] = name
+                self._attributeToFieldNameMap.setdefault(
+                    attribute, []
+                ).append(name)
 
         self._recordTypeSchemas = recordTypeSchemas
 
@@ -758,81 +760,83 @@
             fields = {}
 
             for attribute, values in recordData.iteritems():
-                fieldName = self._attributeToFieldNameMap.get(attribute)
-                attributeRules = self._fieldNameToAttributesMap[fieldName]
+                fieldNames = self._attributeToFieldNameMap.get(attribute, ())
+                for fieldName in fieldNames:
+                    attributeRules = self._fieldNameToAttributesMap[fieldName]
 
-                if fieldName is None:
-                    # self.log.debug(
-                    #     "Unmapped LDAP attribute {attribute!r} in record "
-                    #     "data: {recordData!r}",
-                    #     attribute=attribute, recordData=recordData,
-                    # )
-                    continue
+                    if fieldName is None:
+                        # self.log.debug(
+                        #     "Unmapped LDAP attribute {attribute!r} in record "
+                        #     "data: {recordData!r}",
+                        #     attribute=attribute, recordData=recordData,
+                        # )
+                        continue
 
-                valueType = self.fieldName.valueType(fieldName)
+                    valueType = self.fieldName.valueType(fieldName)
 
-                if valueType in (unicode, UUID):
-                    if not isinstance(values, list):
-                        values = [values]
+                    if valueType in (unicode, UUID):
+                        if not isinstance(values, list):
+                            values = [values]
 
-                    if valueType is unicode:
-                        newValues = []
-                        for v in values:
-                            if isinstance(v, unicode):
-                                # because the ldap unit test produces
-                                # unicode values (?)
-                                newValues.append(v)
-                            else:
-                                newValues.append(unicode(v, "utf-8"))
-                        # newValues = [unicode(v, "utf-8") for v in values]
-                    else:
-                        newValues = [valueType(v) for v in values]
+                        if valueType is unicode:
+                            newValues = []
+                            for v in values:
+                                if isinstance(v, unicode):
+                                    # because the ldap unit test produces
+                                    # unicode values (?)
+                                    newValues.append(v)
+                                else:
+                                    newValues.append(unicode(v, "utf-8"))
+                            # newValues = [unicode(v, "utf-8") for v in values]
+                        else:
+                            newValues = [valueType(v) for v in values]
 
-                    if self.fieldName.isMultiValue(fieldName):
-                        fields[fieldName] = newValues
-                    else:
-                        fields[fieldName] = newValues[0]
+                        if self.fieldName.isMultiValue(fieldName):
+                            fields[fieldName] = newValues
+                        else:
+                            fields[fieldName] = newValues[0]
 
-                elif valueType is bool:
-                    if not isinstance(values, list):
-                        values = [values]
+                    elif valueType is bool:
+                        if not isinstance(values, list):
+                            values = [values]
 
-                    rule = attributeRules[0]  # there is only one true value
-                    if ":" in rule:
-                        ignored, trueValue = rule.split(":")
-                    else:
-                        trueValue = "true"
 
-                    for value in values:
-                        if value == trueValue:
-                            fields[fieldName] = True
-                            break
-                    else:
-                        fields[fieldName] = False
+                        rule = attributeRules[0]  # there is only one true value
+                        if ":" in rule:
+                            ignored, trueValue = rule.split(":")
+                        else:
+                            trueValue = "true"
 
-                elif issubclass(valueType, Names):
-                    if not isinstance(values, list):
-                        values = [values]
-
-                    for rule in attributeRules:
-                        attribute, attributeValue, fieldValue = rule.split(":")
-
                         for value in values:
-                            if value == attributeValue:
-                                # convert to a constant
-                                try:
-                                    fieldValue = valueType.lookupByName(fieldValue)
-                                    fields[fieldName] = fieldValue
-                                except ValueError:
-                                    pass
+                            if value == trueValue:
+                                fields[fieldName] = True
                                 break
+                        else:
+                            fields[fieldName] = False
 
-                else:
-                    raise LDAPConfigurationError(
-                        "Unknown value type {0} for field {1}".format(
-                            valueType, fieldName
+                    elif issubclass(valueType, Names):
+                        if not isinstance(values, list):
+                            values = [values]
+
+                        for rule in attributeRules:
+                            attribute, attributeValue, fieldValue = rule.split(":")
+
+                            for value in values:
+                                if value == attributeValue:
+                                    # convert to a constant
+                                    try:
+                                        fieldValue = valueType.lookupByName(fieldValue)
+                                        fields[fieldName] = fieldValue
+                                    except ValueError:
+                                        pass
+                                    break
+
+                    else:
+                        raise LDAPConfigurationError(
+                            "Unknown value type {0} for field {1}".format(
+                                valueType, fieldName
+                            )
                         )
-                    )
 
             # Skip any results missing the uid, which is a required field
             if self.fieldName.uid not in fields:

Modified: twext/trunk/twext/who/ldap/test/test_service.py
===================================================================
--- twext/trunk/twext/who/ldap/test/test_service.py	2014-10-23 16:58:03 UTC (rev 14099)
+++ twext/trunk/twext/who/ldap/test/test_service.py	2014-10-23 22:40:08 UTC (rev 14100)
@@ -107,25 +107,49 @@
     multiChoice.description = u"Multiple Choice Test Field"
     multiChoice.valueType = TestFieldWithChoices
 
-    trueFalse = NamedConstant()
-    trueFalse.description = u"Boolean Test Field"
-    trueFalse.valueType = bool
+    boolean1 = NamedConstant()
+    boolean1.description = u"Boolean Test Field"
+    boolean1.valueType = bool
 
+    boolean2 = NamedConstant()
+    boolean2.description = u"Boolean Test Field 2"
+    boolean2.valueType = bool
 
+    boolean3 = NamedConstant()
+    boolean3.description = u"Boolean Test Field 3"
+    boolean3.valueType = bool
 
+
 TEST_FIELDNAME_MAP = dict(DEFAULT_FIELDNAME_ATTRIBUTE_MAP)
+TEST_FIELDNAME_MAP[BaseFieldName.uid] = (u"__who_uid__",)
+
 TEST_FIELDNAME_MAP[TestFieldName.multiChoice] = (
     u"testField:One:one",
     u"testField:Two:two",
     u"testField:Three:three",
 )
-TEST_FIELDNAME_MAP[TestFieldName.trueFalse] = (
+
+# Set up two Fields which will map to the same LDAP attribute ("foo")
+# to make sure both Fields get set.  Their field values *will* be different,
+# based on whether there is a colon in the attribute rule.  If the LDAP
+# value matches the string following the colon, the Field value will be True.
+# If there is no colon, then the string to match is literally the string "true".
+TEST_FIELDNAME_MAP[TestFieldName.boolean1] = (
     u"foo:active",
 )
-TEST_FIELDNAME_MAP[BaseFieldName.uid] = (u"__who_uid__",)
+TEST_FIELDNAME_MAP[TestFieldName.boolean2] = (
+    u"foo",
+)
 
+# This Field will map to LDAP attribute "bar" and will be used for matching
+# the string "true"
+TEST_FIELDNAME_MAP[TestFieldName.boolean3] = (
+    u"bar",
+)
 
 
+
+
 class TestService(DirectoryService, QueryMixIn):
     pass
 
@@ -430,6 +454,8 @@
                 {
                     "__who_uid__": u"active",
                     "foo": u"active",
+                    "bar": u"true",
+                    "unknown": u"unknown",  # will ignore unknown LDAP attrs
                 }
             ),
             (
@@ -437,14 +463,21 @@
                 {
                     "__who_uid__": u"inactive",
                     "foo": u"inactive",
+                    "bar": u"false",
                 }
             ),
         )
         records = service._recordsFromReply(reply, recordType=RecordType.user)
-        self.assertTrue(records[0].trueFalse)
-        self.assertFalse(records[1].trueFalse)
 
+        self.assertTrue(records[0].boolean1)   # foo == active so True
+        self.assertFalse(records[0].boolean2)  # foo != true so False
+        self.assertTrue(records[0].boolean3)  # bar == true so True
 
+        self.assertFalse(records[1].boolean1)  # foo != active so False
+        self.assertFalse(records[1].boolean2)  # foo != true so False
+        self.assertFalse(records[1].boolean3)  # bar != true so False
+
+
     def test_multipleChoice(self):
         service = self.service()
         reply = (
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20141023/6cfb864c/attachment-0001.html>


More information about the calendarserver-changes mailing list