Skip to content

Commit 85225d0

Browse files
committed
Tidy permissions.md
1 parent dcdd699 commit 85225d0

File tree

1 file changed

+61
-89
lines changed

1 file changed

+61
-89
lines changed
Lines changed: 61 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,45 @@
11
---
22
myst:
33
html_meta:
4-
"description": ""
5-
"property=og:description": ""
6-
"property=og:title": ""
7-
"keywords": ""
4+
"description": "How to set up add permissions, view permissions, and field view/edit permissions for Plone content types"
5+
"property=og:description": "How to set up add permissions, view permissions, and field view/edit permissions for Plone content types"
6+
"property=og:title": "How to set up add permissions, view permissions, and field view/edit permissions for Plone content types"
7+
"keywords": "Plone, Permissions, content types"
88
---
99

1010
# Permissions
1111

12-
**Setting up add permissions, view permissions and field view/edit permissions**
13-
14-
Plone’s security system is based on the concept of
15-
*permissions* protecting *operations*
16-
(like accessing a view,
17-
viewing a field,
18-
modifying a field,
19-
or adding a type of content)
20-
that are granted to *roles*,
21-
which in turn are granted to *users* and/or *groups*.
22-
In the context of developing content types,
23-
permissions are typically used in three different ways:
24-
25-
- A content type or group of related content types often has a custom
26-
*add permission* which controls who can add this type of content.
27-
- Views (including forms) are sometimes protected by custom
28-
permissions.
29-
- Individual fields are sometimes protected by permissions,
30-
so that some users can view and edit fields that others can’t see.
12+
This chapter describes how to set up add permissions, view permissions, and field view/edit permissions.
13+
14+
Plone's security system is based on the concept of *permissions* protecting *operations*.
15+
These operations include accessing a view, viewing a field, modifying a field, or adding a type of content.
16+
Permissions are granted to *roles*, which in turn are granted to *users* or *groups*.
17+
In the context of developing content types, permissions are typically used in three different ways.
18+
19+
- A content type or group of related content types often has a custom *add permission* which controls who can add this type of content.
20+
- Views (including forms) are sometimes protected by custom permissions.
21+
- Individual fields are sometimes protected by permissions, so that some users can view and edit fields that others can't see.
3122

3223
It is easy to create new permissions.
3324
However, be aware that it is considered good practice to use the standard permissions wherever possible and use *workflow* to control which roles are granted these permissions on a per-instance basis.
3425

35-
For more basic information on permissions and how to create custom permissions read the [Security Section] in the Plone documentation.
26+
For more basic information on permissions and how to create custom permissions read the [Security Section](https://5.docs.plone.org/develop/plone/security/index.html) in the Plone documentation.
27+
3628

3729
## Performing permission checks in code
3830

39-
It is sometimes necessary to check permissions explicitly in code, for
40-
example in a view. A permission check always checks a permission on a
41-
context object, since permissions can change with workflow.
31+
It is sometimes necessary to check permissions explicitly in code, for example, in a view.
32+
A permission check always checks a permission on a context object, since permissions can change with workflow.
4233

43-
:::{note}
44-
Never make security dependent on users roles directly.
34+
```{note}
35+
Never make security dependent on users' roles directly.
4536
Always check for a permission, and assign the permission to the appropriate role or roles.
46-
:::
37+
```
4738

48-
As an example,
49-
let’s display a message on the view of a `Session` type
50-
if the user has the `cmf.RequestReview` permission.
51-
In `session.py`, we update the `View` class with the following:
39+
As an example, let's display a message on the view of a `Session` type if the user has the `cmf.RequestReview` permission.
40+
In {file}`session.py`, we update the `View` class with the following.
5241

53-
```
42+
```python
5443
from zope.security import checkPermission
5544

5645
class View(BrowserView):
@@ -59,67 +48,58 @@ class View(BrowserView):
5948
return checkPermission('cmf.RequestReview', self.context)
6049
```
6150

62-
And in the `session_templates/view.pt` template, we add:
51+
And in the {file}`session_templates/view.pt` template, we add the following.
6352

64-
```html
53+
```xml
6554
<div class="discreet"
6655
tal:condition="view/canRequestReview"
6756
i18n:translate="suggest_review">
6857
Please submit this for review.
6958
</div>
7059
```
7160

61+
7262
## Content type add permissions
7363

74-
Dexterity content types’ add permissions are set in the FTI,
75-
using the `add_permission` property.
76-
This can be changed through the web
77-
or in the GenericSetup import step for the content type.
64+
Dexterity content types' add permissions are set in the FTI, using the `add_permission` property.
65+
This can be changed through the web or in the GenericSetup import step for the content type.
7866

79-
To make the `Session` type use our new permission, we modify the
80-
`add_permission` line in
81-
`profiles/default/example.conference.session.xml`:
67+
To make the `Session` type use our new permission, we modify the `add_permission` line in {file}`profiles/default/example.conference.session.xml`.
8268

8369
```xml
8470
<property name="add_permission">example.conference.AddSession</property>
8571
```
8672

73+
8774
## Protecting views and forms
8875

89-
Access to views and other browser resources (like viewlets or portlets)
90-
can be protected by permissions, either using the `permission` attribute
91-
on ZCML statements like:
76+
Access to views and other browser resources such as viewlets or portlets can be protected by permissions, either using the `permission` attribute on ZCML statements such as the following
9277

93-
```
78+
```xml
9479
<browser:page
95-
...
9680
permission="zope.Public"
97-
...
9881
/>
9982
```
10083

10184
We could also use the special `zope.Public` permission name to make the view accessible to anyone.
10285

86+
10387
## Protecting form fields
10488

105-
Individual fields in a schema may be associated with a *read* permission
106-
and a *write* permission.
107-
The read permission is used to control access to the field’s value via protected code
108-
(e.g. scripts or templates created through the web)
109-
and URL traversal,
110-
and can be used to control the appearance of fields when using display forms
111-
(if you use custom views that access the attribute directly, you’ll need to perform your own checks).
112-
Write permissions can be used to control whether or not a given field appears on a type’s add and edit forms.
89+
Individual fields in a schema may be associated with a *read* permission and a *write* permission.
90+
The read permission is used to control access to the field's value via protected code, such as scripts or templates created through the web, and URL traversal.
91+
It can be used to control the appearance of fields when using display forms.
92+
If you use custom views that access the attribute directly, you'll need to perform your own checks.
93+
Write permissions can be used to control whether or not a given field appears on a type's add and edit forms.
11394

114-
In both cases,
115-
read and write permissions are annotated onto the schema using directives similar to those we’ve already seen for form widget hints.
116-
The `read_permission()` and `write_permission()` directives are found in the [plone.autoform] package.
95+
In both cases, read and write permissions are annotated onto the schema using directives similar to those we've already seen for form widget hints.
96+
The `read_permission()` and `write_permission()` directives are found in the [`plone.autoform`](https://pypi.org/project/plone.autoform/) package.
11797

118-
If XML-schemas are used for defintion see {ref}`Dexterity XML: security attributes <dexterity-xml-security>`.
98+
If XML schemas are used for definitions, see [Dexterity XML, supermodel/security attributes](https://5.docs.plone.org/external/plone.app.dexterity/docs/reference/dexterity-xml.html#supermodel-security-attributes).
11999

120-
Simple example protecting a field to be readable for Site Administrators only:
100+
The following example protects a field to be readable for Site Administrators only.
121101

122-
```
102+
```python
123103
from zope import schema
124104
from plone.supermodel import model
125105
from plone.autoform.directives import read_permission, write_permission
@@ -133,13 +113,12 @@ class IExampleProtectedInformation(model):
133113
)
134114
```
135115

136-
As a complex example, let’s add a field for *Session* reviewers to record the track for a session.
137-
We’ll store the vocabulary of available tracks on the parent `Program` object in a text field,
138-
so that the creator of the `Program` can choose the available tracks.
116+
As a complex example, let's add a field for *Session* reviewers to record the track for a session.
117+
We'll store the vocabulary of available tracks on the parent `Program` object in a text field, so that the creator of the `Program` can choose the available tracks.
139118

140-
First, we add this to the `IProgram` schema in `program.py`:
119+
First, we add this to the `IProgram` schema in {file}`program.py`.
141120

142-
```
121+
```python
143122
form.widget(tracks=TextLinesFieldWidget)
144123
tracks = schema.List(
145124
title=_("Tracks"),
@@ -149,21 +128,21 @@ tracks = schema.List(
149128
)
150129
```
151130

152-
The `TextLinesFieldWidget` is used to edit a list of text lines in a
153-
text area. It is imported as:
131+
The `TextLinesFieldWidget` is used to edit a list of text lines in a text area.
132+
It is imported as shown.
154133

155-
```
134+
```python
156135
from plone.z3cform.textlines.textlines import TextLinesFieldWidget
157136
```
158137

159-
Next, well add a vocabulary for this to `session.py`:
138+
Next, we'll add a vocabulary for this to {file}`session.py`:
160139

161-
```
140+
```python
162141
from Acquisition import aq_inner, aq_parent
163142
from zope.component import provider
164143
from zope.schema.interfaces import IContextSourceBinder
165144
from zope.schema.vocabulary import SimpleVocabulary
166-
...
145+
167146

168147
@provider(IContextSourceBinder)
169148
def possibleTracks(context):
@@ -180,16 +159,13 @@ def possibleTracks(context):
180159
return SimpleVocabulary.fromValues(values)
181160
```
182161

183-
This vocabulary finds the closest `IProgram`
184-
(in the add form, the `context` will be the `Program`,
185-
but on the edit form, it will be the `Session`,
186-
so we need to check the parent)
187-
and uses its `tracks` variable as the vocabulary.
162+
This vocabulary finds the closest `IProgram`.
163+
In the add form, the `context` will be the `Program`, but on the edit form, it will be the `Session`, so we need to check the parent.
164+
It uses its `tracks` variable as the vocabulary.
188165

189-
Next, we add a field to the `ISession` interface in the same file and
190-
protect it with the relevant write permission:
166+
Next, we add a field to the `ISession` interface in the same file and protect it with the relevant write permission.
191167

192-
```
168+
```python
193169
write_permission(track='example.conference.ModifyTrack')
194170
track = schema.Choice(
195171
title=_("Track"),
@@ -198,9 +174,5 @@ track = schema.Choice(
198174
)
199175
```
200176

201-
With this in place, users with the `example.conference: Modify track`
202-
permission should be able to edit tracks for a session. For everyone
203-
else, the field will be hidden in the edit form.
204-
205-
[plone.autoform]: http://pypi.python.org/pypi/plone.autoform
206-
[security section]: http://docs.plone.org/develop/plone/security/index.html
177+
With this in place, users with the `example.conference: Modify track` permission should be able to edit tracks for a session.
178+
For everyone else, the field will be hidden in the edit form.

0 commit comments

Comments
 (0)