Skip to content

Commit

Permalink
Added "trusted" option for defining trusted subscribers.
Browse files Browse the repository at this point in the history
Deprecated zope.security.trustedRemoveSecurityProxy and
zope.security.getProxiedObject. Use zope.security.removeSecurityProxy
instead.  (This should also be used rather than removeAllProxies.)
  • Loading branch information
Jim Fulton committed Aug 20, 2004
1 parent fc2d05d commit 67d6766
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 8 deletions.
6 changes: 5 additions & 1 deletion metaconfigure.py
Expand Up @@ -73,7 +73,8 @@ def proxify(ob, checker):

return ob

def subscriber(_context, factory, for_, provides=None, permission=None):
def subscriber(_context, factory, for_, provides=None, permission=None,
trusted=False):
factory = [factory]

if permission is not None:
Expand All @@ -98,6 +99,9 @@ def factory(ob):
ob = f(ob)
return ob

if trusted:
factory = TrustedAdapterFactory(factory)

_context.action(
discriminator = None,
callable = handler,
Expand Down
14 changes: 14 additions & 0 deletions metadirectives.py
Expand Up @@ -232,6 +232,20 @@ class ISubscriberDirective(zope.interface.Interface):
required=False
)

trusted = zope.configuration.fields.Bool(
title=u"Trusted",
description=u"""Make the subscriber a trusted subscriber
Trusted subscribers have unfettered access to the objects they
adapt. If asked to adapt security-proxied objects, then,
rather than getting an unproxied subscriber of security-proxied
objects, you get a security-proxied subscriber of unproxied
objects.
""",
required=False,
default=False,
)

class IUtilityDirective(IBasicComponentInformation):
"""Register a utility"""

Expand Down
55 changes: 48 additions & 7 deletions tests/test_directives.py
Expand Up @@ -28,7 +28,7 @@
from zope.configuration.xmlconfig import xmlconfig, XMLConfig
from zope.configuration.exceptions import ConfigurationError

from zope.proxy import getProxiedObject
from zope.security.proxy import removeSecurityProxy
from zope.security.proxy import getTestProxyItems

import zope.app.component
Expand Down Expand Up @@ -124,6 +124,47 @@ def testSubscriber(self):
self.assertEqual(a3.__class__, A3)
self.assertEqual(a3.context, (content, a1))

def testTrustedSubscriber(self):
from zope.app.component.tests.adapter import A1, A2, A3, I3
from zope.app.component.tests.adapter import IS
from zope.component.tests.components import Content

xmlconfig(StringIO(template % (
"""
<subscriber
provides="zope.app.component.tests.adapter.IS"
factory="zope.app.component.tests.adapter.A3"
for="zope.component.tests.components.IContent
zope.app.component.tests.adapter.I1"
trusted="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(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)


# around an unproxied object:
from zope.security.proxy import removeSecurityProxy
self.assert_(removeSecurityProxy(a3).context[0] is content)

def testSubscriber_w_no_provides(self):
from zope.app.component.tests.adapter import A1, A2, Handler, I3
from zope.component.tests.components import Content
Expand Down Expand Up @@ -211,7 +252,7 @@ def testTrustedAdapter(self):
"""
)))

# With an unproxied object, busoness as usual
# With an unproxied object, business as usual
ob = Content()
self.assertEqual(type(I1(ob)), type(A1()))

Expand All @@ -225,9 +266,9 @@ def testTrustedAdapter(self):
self.assertEqual(type(a), Proxy)

# around an unproxied object:
from zope.security.proxy import getProxiedObject
a = getProxiedObject(a)
a.context[0] is ob
from zope.security.proxy import removeSecurityProxy
a = removeSecurityProxy(a)
self.assert_(a.context[0] is ob)



Expand Down Expand Up @@ -398,7 +439,7 @@ def testProtectedAdapter(self):
adapter = ProxyFactory(IApp(Content()))
items = [item[0] for item in getTestProxyItems(adapter)]
self.assertEqual(items, ['a', 'f'])
self.assertEqual(getProxiedObject(adapter).__class__, Comp)
self.assertEqual(removeSecurityProxy(adapter).__class__, Comp)

def testAdapterUndefinedPermission(self):
config = StringIO(template % (
Expand Down Expand Up @@ -490,7 +531,7 @@ def testProtectedUtility(self):
utility = ProxyFactory(zapi.getUtility(IApp))
items = [item[0] for item in getTestProxyItems(utility)]
self.assertEqual(items, ['a', 'f'])
self.assertEqual(getProxiedObject(utility), comp)
self.assertEqual(removeSecurityProxy(utility), comp)

def testUtilityUndefinedPermission(self):
config = StringIO(template % (
Expand Down

0 comments on commit 67d6766

Please sign in to comment.