Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Respect permission value for utility factory registrations #55

Merged
merged 5 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
Changes
=========

4.7.0 (unreleased)
5.0.0 (unreleased)
==================

- Respect permission value for utility factory registrations
(`#54 <https://github.com/zopefoundation/zope.component/issues/54>`_)

- Add support for Python 3.9


Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def read(*rnames):

setup(
name='zope.component',
version='4.7.0.dev0',
version='5.0.0.dev0',
url='https://github.com/zopefoundation/zope.component',
project_urls={
'Documentation': 'https://zopecomponent.readthedocs.io/',
Expand Down
37 changes: 37 additions & 0 deletions src/zope/component/tests/test_zcml.py
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,43 @@ class Foo(object):
self.assertEqual(action['discriminator'], None)
self.assertEqual(action['args'], ('', IFoo))

@skipIfNoSecurity
def test_w_factory_w_provides_w_permission(self):
from zope.interface import Interface
from zope.component.interface import provideInterface
from zope.component.zcml import handler
class IFoo(Interface):
pass
class Foo(object):
pass
_cfg_ctx = _makeConfigContext()
self._callFUT(_cfg_ctx, factory=Foo,
provides=IFoo, permission='testing')
self.assertEqual(len(_cfg_ctx._actions), 2)
self.assertEqual(_cfg_ctx._actions[0][0], ())
# Register the utility
action =_cfg_ctx._actions[0][1]
self.assertEqual(action['callable'], handler)
self.assertEqual(action['discriminator'], ('utility', IFoo, ''))
self.assertEqual(action['args'][0], 'registerUtility')
self.assertEqual(action['args'][1], None)
self.assertEqual(action['args'][2], IFoo)
self.assertEqual(action['args'][3], '')
self.assertEqual(action['args'][4], 'TESTING')
fctry = action['kw']['factory']
# Factory is wrapped
self.assertNotEqual(fctry, Foo)
self.assertEqual(fctry.factory, Foo)
secured = fctry()
self.assertIsInstance(secured, Foo)
# Register the provided interface
self.assertEqual(_cfg_ctx._actions[1][0], ())
action =_cfg_ctx._actions[1][1]
self.assertEqual(action['callable'], provideInterface)
self.assertEqual(action['discriminator'], None)
self.assertEqual(action['args'], ('', IFoo))


class Test_interface(unittest.TestCase):

def _callFUT(self, *args, **kw):
Expand Down
6 changes: 5 additions & 1 deletion src/zope/component/zcml.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,11 @@ def utility(_context, provides=None, component=None, factory=None,
name = getName(component)

if permission is not None:
component = proxify(component, provides=provides, permission=permission)
if component:
component = proxify(component, provides=provides,
permission=permission)
if factory:
factory = protectedFactory(factory, provides, permission)

_context.action(
discriminator = ('utility', provides, name),
Expand Down