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

source_changes at macosforge.org source_changes at macosforge.org
Fri Aug 14 19:13:52 PDT 2015


Revision: 15040
          http://trac.calendarserver.org//changeset/15040
Author:   sagen at apple.com
Date:     2015-08-14 19:13:52 -0700 (Fri, 14 Aug 2015)
Log Message:
-----------
Allow multiple LDAP attributes to be the source for a single record field

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	2015-08-13 17:14:58 UTC (rev 15039)
+++ twext/trunk/twext/who/ldap/_service.py	2015-08-15 02:13:52 UTC (rev 15040)
@@ -776,74 +776,68 @@
             # Populate a fields dictionary
             fields = {}
 
-            for attribute, values in recordData.iteritems():
-                fieldNames = self._attributeToFieldNameMap.get(attribute, ())
-                for fieldName in fieldNames:
-                    attributeRules = self._fieldNameToAttributesMap[fieldName]
+            for fieldName, attributeRules in self._fieldNameToAttributesMap.iteritems():
+                valueType = self.fieldName.valueType(fieldName)
 
-                    if fieldName is None:
-                        # self.log.debug(
-                        #     "Unmapped LDAP attribute {attribute!r} in record "
-                        #     "data: {recordData!r}",
-                        #     attribute=attribute, recordData=recordData,
-                        # )
-                        continue
+                for attributeRule in attributeRules:
+                    attributeName = attributeRule.split(":")[0]
+                    if attributeName in recordData:
+                        values = recordData[attributeName]
 
-                    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"))
+                            else:
+                                try:
+                                    newValues = [valueType(v) for v in values]
+                                except Exception, e:
+                                    self.log.warn(
+                                        "Can't parse value {name} {values} ({error})",
+                                        name=fieldName, values=values, error=str(e)
+                                    )
+                                    continue
 
-                        if valueType is unicode:
-                            newValues = []
-                            for v in values:
-                                if isinstance(v, unicode):
-                                    # because the ldap unit test produces
-                                    # unicode values (?)
-                                    newValues.append(v)
+                            if self.fieldName.isMultiValue(fieldName):
+                                if fieldName in fields:
+                                    fields[fieldName].extend(newValues)
                                 else:
-                                    newValues.append(unicode(v, "utf-8"))
-                        else:
-                            try:
-                                newValues = [valueType(v) for v in values]
-                            except Exception, e:
-                                self.log.warn(
-                                    "Can't parse value {name} {values} ({error})",
-                                    name=fieldName, values=values, error=str(e)
-                                )
-                                continue
+                                    fields[fieldName] = newValues
+                            else:
+                                # First one in the list wins
+                                if fieldName not in fields:
+                                    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]
+                            if ":" in attributeRule:
+                                ignored, trueValue = attributeRule.split(":")
+                            else:
+                                trueValue = "true"
 
-                    elif valueType is bool:
-                        if not isinstance(values, list):
-                            values = [values]
+                            for value in values:
+                                if value == trueValue:
+                                    fields[fieldName] = True
+                                    break
+                            else:
+                                fields[fieldName] = False
 
+                        elif issubclass(valueType, Names):
+                            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"
+                            attribute, attributeValue, fieldValue = attributeRule.split(":")
 
-                        for value in values:
-                            if value == trueValue:
-                                fields[fieldName] = True
-                                break
-                        else:
-                            fields[fieldName] = False
-
-                    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
@@ -854,12 +848,12 @@
                                         pass
                                     break
 
-                    else:
-                        raise LDAPConfigurationError(
-                            "Unknown value type {0} for field {1}".format(
-                                valueType, fieldName
+                        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	2015-08-13 17:14:58 UTC (rev 15039)
+++ twext/trunk/twext/who/ldap/test/test_service.py	2015-08-15 02:13:52 UTC (rev 15040)
@@ -121,8 +121,10 @@
 
 
 TEST_FIELDNAME_MAP = dict(DEFAULT_FIELDNAME_ATTRIBUTE_MAP)
-TEST_FIELDNAME_MAP[BaseFieldName.uid] = (u"__who_uid__",)
+TEST_FIELDNAME_MAP[BaseFieldName.uid] = (u"__who_uid__", u"altuid")
 
+TEST_FIELDNAME_MAP[BaseFieldName.fullNames] = (u"altname", u"cn")
+
 TEST_FIELDNAME_MAP[TestFieldName.multiChoice] = (
     u"testField:One:one",
     u"testField:Two:two",
@@ -524,6 +526,61 @@
         self.assertFalse(service.fieldName.multiChoice in records[1].fields)
 
 
+    def test_multipleAttributes(self):
+        """
+        Multiple LDAP attributes can be the source for a single record field.
+        If it's a single-value field, the *first* attribute in the map that
+        is in the results will be used for the field value.  If it's a multi
+        value field, then the value will be a list extended from the values
+        in each associated LDAP attribute, in the order of the map entry.
+
+        For example, in the test map above, the uid field is mapped to __who_uid__
+        and altuid in that order.  When the LDAP results are parsed, the __who_uid__
+        attribute is checked first, and if it has a value it is used; otherwise
+        the value from altuid would be used.
+
+        In the case of "fullNames" which is multi-value, because the order in the test
+        map is "altname" then "cn", when the LDAP results are parsed, fullNames
+        will end up being set to a list comprising of first the altname values
+        then the cn values.
+        """
+        service = self.service()
+        reply = (
+            (
+                "dn",
+                {
+                    "__who_uid__": u"zero",
+                    "altuid": u"altzero",
+                    "cn": [u"cn-name", "another-cn"],
+                    "altname": [u"alt-name", "another-alt"],
+                }
+            ),
+            (
+                "dn",
+                {
+                    "altuid": u"one",
+                    "altname": [u"alt-name"],
+                    "cn": [u"cn-name"],
+                }
+            ),
+            (
+                "dn",
+                {
+                    "__who_uid__": u"two",
+                    "cn": [u"cn-name"],
+                }
+            ),
+        )
+        records = service._recordsFromReply(reply, recordType=RecordType.user)
+
+        self.assertEquals(records[0].uid, "zero")
+        self.assertEquals(records[0].fullNames, ["alt-name", "another-alt", "cn-name", "another-cn"])
+        self.assertEquals(records[1].uid, "one")
+        self.assertEquals(records[1].fullNames, ["alt-name", "cn-name"])
+        self.assertEquals(records[2].uid, "two")
+        self.assertEquals(records[2].fullNames, ["cn-name"])
+
+
 def mockDirectoryDataFromXMLService(service):
     dc0 = u"org"
     dc1 = u"calendarserver"
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20150814/95749039/attachment-0001.html>


More information about the calendarserver-changes mailing list