[Zope3-checkins]
SVN: Zope3/branches/dominik-locatableadapters/src/zope/app/component/
extend adapter directive with a locate attribute
Dominik Huber
dominik.huber at projekt01.ch
Thu May 19 12:12:04 EDT 2005
Log message for revision 30420:
extend adapter directive with a locate attribute
Changed:
U Zope3/branches/dominik-locatableadapters/src/zope/app/component/metaconfigure.py
U Zope3/branches/dominik-locatableadapters/src/zope/app/component/metadirectives.py
U Zope3/branches/dominik-locatableadapters/src/zope/app/component/tests/test_directives.py
-=-
Modified: Zope3/branches/dominik-locatableadapters/src/zope/app/component/metaconfigure.py
===================================================================
--- Zope3/branches/dominik-locatableadapters/src/zope/app/component/metaconfigure.py 2005-05-19 15:45:08 UTC (rev 30419)
+++ Zope3/branches/dominik-locatableadapters/src/zope/app/component/metaconfigure.py 2005-05-19 16:12:04 UTC (rev 30420)
@@ -31,6 +31,7 @@
from zope.app import zapi
from zope.app.security.adapter import LocatingTrustedAdapterFactory
from zope.app.security.adapter import LocatingUntrustedAdapterFactory
+from zope.app.security.adapter import TrustedAdapterFactory
PublicPermission = 'zope.Public'
@@ -62,7 +63,7 @@
_handler=handler
def subscriber(_context, for_=None, factory=None, handler=None, provides=None,
- permission=None, trusted=False):
+ permission=None, trusted=False, locate=False):
if factory is None:
@@ -113,14 +114,15 @@
return ob
# invoke custom adapter factories
- if trusted:
- # trusted adapters that requires dedicated permission all the time
- factory = LocatingTrustedAdapterFactory(factory)
+ if locate or (permission is not None and permission is not CheckerPublic):
+ if trusted:
+ factory = LocatingTrustedAdapterFactory(factory)
+ else:
+ factory = LocatingUntrustedAdapterFactory(factory)
+ else:
+ if trusted:
+ factory = TrustedAdapterFactory(factory)
- elif permission is not None and permission is not CheckerPublic:
- # untrusted adapters that requires any dedicated permission
- factory = LocatingUntrustedAdapterFactory(factory)
-
_context.action(
discriminator = None,
callable = _handler,
@@ -145,7 +147,7 @@
)
def adapter(_context, factory, provides=None, for_=None, permission=None,
- name='', trusted=False):
+ name='', trusted=False, locate=False):
if for_ is None:
if len(factory) == 1:
@@ -184,15 +186,15 @@
factory = _protectedFactory(factory, checker)
# invoke custom adapter factories
- if trusted:
- # trusted adapters that requires dedicated permission all the time
- factory = LocatingTrustedAdapterFactory(factory)
+ if locate or (permission is not None and permission is not CheckerPublic):
+ if trusted:
+ factory = LocatingTrustedAdapterFactory(factory)
+ else:
+ factory = LocatingUntrustedAdapterFactory(factory)
+ else:
+ if trusted:
+ factory = TrustedAdapterFactory(factory)
- elif permission is not None and permission is not CheckerPublic:
- # untrusted adapters that requires any dedicated permission
- factory = LocatingUntrustedAdapterFactory(factory)
-
-
_context.action(
discriminator = ('adapter', for_, provides, name),
callable = handler,
Modified: Zope3/branches/dominik-locatableadapters/src/zope/app/component/metadirectives.py
===================================================================
--- Zope3/branches/dominik-locatableadapters/src/zope/app/component/metadirectives.py 2005-05-19 15:45:08 UTC (rev 30419)
+++ Zope3/branches/dominik-locatableadapters/src/zope/app/component/metadirectives.py 2005-05-19 16:12:04 UTC (rev 30420)
@@ -219,6 +219,17 @@
default=False,
)
+ locate = zope.configuration.fields.Bool(
+ title=_("Locate"),
+ description=_("""Make the adapter a locatable adapter
+
+ Located adapter should be used if a non-public permission
+ is used.
+ """),
+ required=False,
+ default=False,
+ )
+
class ISubscriberDirective(zope.interface.Interface):
"""
Register a subscriber
@@ -273,6 +284,17 @@
default=False,
)
+ locate = zope.configuration.fields.Bool(
+ title=_("Locate"),
+ description=_("""Make the adapter a locatable adapter
+
+ Located adapter should be used if a non-public permission
+ is used.
+ """),
+ required=False,
+ default=False,
+ )
+
class IUtilityDirective(IBasicComponentInformation):
"""Register a utility."""
Modified: Zope3/branches/dominik-locatableadapters/src/zope/app/component/tests/test_directives.py
===================================================================
--- Zope3/branches/dominik-locatableadapters/src/zope/app/component/tests/test_directives.py 2005-05-19 15:45:08 UTC (rev 30419)
+++ Zope3/branches/dominik-locatableadapters/src/zope/app/component/tests/test_directives.py 2005-05-19 16:12:04 UTC (rev 30420)
@@ -205,9 +205,48 @@
self.assertEqual(type(a3), Proxy)
- # around an unproxied object:
+ # behind the security proxy is no locatin proxy:
from zope.security.proxy import removeSecurityProxy
self.assert_(removeSecurityProxy(a3).context[0] is content)
+ self.assertEqual(type(removeSecurityProxy(a3)).__name__, 'A3')
+
+
+ def testLocatableTrustedSubscriber(self):
+ xmlconfig(StringIO(template % (
+ '''
+ <subscriber
+ provides="zope.app.component.tests.adapter.IS"
+ factory="zope.app.component.tests.adapter.A3"
+ for="zope.app.component.tests.components.IContent
+ zope.app.component.tests.adapter.I1"
+ trusted="yes"
+ locate="yes"
+ />
+ '''
+ )))
+ # With an unproxied object, business as usual
+ content = Content()
+ a1 = A1()
+ subscribers = zapi.subscribers((content, a1), IS)
+
+ a3 = subscribers[0]
+
+ self.assertEqual(a3.__class__, A3)
+ self.assertEqual(type(a3).__name__, 'A3')
+ self.assertEqual(a3.context, (content, a1))
+
+ # Now with a proxied object:
+ from zope.security.checker import ProxyFactory
+ p = ProxyFactory(content)
+
+ # we get a proxied subscriber:
+ a3 = zapi.subscribers((p, a1), IS)[0]
+ from zope.security.proxy import Proxy
+ self.assertEqual(type(a3), Proxy)
+
+ # behind the security proxy is a locatio proxy:
+ from zope.security.proxy import removeSecurityProxy
+ self.assert_(removeSecurityProxy(a3).context[0] is content)
self.assertEqual(type(removeSecurityProxy(a3)).__name__, 'LocationProxy')
def testSubscriber_w_no_provides(self):
@@ -291,6 +330,29 @@
self.assertEqual(IApp(Content()).__class__, Comp)
+ def testAdapterWithPermission(self):
+ # Full import is critical!
+ self.assertEqual(IV(Content(), None), None)
+
+ xmlconfig(StringIO(template % (
+ '''
+ <permission
+ id="y.x"
+ title="XY"
+ description="Allow XY." />
+
+ <adapter
+ factory="zope.app.component.tests.components.Comp"
+ provides="zope.app.component.tests.components.IApp"
+ for="zope.app.component.tests.components.IContent"
+ permission="y.x"
+ />
+ '''
+ )))
+
+ self.assertEqual(IApp(Content()).__class__, Comp)
+ self.assertEqual(type(IApp(Content())).__name__, 'LocationProxy')
+
def testAdapter_wo_provides_or_for(self):
# Full import is critical!
self.assertEqual(IV(Content(), None), None)
@@ -360,11 +422,117 @@
# around an unproxied object:
from zope.security.proxy import removeSecurityProxy
a = removeSecurityProxy(a)
+ self.assertEqual(type(a).__name__, 'A1')
+ self.assert_(a.context[0] is ob)
+
+
+ def testTrustedAdapterWithPermission(self):
+ # Full import is critical!
+ xmlconfig(StringIO(template % (
+ '''
+ <permission
+ id="y.x"
+ title="XY"
+ description="Allow XY." />
+
+ <adapter
+ factory="zope.app.component.tests.adapter.A1"
+ provides="zope.app.component.tests.adapter.I1"
+ for="zope.app.component.tests.components.IContent"
+ permission="y.x"
+ trusted="yes"
+ />
+ '''
+ )))
+
+ # With an unproxied object, business as usual
+ ob = Content()
+ self.assertEqual(type(I1(ob)).__name__, 'A1')
+
+ # Now with a proxied object:
+ from zope.security.checker import ProxyFactory
+ p = ProxyFactory(ob)
+
+ # we get a proxied adapter:
+ a = I1(p)
+ from zope.security.proxy import Proxy
+ self.assertEqual(type(a), Proxy)
+
+ # behind the security proxy is location proxy
+ # if non-public permission is used
+ from zope.security.proxy import removeSecurityProxy
+ a = removeSecurityProxy(a)
self.assertEqual(type(a).__name__, 'LocationProxy')
self.assert_(a.context[0] is ob)
-
-
+
+ def testTrustedAdapterWithPublicPermission(self):
+ # Full import is critical!
+ xmlconfig(StringIO(template % (
+ '''
+ <adapter
+ factory="zope.app.component.tests.adapter.A1"
+ provides="zope.app.component.tests.adapter.I1"
+ for="zope.app.component.tests.components.IContent"
+ permission="zope.Public"
+ trusted="yes"
+ />
+ '''
+ )))
+
+ # With an unproxied object, business as usual
+ ob = Content()
+ self.assertEqual(type(I1(ob)).__name__, 'A1')
+
+ # Now with a proxied object:
+ from zope.security.checker import ProxyFactory
+ p = ProxyFactory(ob)
+
+ # we get a proxied adapter:
+ a = I1(p)
+ from zope.security.proxy import Proxy
+ self.assertEqual(type(a), Proxy)
+
+ # behind the security proxy is no location proxy
+ from zope.security.proxy import removeSecurityProxy
+ a = removeSecurityProxy(a)
+ self.assertEqual(type(a).__name__, 'A1')
+ self.assert_(a.context[0] is ob)
+
+
+ def testLocatableTrustedAdapter(self):
+ # Full import is critical!
+ xmlconfig(StringIO(template % (
+ '''
+ <adapter
+ factory="zope.app.component.tests.adapter.A1"
+ provides="zope.app.component.tests.adapter.I1"
+ for="zope.app.component.tests.components.IContent"
+ trusted="yes"
+ locate="yes"
+ />
+ '''
+ )))
+
+ # With an unproxied object, business as usual
+ ob = Content()
+ self.assertEqual(type(I1(ob)).__name__, 'A1')
+
+ # Now with a proxied object:
+ from zope.security.checker import ProxyFactory
+ p = ProxyFactory(ob)
+
+ # we get a proxied adapter:
+ a = I1(p)
+ from zope.security.proxy import Proxy
+ self.assertEqual(type(a), Proxy)
+
+ # behind the security proxy is always location proxy:
+ from zope.security.proxy import removeSecurityProxy
+ a = removeSecurityProxy(a)
+ self.assertEqual(type(a).__name__, 'LocationProxy')
+ self.assert_(a.context[0] is ob)
+
def testAdapter_w_multiple_factories(self):
xmlconfig(StringIO(template % (
'''
More information about the Zope3-Checkins
mailing list