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

source_changes at macosforge.org source_changes at macosforge.org
Wed Apr 9 15:25:43 PDT 2014


Revision: 13232
          http://trac.calendarserver.org//changeset/13232
Author:   sagen at apple.com
Date:     2014-04-09 15:25:43 -0700 (Wed, 09 Apr 2014)
Log Message:
-----------
Start to get ldap members( ) working

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

Modified: twext/trunk/twext/who/directory.py
===================================================================
--- twext/trunk/twext/who/directory.py	2014-04-09 19:33:34 UTC (rev 13231)
+++ twext/trunk/twext/who/directory.py	2014-04-09 22:25:43 UTC (rev 13232)
@@ -418,10 +418,10 @@
 
     def __repr__(self):
         return (
-            "<{self.__class__.__name__} ({recordType}){shortName}>".format(
+            "<{self.__class__.__name__} ({recordType}){uid}>".format(
                 self=self,
                 recordType=describe(self.recordType),
-                shortName=self.shortNames[0],
+                uid=self.uid,
             )
         )
 

Modified: twext/trunk/twext/who/ldap/_service.py
===================================================================
--- twext/trunk/twext/who/ldap/_service.py	2014-04-09 19:33:34 UTC (rev 13231)
+++ twext/trunk/twext/who/ldap/_service.py	2014-04-09 22:25:43 UTC (rev 13232)
@@ -304,6 +304,10 @@
 
         @raises: L{LDAPConnectionError} if unable to connect.
         """
+
+        # FIXME: ldap connection objects are not thread safe, so let's set up
+        # a connection pool
+
         if not hasattr(self, "_connection"):
             self.log.info("Connecting to LDAP at {log_source.url}")
             connection = ldap.initialize(self.url)
@@ -372,8 +376,7 @@
         self.log.debug("Authenticating {dn}", dn=dn)
         connection = ldap.initialize(self.url)
 
-        # FIXME: Use trace_file option to wire up debug logging when
-        # Twisted adopts the new logging stuff.
+        # FIXME:  Use a separate connection pool perhaps
 
         for option, value in (
             (ldap.OPT_TIMEOUT, self._timeout),
@@ -411,9 +414,13 @@
         self.log.info("Performing LDAP query: {query}", query=queryString)
 
         try:
-            reply = connection.search_s(
-                self._baseDN, ldap.SCOPE_SUBTREE, queryString  # FIXME: attrs
+            reply = yield deferToThread(
+                connection.search_s,
+                self._baseDN,
+                ldap.SCOPE_SUBTREE,
+                queryString  # FIXME: attrs
             )
+
         except ldap.FILTER_ERROR as e:
             self.log.error(
                 "Unable to perform query {0!r}: {1}"
@@ -421,6 +428,34 @@
             )
             raise LDAPQueryError("Unable to perform query", e)
 
+        records = yield self._recordsFromReply(reply)
+        returnValue(records)
+
+
+    @inlineCallbacks
+    def _recordWithDN(self, dn):
+        """
+        @param dn: The DN of the record to search for
+        @type dn: C{str}
+        """
+        connection = yield self._connect()
+
+        self.log.info("Performing LDAP DN query: {dn}", dn=dn)
+
+        reply = yield deferToThread(
+            connection.search_s,
+            dn,
+            ldap.SCOPE_SUBTREE,
+            "(objectClass=*)"  # FIXME: attrs
+        )
+        records = self._recordsFromReply(reply)
+        if len(records):
+            returnValue(records[0])
+        else:
+            returnValue(None)
+
+
+    def _recordsFromReply(self, reply):
         records = []
 
         for dn, recordData in reply:
@@ -447,11 +482,11 @@
                 fieldNames = self._attributeToFieldNameMap.get(attribute)
 
                 if fieldNames is None:
-                    self.log.debug(
-                        "Unmapped LDAP attribute {attribute!r} in record "
-                        "data: {recordData!r}",
-                        attribute=attribute, recordData=recordData,
-                    )
+                    # self.log.debug(
+                    #     "Unmapped LDAP attribute {attribute!r} in record "
+                    #     "data: {recordData!r}",
+                    #     attribute=attribute, recordData=recordData,
+                    # )
                     continue
 
                 for fieldName in fieldNames:
@@ -475,11 +510,8 @@
                             )
                         )
 
-            # Skip any results missing the required fields
-            if (
-                (self.fieldName.uid not in fields) or
-                (self.fieldName.shortNames not in fields)
-            ):
+            # Skip any results missing the uid
+            if self.fieldName.uid not in fields:
                 continue
 
             # Set record type and dn fields
@@ -494,7 +526,7 @@
 
         self.log.debug("LDAP results: {records}", records=records)
 
-        returnValue(records)
+        return records
 
 
     def recordsFromNonCompoundExpression(self, expression, records=None):
@@ -542,13 +574,19 @@
 
     @inlineCallbacks
     def members(self):
+
         if self.recordType != self.service.recordType.group:
             returnValue(())
 
-        raise NotImplementedError()
+        members = set()
+        for dn in getattr(self, "memberDNs", []):
+            record = yield self.service._recordWithDN(dn)
+            members.add(record)
 
+        returnValue(members)
 
-    @inlineCallbacks
+
+    # @inlineCallbacks
     def groups(self):
         raise NotImplementedError()
 

Modified: twext/trunk/twext/who/test/test_directory.py
===================================================================
--- twext/trunk/twext/who/test/test_directory.py	2014-04-09 19:33:34 UTC (rev 13231)
+++ twext/trunk/twext/who/test/test_directory.py	2014-04-09 22:25:43 UTC (rev 13232)
@@ -829,7 +829,7 @@
         wsanchez = self.makeRecord(self.fields_wsanchez)
 
         self.assertEquals(
-            "<DirectoryRecord (user)wsanchez>",
+            "<DirectoryRecord (user)UID:wsanchez>",
             repr(wsanchez)
         )
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20140409/c6d7176b/attachment.html>


More information about the calendarserver-changes mailing list