~vcs-imports/zope.app.securitypolicy/trunk

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
Granting Example
================

In this example, we look at granting (or denying) principals (or
roles) to principals.

To make grants, we visit grant.html.  This doesn't display any 
grant information until we select a principal.

  >>> print http(r"""
  ... GET /@@grant.html HTTP/1.1
  ... Authorization: Basic mgr:mgrpw
  ... Referer: http://localhost:8081/@@contents.html
  ... """)
  HTTP/1.1 200 OK
  ...

If we submit a search request, we'll get a list of principals, from
which we can choose:

  >>> print http(r"""
  ... POST /@@grant.html HTTP/1.1
  ... Authorization: Basic mgr:mgrpw
  ... Content-Length: 117
  ... Content-Type: application/x-www-form-urlencoded
  ... Referer: http://localhost:8081/@@grant.html
  ... 
  ... field.principal.displayed=y"""
  ... "&field.principal.MA__.searchstring="
  ... "&field.principal.MA__.search=Search")
  HTTP/1.1 200 OK
  ...
  <option value="em9wZS5tZ3I_">Manager</option>
  ...

We can then choose one.  If we do so, we get output that includes form
elements for inputing security settings:

  >>> print http(r"""
  ... POST /@@grant.html HTTP/1.1
  ... Authorization: Basic mgr:mgrpw
  ... Content-Length: 62415
  ... Content-Type: application/x-www-form-urlencoded
  ... Referer: http://localhost:8081/@@grant.html
  ... 
  ... field.principal.displayed=y"""
  ... """&field.principal.MA__.searchstring="""
  ... """&field.principal.MA__.selection=em9wZS5tZ3I_"""
  ... """&field.principal.MA__.apply=Apply""")
  HTTP/1.1 200 OK
  ...
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.role.zope.Member.0" title="Allow">
  <input class="radioType" id="field.em9wZS5tZ3I_.role.zope.Member.0" name="field.em9wZS5tZ3I_.role.zope.Member" type="radio" value="allow" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="default">
  <center>
  <label for="field.em9wZS5tZ3I_.role.zope.Member.1" title="Unset">
  <input class="radioType" checked="checked" id="field.em9wZS5tZ3I_.role.zope.Member.1" name="field.em9wZS5tZ3I_.role.zope.Member" type="radio" value="unset" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.role.zope.Member.2" title="Deny">
  <input class="radioType" id="field.em9wZS5tZ3I_.role.zope.Member.2" name="field.em9wZS5tZ3I_.role.zope.Member" type="radio" value="deny" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  ...
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.permission.zope.ManageCode.0" title="Allow">
  <input class="radioType" id="field.em9wZS5tZ3I_.permission.zope.ManageCode.0" name="field.em9wZS5tZ3I_.permission.zope.ManageCode" type="radio" value="allow" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="default">
  <center>
  <label for="field.em9wZS5tZ3I_.permission.zope.ManageCode.1" title="Unset">
  <input class="radioType" checked="checked" id="field.em9wZS5tZ3I_.permission.zope.ManageCode.1" name="field.em9wZS5tZ3I_.permission.zope.ManageCode" type="radio" value="unset" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.permission.zope.ManageCode.2" title="Deny">
  <input class="radioType" id="field.em9wZS5tZ3I_.permission.zope.ManageCode.2" name="field.em9wZS5tZ3I_.permission.zope.ManageCode" type="radio" value="deny" onclick="changeMatrix(this);" />
  </label>
  </center>
  ...

Before we submit any data, there are no grants for the root object
except for a one made by the testing framework that grants the manager
role to the test manager:

  >>> root = getRootFolder()
  >>> import zope.app.securitypolicy.interfaces
  >>> grants = zope.app.securitypolicy.interfaces.IGrantInfo(root) 
  >>> grants.principalPermissionGrant('zope.mgr', 'zope.ManageCode')
  PermissionSetting: Unset
  >>> list(grants.getRolesForPrincipal('zope.mgr'))
  [('zope.Manager', PermissionSetting: Allow)]

Now, we can submit changes. (I've actually reduced the form input
to just the things we want to change to both limit the text here and
to reduce dependencies on specific roles and permissions:


  >>> print http(r"""
  ... POST /@@grant.html HTTP/1.1
  ... Authorization: Basic mgr:mgrpw
  ... Content-Length: 62437
  ... Content-Type: application/x-www-form-urlencoded
  ... Referer: http://localhost:8081/@@grant.html
  ... 
  ... field.principal=em9wZS5tZ3I_"""
  ... """&field.principal.displayed=y"""
  ... """&field.principal.MA__.searchstring="""
  ... """&field.em9wZS5tZ3I_.role.zope.Member=allow"""
  ... """&field.em9wZS5tZ3I_.role.zope.Member-empty-marker=1"""
  ... """&field.em9wZS5tZ3I_.permission.zope.ManageCode=deny"""
  ... """&field.em9wZS5tZ3I_.permission.zope.ManageCode-empty-marker=1"""
  ... """&GRANT_SUBMIT=Change""")
  HTTP/1.1 200 OK
  ...
  <td class="default">
  <center>
  <label for="field.em9wZS5tZ3I_.role.zope.Member.0" title="Allow">
  <input class="radioType" checked="checked" id="field.em9wZS5tZ3I_.role.zope.Member.0" name="field.em9wZS5tZ3I_.role.zope.Member" type="radio" value="allow" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.role.zope.Member.1" title="Unset">
  <input class="radioType" id="field.em9wZS5tZ3I_.role.zope.Member.1" name="field.em9wZS5tZ3I_.role.zope.Member" type="radio" value="unset" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.role.zope.Member.2" title="Deny">
  <input class="radioType" id="field.em9wZS5tZ3I_.role.zope.Member.2" name="field.em9wZS5tZ3I_.role.zope.Member" type="radio" value="deny" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  ...
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.permission.zope.ManageCode.0" title="Allow">
  <input class="radioType" id="field.em9wZS5tZ3I_.permission.zope.ManageCode.0" name="field.em9wZS5tZ3I_.permission.zope.ManageCode" type="radio" value="allow" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="">
  <center>
  <label for="field.em9wZS5tZ3I_.permission.zope.ManageCode.1" title="Unset">
  <input class="radioType" id="field.em9wZS5tZ3I_.permission.zope.ManageCode.1" name="field.em9wZS5tZ3I_.permission.zope.ManageCode" type="radio" value="unset" onclick="changeMatrix(this);" />
  </label>
  </center>
  </td>
  <BLANKLINE>
  <td class="default">
  <center>
  <label for="field.em9wZS5tZ3I_.permission.zope.ManageCode.2" title="Deny">
  <input class="radioType" checked="checked" id="field.em9wZS5tZ3I_.permission.zope.ManageCode.2" name="field.em9wZS5tZ3I_.permission.zope.ManageCode" type="radio" value="deny" onclick="changeMatrix(this);" />
  </label>
  </center>
  ...

And, if we check the grants, we see the changes:

  >>> grants = zope.app.securitypolicy.interfaces.IGrantInfo(root) 

  >>> grants.principalPermissionGrant('zope.mgr', 'zope.ManageCode')
  PermissionSetting: Deny

  >>> role_grants = list(grants.getRolesForPrincipal('zope.mgr'))
  >>> role_grants.sort()
  >>> role_grants
  ... # doctest: +NORMALIZE_WHITESPACE
  [('zope.Manager', PermissionSetting: Allow), 
   ('zope.Member', PermissionSetting: Allow)]