[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/component/ Fixed a bug that prevented use of permissions with multi-adapters.

Jim Fulton jim at zope.com
Mon Feb 7 19:07:12 EST 2005


Log message for revision 29080:
  Fixed a bug that prevented use of permissions with multi-adapters.
  

Changed:
  U   Zope3/trunk/src/zope/app/component/metaconfigure.py
  U   Zope3/trunk/src/zope/app/component/tests/adapter.py
  U   Zope3/trunk/src/zope/app/component/tests/test_directives.py

-=-
Modified: Zope3/trunk/src/zope/app/component/metaconfigure.py
===================================================================
--- Zope3/trunk/src/zope/app/component/metaconfigure.py	2005-02-07 22:56:07 UTC (rev 29079)
+++ Zope3/trunk/src/zope/app/component/metaconfigure.py	2005-02-08 00:07:11 UTC (rev 29080)
@@ -173,12 +173,6 @@
         if provides is None:
             raise TypeError("Missing 'provides' attribute")            
 
-    if permission is not None:
-        if permission == PublicPermission:
-            permission = CheckerPublic
-        checker = InterfaceChecker(provides, permission)
-        factory.append(lambda c: proxify(c, checker))
-
     # Generate a single factory from multiple factories:
     factories = factory
     if len(factories) == 1:
@@ -188,13 +182,14 @@
     elif len(factories) > 1 and len(for_) != 1:
         raise ValueError("Can't use multiple factories and multiple for")
     else:
-        def factory(ob):
-            for f in factories:
-                ob = f(ob)
-            return ob
-        # Store the original factory for documentation
-        factory.factory = factories[0]
+        factory = _rolledUpFactory(factories)
 
+    if permission is not None:
+        if permission == PublicPermission:
+            permission = CheckerPublic
+        checker = InterfaceChecker(provides, permission)
+        factory = _protectedFactory(factory, checker)
+
     if trusted:
         factory = TrustedAdapterFactory(factory)
 
@@ -218,6 +213,32 @@
                     args = ('', iface)
                     )
 
+def _rolledUpFactory(factories):
+    # This has to be named 'factory', aparently, so as not to confuse
+    # apidoc :(
+    def factory(ob):
+        for f in factories:
+            ob = f(ob)
+        return ob
+    # Store the original factory for documentation
+    factory.factory = factories[0]
+    return factory
+
+def _protectedFactory(original_factory, checker):
+    # This has to be named 'factory', aparently, so as not to confuse
+    # apidoc :(
+    def factory(*args):
+        ob = original_factory(*args)
+        try:
+            ob.__Security_checker__ = checker
+        except AttributeError:
+            ob = Proxy(ob, checker)
+        
+        return ob
+    factory.factory = factory
+    return factory
+
+
 def utility(_context, provides=None, component=None, factory=None,
             permission=None, name=''):
     if factory:

Modified: Zope3/trunk/src/zope/app/component/tests/adapter.py
===================================================================
--- Zope3/trunk/src/zope/app/component/tests/adapter.py	2005-02-07 22:56:07 UTC (rev 29079)
+++ Zope3/trunk/src/zope/app/component/tests/adapter.py	2005-02-08 00:07:11 UTC (rev 29080)
@@ -27,7 +27,9 @@
     pass
 
 class I3(zope.interface.Interface):
-    pass
+    def f1(): pass
+    def f2(): pass
+    def f3(): pass
 
 class IS(zope.interface.Interface):
     pass

Modified: Zope3/trunk/src/zope/app/component/tests/test_directives.py
===================================================================
--- Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-07 22:56:07 UTC (rev 29079)
+++ Zope3/trunk/src/zope/app/component/tests/test_directives.py	2005-02-08 00:07:11 UTC (rev 29080)
@@ -383,6 +383,33 @@
         self.assertEqual(a3.__class__, A3)
         self.assertEqual(a3.context, (content, a1, a2))
 
+    def testProtectedMultiAdapter(self):
+        from zope.app.component.tests.adapter import A1, A2, A3, I3
+        from zope.component.tests.components import Content
+
+        xmlconfig(StringIO(template % (
+            """
+            <adapter
+              factory="zope.app.component.tests.adapter.A3
+                      "
+              provides="zope.app.component.tests.adapter.I3"
+              for="zope.component.tests.components.IContent
+                   zope.app.component.tests.adapter.I1
+                   zope.app.component.tests.adapter.I2
+                  "
+              permission="zope.Public"
+              />
+            """
+            )))
+
+        content = Content()
+        a1 = A1()
+        a2 = A2()
+        a3 = ProxyFactory(zapi.queryMultiAdapter((content, a1, a2), I3))
+        self.assertEqual(a3.__class__, A3)
+        items = [item[0] for item in getTestProxyItems(a3)]
+        self.assertEqual(items, ['f1', 'f2', 'f3'])
+
     def testMultiAdapter_wo_for_or_provides(self):
         from zope.app.component.tests.adapter import A1, A2, A3, I3
         from zope.component.tests.components import Content



More information about the Zope3-Checkins mailing list