[Checkins] SVN: zope.webdav/trunk/src/zope/webdav/ Use the IReadDirectory adapter to recursively find all sub resource

Michael Kerrin michael.kerrin at openapp.biz
Mon Feb 19 17:18:16 EST 2007


Log message for revision 72696:
  Use the IReadDirectory adapter to recursively find all sub resource
  during a propfind request.
  

Changed:
  U   zope.webdav/trunk/src/zope/webdav/ftests/test_propfind.py
  U   zope.webdav/trunk/src/zope/webdav/propfind.py
  U   zope.webdav/trunk/src/zope/webdav/tests/test_propfind.py

-=-
Modified: zope.webdav/trunk/src/zope/webdav/ftests/test_propfind.py
===================================================================
--- zope.webdav/trunk/src/zope/webdav/ftests/test_propfind.py	2007-02-19 22:12:13 UTC (rev 72695)
+++ zope.webdav/trunk/src/zope/webdav/ftests/test_propfind.py	2007-02-19 22:18:15 UTC (rev 72696)
@@ -232,7 +232,10 @@
     def test_notfound_property(self):
         httpresponse, xmlbody = self.checkPropfind(
             "/", env = {"DEPTH": "0"},
-            properties = "<D:prop><D:resourcetype /><D:missingproperty /></D:prop>")
+            properties = """<D:prop>
+  <D:resourcetype />
+  <D:missingproperty />
+</D:prop>""")
         responses = xmlbody.findall("{DAV:}response")
         self.assertEqual(len(responses), 1)
         response = responses[0]
@@ -253,7 +256,7 @@
             properties = "<D:prop><D:resourcetype /></D:prop>")
 
         responses = xmlbody.findall("{DAV:}response")
-        self.assertEqual(len(responses), 6)
+        self.assertEqual(len(responses), 7)
 
         # make sure we have all 200 status codes, and the hrefs differ
         for response in responses:
@@ -266,12 +269,13 @@
         hrefs = [href.text for href in
                  xmlbody.findall("{DAV:}response/{DAV:}href")]
         hrefs.sort()
-        self.assertEqual(hrefs, ['http://localhost/',
-                                 'http://localhost/a/',
-                                 'http://localhost/a/r2',
-                                 'http://localhost/a/r3',
-                                 'http://localhost/b/',
-                                 'http://localhost/r1'])
+        self.assertEqual(hrefs, ["http://localhost/",
+                                 "http://localhost/++etc++site/",
+                                 "http://localhost/a/",
+                                 "http://localhost/a/r2",
+                                 "http://localhost/a/r3",
+                                 "http://localhost/b/",
+                                 "http://localhost/r1"])
 
     def test_depthone(self):
         self.createCollectionResourceStructure()
@@ -281,7 +285,7 @@
             properties = "<D:prop><D:resourcetype /></D:prop>")
 
         responses = xmlbody.findall("{DAV:}response")
-        self.assertEqual(len(responses), 4)
+        self.assertEqual(len(responses), 5)
 
         # make sure we have all 200 status codes, and the hrefs differ
         for response in responses:
@@ -294,8 +298,11 @@
         hrefs = [href.text for href in
                  xmlbody.findall("{DAV:}response/{DAV:}href")]
         hrefs.sort()
-        self.assertEqual(hrefs, ['http://localhost/', 'http://localhost/a/',
-                                 'http://localhost/b/', 'http://localhost/r1'])
+        self.assertEqual(hrefs, ["http://localhost/",
+                                 "http://localhost/++etc++site/",
+                                 "http://localhost/a/",
+                                 "http://localhost/b/",
+                                 "http://localhost/r1"])
 
     def test_opaque_properties(self):
         file = self.addResource("/r", "some file content",

Modified: zope.webdav/trunk/src/zope/webdav/propfind.py
===================================================================
--- zope.webdav/trunk/src/zope/webdav/propfind.py	2007-02-19 22:12:13 UTC (rev 72695)
+++ zope.webdav/trunk/src/zope/webdav/propfind.py	2007-02-19 22:18:15 UTC (rev 72696)
@@ -40,8 +40,9 @@
 
 from zope import interface
 from zope import component
-from zope.app.container.interfaces import IReadContainer
+from zope.filerepresentation.interfaces import IReadDirectory
 from zope.app.error.interfaces import IErrorReportingUtility
+from zope.security.checker import canAccess
 from zope.security.interfaces import Unauthorized
 
 from zope.etree.interfaces import IEtree
@@ -133,13 +134,15 @@
         the properties we want to return.
         """
         responses = [propertiesFactory(ob, req, extraArg)]
-
-        if depth in ("1", "infinity") and IReadContainer.providedBy(ob):
+        if depth in ("1", "infinity"):
             subdepth = (depth == "1") and "0" or "infinity"
 
-            for subob in ob.values():
-                responses.extend(self.handlePropfindResource(
-                    subob, req, subdepth, propertiesFactory, extraArg))
+            readdir = IReadDirectory(ob, None)
+            if readdir is not None and canAccess(readdir, "values"):
+                for subob in readdir.values():
+                    if subob is not None:
+                        responses.extend(self.handlePropfindResource(
+                            subob, req, subdepth, propertiesFactory, extraArg))
 
         return responses
 

Modified: zope.webdav/trunk/src/zope/webdav/tests/test_propfind.py
===================================================================
--- zope.webdav/trunk/src/zope/webdav/tests/test_propfind.py	2007-02-19 22:12:13 UTC (rev 72695)
+++ zope.webdav/trunk/src/zope/webdav/tests/test_propfind.py	2007-02-19 22:18:15 UTC (rev 72696)
@@ -28,9 +28,12 @@
 from zope import schema
 import zope.schema.interfaces
 from zope.traversing.browser.interfaces import IAbsoluteURL
+from zope.filerepresentation.interfaces import IReadDirectory
 from zope.app.container.interfaces import IReadContainer
 from zope.app.error.interfaces import IErrorReportingUtility
-from zope.security.interfaces import IUnauthorized
+from zope.security.interfaces import Unauthorized, IUnauthorized
+from zope.security.interfaces import IChecker
+from zope.security.checker import CheckerPublic
 
 import zope.webdav.properties
 import zope.webdav.publisher
@@ -705,6 +708,36 @@
         self.assertEqual(isinstance(exc_info[0][1], NotImplementedError), True)
 
 
+class CollectionSecurityChecker(object):
+    # Simple security checker to make the recursive propfind method handle
+    # the canAccess call that is made during the processing of these requests.
+    interface.implements(IChecker)
+
+    def __init__(self, get_permissions = {}):
+        self.get_permissions = get_permissions
+
+    def check_getattr(self, ob, name):
+        permission = self.get_permissions.get(name)
+        if permission is CheckerPublic:
+            return
+        raise Unauthorized(object, name, permission)
+
+    def check_setattr(self, ob, name):
+        raise NotImplementedError("check_setattr(ob, name) not implemented")
+
+    def check(self, ob, operation):
+        raise NotImplementedError("check(ob, operation) not implemented")
+
+    def proxy(self, value):
+        raise NotImplementedError("proxy(value) not implemented")
+
+
+def readDirectory(container):
+    container.__Security_checker__ = CollectionSecurityChecker(
+        {"values": CheckerPublic})
+    return container
+
+
 class PROPFINDRecuseTest(unittest.TestCase):
 
     def setUp(self):
@@ -713,9 +746,15 @@
         # break all the renderAllProperties methods.
         unauthProperty.restricted = True
 
+        component.getGlobalSiteManager().registerAdapter(
+            readDirectory, (IReadContainer,), provided = IReadDirectory)
+
     def tearDown(self):
         propfindTearDown()
 
+        component.getGlobalSiteManager().unregisterAdapter(
+            readDirectory, (IReadContainer,), provided = IReadDirectory)
+
     def test_handlePropfindResource(self):
         collection = Collection()
         collection["r1"] = Resource("some text - r1", 2)
@@ -734,7 +773,9 @@
   <ns0:href xmlns:ns0="DAV:">/collection/</ns0:href>
   <ns0:propstat xmlns:ns0="DAV:">
     <ns0:prop xmlns:ns0="DAV:">
-      <ns0:resourcetype xmlns:ns0="DAV:"><ns0:collection xmlns:ns0="DAV:"/></ns0:resourcetype>
+      <ns0:resourcetype xmlns:ns0="DAV:">
+        <ns0:collection xmlns:ns0="DAV:"/>
+      </ns0:resourcetype>
     </ns0:prop>
     <ns0:status xmlns:ns0="DAV:">HTTP/1.1 200 OK</ns0:status>
   </ns0:propstat>
@@ -743,7 +784,9 @@
   <ns0:href xmlns:ns0="DAV:">/collection/c/</ns0:href>
   <ns0:propstat xmlns:ns0="DAV:">
     <ns0:prop xmlns:ns0="DAV:">
-      <ns0:resourcetype xmlns:ns0="DAV:"><ns0:collection xmlns:ns0="DAV:"/></ns0:resourcetype>
+      <ns0:resourcetype xmlns:ns0="DAV:">
+        <ns0:collection xmlns:ns0="DAV:"/>
+      </ns0:resourcetype>
     </ns0:prop>
     <ns0:status xmlns:ns0="DAV:">HTTP/1.1 200 OK</ns0:status>
   </ns0:propstat>



More information about the Checkins mailing list