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

Bases: object

Default implementation of zope.security.interfaces.IPermission.

zope.security.permission.checkPermission(context, permission_id)[source]

Check whether a given permission object exists in the provided context as a utility.

>>> 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)

Get the IDs of all defined permission object utilities.

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

>>> ids = sorted(allPermissions(None))
>>> for perm in sorted(allPermissions(None)):
...     print(perm)

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
>>> vocab.getTermByToken('y').value is y

A vocabulary of permission IDs.

Term values are the permission ID strings except for zope.Public, which is the global permission zope.security.checker.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:

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

However, the public permission value is CheckerPublic:

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

and its title is shortened:

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

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

>>> for term in vocab:
...    print(term.title)