summaryrefslogtreecommitdiff
path: root/docs/api/permission.rst
blob: 268f8f64fd79788482c8ec7034e1352068239e95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
:mod:`zope.security.permission`
===============================

.. testsetup::

   from zope.component.testing import setUp
   setUp()

.. autoclass:: zope.security.permission.Permission
   :members:
   :member-order: bysource


.. autofunction:: zope.security.permission.checkPermission

.. doctest::

   >>> 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 :data:`zope.security.checker.CheckerPublic` permission always exists:

.. doctest::

   >>> from zope.security.checker import CheckerPublic
   >>> checkPermission(None, CheckerPublic)


.. autofunction:: zope.security.permission.allPermissions

.. doctest::

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


.. autofunction:: zope.security.permission.PermissionsVocabulary

To illustrate, we need to register the permissions vocabulary:

.. doctest::

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

.. doctest::

   >>> vocab = registry.get(None, 'Permissions')
   >>> vocab.getTermByToken('x').value is x
   True
   >>> vocab.getTermByToken('y').value is y
   True


.. autofunction:: zope.security.permission.PermissionIdsVocabulary

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

.. doctest::

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

.. doctest::

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

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

.. doctest::

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

However, the public permission value is CheckerPublic:

.. doctest::

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

and its title is shortened:

.. doctest::

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

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

.. doctest::

   >>> for term in vocab:
   ...    print(term.title)
   Public
   x
   y


.. testcleanup::

   from zope.component.testing import tearDown
   tearDown()