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

« back to all changes in this revision

Viewing changes to src/zope/app/securitypolicy/browser/granting.txt

  • Committer: rogerineichen
  • Date: 2007-09-26 14:51:44 UTC
  • Revision ID: vcs-imports@canonical.com-20070926145144-lyl0lnz4xl2jb41i
Revert my latest changes, was the wrong checkout base

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Granting View
 
2
=============
 
3
 
 
4
The granting view allows the user to grant permissions and roles to
 
5
principals. The view unfortunately depends on a lot of other components:
 
6
 
 
7
  - Roles
 
8
 
 
9
    >>> from zope.app.testing import ztapi
 
10
    >>> from zope.app.securitypolicy.role import Role
 
11
    >>> from zope.app.securitypolicy.interfaces import IRole
 
12
    >>> ztapi.provideUtility(IRole, Role(u'role1', u'Role 1'), u'role1')
 
13
    >>> ztapi.provideUtility(IRole, Role(u'role2', u'Role 2'), u'role2')
 
14
    >>> ztapi.provideUtility(IRole, Role(u'role3', u'Role 3'), u'role3')
 
15
 
 
16
  - Permissions
 
17
 
 
18
    >>> from zope.security.permission import Permission
 
19
    >>> from zope.security.interfaces import IPermission
 
20
    >>> ztapi.provideUtility(IPermission, Permission(u'permission1',
 
21
    ...                      u'Permission 1'), u'permission1')
 
22
    >>> ztapi.provideUtility(IPermission, Permission(u'permission2',
 
23
    ...                      u'Permission 2'), u'permission2')
 
24
    >>> ztapi.provideUtility(IPermission, Permission(u'permission3',
 
25
    ...                      u'Permission 3'), u'permission3')
 
26
 
 
27
  - Authentication Utility
 
28
 
 
29
    >>> class Principal:
 
30
    ...     def __init__(self, id, title): self.id, self.title = id, title
 
31
 
 
32
    >>> from zope.app.security.interfaces import IAuthentication
 
33
    >>> from zope.app.security.interfaces import PrincipalLookupError
 
34
    >>> from zope.interface import implements
 
35
    >>> class AuthUtility:
 
36
    ...     implements(IAuthentication)
 
37
    ...     data = {'jim': Principal('jim', 'Jim Fulton'),
 
38
    ...             'stephan': Principal('stephan', 'Stephan Richter')}
 
39
    ...
 
40
    ...     def getPrincipal(self, id):
 
41
    ...         try:
 
42
    ...             return self.data.get(id)
 
43
    ...         except KeyError:
 
44
    ...             raise PrincipalLookupError(id)
 
45
    ...
 
46
    ...     def getPrincipals(self, search):
 
47
    ...         return [principal
 
48
    ...                 for principal in self.data.values()
 
49
    ...                 if search in principal.title]
 
50
 
 
51
    >>> ztapi.provideUtility(IAuthentication, AuthUtility())
 
52
 
 
53
  - Security-related Adapters
 
54
 
 
55
    >>> from zope.annotation.interfaces import IAnnotatable
 
56
    >>> from zope.app.securitypolicy.interfaces import IPrincipalRoleManager
 
57
    >>> from zope.app.securitypolicy.principalrole import \
 
58
    ...     AnnotationPrincipalRoleManager
 
59
 
 
60
    >>> ztapi.provideAdapter(IAnnotatable, IPrincipalRoleManager,
 
61
    ...                      AnnotationPrincipalRoleManager)
 
62
 
 
63
    >>> from zope.app.securitypolicy.interfaces import \
 
64
    ...     IPrincipalPermissionManager
 
65
    >>> from zope.app.securitypolicy.principalpermission import \
 
66
    ...     AnnotationPrincipalPermissionManager
 
67
 
 
68
    >>> ztapi.provideAdapter(IAnnotatable, IPrincipalPermissionManager,
 
69
    ...                      AnnotationPrincipalPermissionManager)
 
70
 
 
71
  - Vocabulary Choice Widgets
 
72
 
 
73
    >>> from zope.schema.interfaces import IChoice
 
74
    >>> from zope.app.form.browser import ChoiceInputWidget
 
75
    >>> from zope.app.form.interfaces import IInputWidget
 
76
    >>> ztapi.browserViewProviding(IChoice, ChoiceInputWidget, IInputWidget)
 
77
 
 
78
    >>> from zope.schema.interfaces import IVocabularyTokenized
 
79
    >>> from zope.publisher.interfaces.browser import IBrowserRequest
 
80
    >>> from zope.app.form.browser import DropdownWidget
 
81
    >>> ztapi.provideMultiView((IChoice, IVocabularyTokenized),
 
82
    ...                        IBrowserRequest, IInputWidget, '',
 
83
    ...                        DropdownWidget)
 
84
 
 
85
  - Support Views for the Principal Source Widget
 
86
 
 
87
    >>> from zope.app.security.interfaces import IPrincipalSource
 
88
    >>> from zope.app.security.browser.principalterms import PrincipalTerms
 
89
    >>> from zope.app.form.browser.interfaces import ITerms
 
90
    >>> ztapi.browserViewProviding(IPrincipalSource, PrincipalTerms, ITerms)
 
91
 
 
92
    >>> from zope.app.security.browser.auth import AuthUtilitySearchView
 
93
    >>> from zope.app.form.browser.interfaces import ISourceQueryView
 
94
    >>> ztapi.browserViewProviding(IAuthentication,
 
95
    ...                            AuthUtilitySearchView,
 
96
    ...                            ISourceQueryView)
 
97
 
 
98
 
 
99
    >>> from zope.schema.interfaces import ISource
 
100
    >>> from zope.app.form.browser.source import SourceInputWidget
 
101
    >>> ztapi.provideMultiView((IChoice, ISource), IBrowserRequest,
 
102
    ...                        IInputWidget, '', SourceInputWidget)
 
103
 
 
104
  - Attribute Annotatable Adapter
 
105
 
 
106
    >>> from zope.app.testing import setup
 
107
    >>> setup.setUpAnnotations()
 
108
    >>> setup.setUpSiteManagerLookup()
 
109
 
 
110
  - Content Object
 
111
 
 
112
    >>> from zope.annotation.interfaces import IAttributeAnnotatable
 
113
    >>> class Content:
 
114
    ...     implements(IAttributeAnnotatable)
 
115
    ...     __annotations__ = {}
 
116
 
 
117
  (This is Jim's understanding of a "easy" setup!)
 
118
 
 
119
Now that we have all the components we need, let's create *the* view.
 
120
 
 
121
  >>> ob = Content()
 
122
  >>> from zope.publisher.browser import TestRequest
 
123
  >>> request = TestRequest()
 
124
 
 
125
  >>> from zope.app.securitypolicy.browser.granting import Granting
 
126
  >>> view = Granting(ob, request)
 
127
 
 
128
If we call status, we get nothing and the view's principal attribute is `None`:
 
129
 
 
130
  >>> view.status()
 
131
  u''
 
132
  >>> view.principal
 
133
 
 
134
Since we have not selected a principal, we have no role or permission widgets:
 
135
 
 
136
  >>> getattr(view, 'roles', None)
 
137
  >>> getattr(view, 'permissions', None)
 
138
 
 
139
Now that we have a selected principal, then
 
140
 
 
141
 
 
142
  >>> view.request.form['field.principal.displayed'] = 'y'
 
143
  >>> view.request.form['field.principal'] = 'amlt'
 
144
 
 
145
(Yes, 'amlt' is the base 64 code for 'jim'.)
 
146
 
 
147
  >>> view.status()
 
148
  u''
 
149
 
 
150
and now the `view.principal` is set:
 
151
 
 
152
  >>> view.principal
 
153
  'jim'
 
154
 
 
155
Now we should have a list of role and permission widgets, and all of them
 
156
should be unset, because do not have any settings for 'jim'.
 
157
 
 
158
  >>> [role.context.title for role in view.roles]
 
159
  [u'Role 1', u'Role 2', u'Role 3']
 
160
  >>> [perm.context.title for perm in view.permissions]
 
161
  [u'Permission 1', u'Permission 2', u'Permission 3']
 
162
 
 
163
Now we change some settings and submit the form:
 
164
 
 
165
  >>> from zope.app.securitypolicy.interfaces import Allow, Deny, Unset
 
166
 
 
167
  >>> view.request.form['field.amlt.role.role1'] = 'unset'
 
168
  >>> view.request.form['field.amlt.role.role1-empty-makrer'] = 1
 
169
  >>> view.request.form['field.amlt.role.role2'] = 'allow'
 
170
  >>> view.request.form['field.amlt.role.role2-empty-makrer'] = 1
 
171
  >>> view.request.form['field.amlt.role.role3'] = 'deny'
 
172
  >>> view.request.form['field.amlt.role.role3-empty-makrer'] = 1
 
173
 
 
174
  >>> view.request.form['field.amlt.permission.permission1'] = 'unset'
 
175
  >>> view.request.form['field.amlt.permission.permission1-empty-makrer'] = 1
 
176
  >>> view.request.form['field.amlt.permission.permission2'] = 'allow'
 
177
  >>> view.request.form['field.amlt.permission.permission2-empty-makrer'] = 1
 
178
  >>> view.request.form['field.amlt.permission.permission3'] = 'deny'
 
179
  >>> view.request.form['field.amlt.permission.permission3-empty-makrer'] = 1
 
180
 
 
181
  >>> view.request.form['GRANT_SUBMIT'] = 'Submit'
 
182
 
 
183
If we get the status now, the data should be written and a status message
 
184
should be returned:
 
185
 
 
186
  >>> view.status()
 
187
  u'Grants updated.'
 
188
 
 
189
  >>> roles = IPrincipalRoleManager(ob)
 
190
  >>> roles.getSetting('role1', 'jim') is Unset
 
191
  True
 
192
  >>> roles.getSetting('role2', 'jim') is Allow
 
193
  True
 
194
  >>> roles.getSetting('role3', 'jim') is Deny
 
195
  True
 
196
 
 
197
  >>> roles = IPrincipalPermissionManager(ob)
 
198
  >>> roles.getSetting('permission1', 'jim') is Unset
 
199
  True
 
200
  >>> roles.getSetting('permission2', 'jim') is Allow
 
201
  True
 
202
  >>> roles.getSetting('permission3', 'jim') is Deny
 
203
  True