zope.security.permission

class zope.security.permission.Permission(id, title='', description='')[source]
zope.security.permission.checkPermission(context, permission_id)[source]

Check whether a given permission exists in the provided context.

>>> from zope.security.permission import checkPermission
>>> from zope.component import provideUtility
>>> from zope.security.interfaces import IPermission
>>> from zope.security.permission import Permission
>>> x = Permission('x')
>>> provideUtility(x, IPermission, 'x')

>>> checkPermission(None, 'x')
>>> checkPermission(None, 'y')
Traceback (most recent call last):
...
ValueError: ('Undefined permission id', 'y')

The zope.security.checker.CheckerPublic permission always exists:

>>> from zope.security.checker import CheckerPublic
>>> checkPermission(None, CheckerPublic)
zope.security.permission.allPermissions(context=None)[source]

Get the ids of all defined permissions

>>> from zope.security.permission import allPermissions
>>> from zope.component import provideUtility
>>> y = Permission('y')
>>> provideUtility(y, IPermission, 'y')

>>> ids = list(allPermissions(None))
>>> ids.sort()
>>> ids
['x', 'y']
zope.security.permission.PermissionsVocabulary(context=None)[source]

A vocabulary of permission IDs.

Term values are permissions, while term tokens are permission IDs.

To illustrate, we need to register the permissions vocabulary:

>>> from zope.security.permission import PermissionsVocabulary
>>> from zope.schema.vocabulary import _clear
>>> _clear()

>>> from zope.schema.vocabulary import getVocabularyRegistry
>>> registry = getVocabularyRegistry()
>>> registry.register('Permissions', PermissionsVocabulary)

We can now lookup the permissions we created earlier using the vocabulary:

>>> vocab = registry.get(None, 'Permissions')
>>> vocab.getTermByToken('x').value is x
True
>>> vocab.getTermByToken('y').value is y
True
zope.security.permission.PermissionIdsVocabulary(context=None)[source]

A vocabulary of permission IDs.

Term values are the permission ID strings except for ‘zope.Public’, which is the global permission CheckerPublic.

Term titles are the permission ID strings except for ‘zope.Public’, which is shortened to ‘Public’.

Terms are sorted by title except for ‘Public’, which always appears as the first term.

To illustrate, we need to register the permission IDs vocabulary:

>>> from zope.security.permission import PermissionIdsVocabulary
>>> registry.register('Permission Ids', PermissionIdsVocabulary)

We also need to register the special 'zope.Public' permission:

>>> provideUtility(Permission('zope.Public'), IPermission, 'zope.Public')

We can now lookup these permissions using the vocabulary:

>>> vocab = registry.get(None, 'Permission Ids')

The non-public permissions ‘x’ and ‘y’ are string values:

>>> vocab.getTermByToken('x').value
'x'
>>> vocab.getTermByToken('y').value
'y'

However, the public permission value is CheckerPublic:

>>> vocab.getTermByToken('zope.Public').value is CheckerPublic
True

and its title is shortened:

>>> vocab.getTermByToken('zope.Public').title
'Public'

The terms are sorted by title except for the public permission, which is listed first:

>>> [term.title for term in vocab]
['Public', 'x', 'y']