zope.security.zcml

Most users will not directly need to access the contents of this module; they will probably just configure via ZCML.

API Reference

class zope.security.zcml.Permission(*args, **kw)[source]

This field describes a permission.

Pass in field values as keyword parameters.

Generally, you want to pass either a title and description, or a doc string. If you pass no doc string, it will be computed from the title and description. If you pass a doc string that follows the Python coding style (title line separated from the body by a blank line), the title and description will be computed from the doc string. Unfortunately, the doc string must be passed as a positional argument.

Here are some examples:

>>> from zope.schema._bootstrapfields import Field
>>> f = Field()
>>> f.__doc__, str(f.title), str(f.description)
('', '', '')
>>> f = Field(title=u'sample')
>>> str(f.__doc__), str(f.title), str(f.description)
('sample', 'sample', '')
>>> f = Field(title=u'sample', description=u'blah blah\nblah')
>>> str(f.__doc__), str(f.title), str(f.description)
('sample\n\nblah blah\nblah', 'sample', 'blah blah\nblah')

Let’s look at an example:

>>> from zope.security.zcml import Permission
>>> class FauxContext(object):
...     permission_mapping = {'zope.ManageCode':'zope.private'}
...     _actions = []
...     def action(self, **kws):
...        self._actions.append(kws)
>>> context = FauxContext()
>>> field = Permission().bind(context)

Let’s test the fromUnicode method:

>>> field.fromUnicode(u'zope.foo')
'zope.foo'
>>> field.fromUnicode(u'zope.ManageCode')
'zope.private'

Now let’s see whether validation works alright

>>> field._validate('zope.ManageCode')
>>> context._actions[0]['args']
(None, 'zope.foo')

>>> from zope.schema.interfaces import InvalidId
>>> try:
...     field._validate('3 foo')
... except InvalidId as e:
...     e
InvalidId('3 foo')

zope.Public is always valid
>>> field._validate('zope.Public')
interface zope.security.zcml.ISecurityPolicyDirective[source]

Defines the security policy that will be used for Zope.

zope.security.zcml.securityPolicy(_context, component)[source]
interface zope.security.zcml.IPermissionDirective[source]

Define a new security object.

zope.security.zcml.permission(_context, id, title, description='')[source]
interface zope.security.zcml.IRedefinePermission[source]

Define a permission to replace another permission.

zope.security.zcml.redefinePermission(_context, from_, to)[source]

Configuring security via ZCML

zope.security provides a ZCML file that configures some utilities and a couple of standard permissions:

>>> from zope.component import getGlobalSiteManager
>>> from zope.configuration.xmlconfig import XMLConfig
>>> from zope.component.testing import setUp
>>> import zope.security
>>> setUp()  # clear global component registry
>>> XMLConfig('permissions.zcml', zope.security)()

>>> len(list(getGlobalSiteManager().registeredUtilities()))
7

Clear the current state:

>>> from zope.component.testing import setUp, tearDown
>>> tearDown()
>>> setUp()

>>> XMLConfig('configure.zcml', zope.security)()

>>> len(list(getGlobalSiteManager().registeredUtilities()))
10