This repository has been archived by the owner on Mar 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathREADME.txt
181 lines (139 loc) · 5.02 KB
/
README.txt
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
============
Working Copy
============
The working copy is the area of the CMS where users actually edit content.
First we register the location utility:
>>> import zope.component
>>> import zope.interface.verify
>>> from zeit.cms.workingcopy.interfaces import IWorkingcopyLocation
>>> from zeit.cms.workingcopy.workingcopy import WorkingcopyLocation
>>> zope.interface.verify.verifyClass(
... IWorkingcopyLocation, WorkingcopyLocation)
True
>>> site_manager = zope.component.getSiteManager()
>>> location = WorkingcopyLocation()
>>> site_manager.registerUtility(location, IWorkingcopyLocation)
We also need a principal permission manager:
>>> class PPM(object):
...
... def __init__(self, context):
... self.context = context
...
... def grantPermissionToPrincipal(self, permission, principal):
... print "Granting %s to %s" % (permission, principal)
...
>>> import zope.securitypolicy.interfaces
>>> site_manager.registerAdapter(
... PPM, (zope.interface.Interface, ),
... zope.securitypolicy.interfaces.IPrincipalPermissionManager)
And we need a principal role manager:
>>> class PRM(object):
...
... def __init__(self, context):
... self.context = context
...
... def assignRoleToPrincipal(self, role, principal):
... print "Assigning role %s to %s" % (role, principal)
...
>>> site_manager.registerAdapter(
... PRM, (zope.interface.Interface, ),
... zope.securitypolicy.interfaces.IPrincipalRoleManager)
Adapting Principals
===================
Since every user has his own working area we have an adapter from IPrincipal to
IWorkingcopy. The working copy is created implicitly. This becomes visible by
the "Granting ..." messages:
>>> from zeit.cms.workingcopy.workingcopy import principalAdapter
>>> class Principal:
... id = 'hans'
...
>>> principal = Principal()
>>> workingcopy = principalAdapter(principal)
Granting zeit.EditContent to hans
Assigning role zeit.Owner to hans
>>> workingcopy
<zeit.cms.workingcopy.workingcopy.Workingcopy object at 0x...>
During adaptaion the Workingcopy object was automatically be added to the
location:
>>> print list(location.keys())
[u'hans']
>>> location[u'hans'] is workingcopy
True
When we adapt the Principal again, we'll also get the same workingcopy:
>>> workingcopy is principalAdapter(principal)
True
Getting the Workingcopy of the Currently Logged in User
=======================================================
Getting the Workingcopy of the currently logged in user is done by the
`getWorkingCopy` method of `WorkingcopyLocation`. Log in `kurt` and get his
workingcopy:
>>> import zeit.cms.testing
>>> principal = zeit.cms.testing.create_interaction(u'kurt')
>>> workingcopy = location.getWorkingcopy()
Granting zeit.EditContent to kurt
Assigning role zeit.Owner to kurt
>>> workingcopy
<zeit.cms.workingcopy.workingcopy.Workingcopy object at 0x...>
>>> workingcopy.__name__
u'kurt'
>>> print sorted(location.keys())
[u'hans', u'kurt']
>>> zope.security.management.endInteraction()
Identifying Local Content
=========================
Only content which provides ILocalContent can be added to the workingcopy:
>>> from zeit.cms.workingcopy.interfaces import ILocalContent
>>> from zeit.cms.repository.unknown import UnknownResource
>>> content = UnknownResource(u'oink')
>>> ILocalContent.providedBy(content)
False
>>> workingcopy[u'mycontent'] = content
Traceback (most recent call last):
...
ValueError: Must provide ILocalContent
After marking as ILocalContent it can be added:
>>> zope.interface.directlyProvides(content, ILocalContent)
>>> workingcopy[u'mycontent'] = content
>>> ILocalContent.providedBy(content)
True
Sorting
=======
When things are added to the workingcopy they are returned in reverse order.
Currently there is only one object in the database:
>>> list(workingcopy)
[u'mycontent']
Let's add another content:
>>> content = UnknownResource(u'oink')
>>> zope.interface.directlyProvides(content, ILocalContent)
>>> workingcopy[u'other-content'] = content
>>> list(workingcopy)
[u'other-content', u'mycontent']
>>> [v.__name__ for v in workingcopy.values()]
[u'other-content', u'mycontent']
Add another content:
>>> content = UnknownResource(u'oink')
>>> zope.interface.directlyProvides(content, ILocalContent)
>>> workingcopy[u'just-another-content'] = content
>>> list(workingcopy)
[u'just-another-content', u'other-content', u'mycontent']
>>> [v.__name__ for v in workingcopy.values()]
[u'just-another-content', u'other-content', u'mycontent']
Remove `other-content`:
>>> del workingcopy[u'other-content']
>>> list(workingcopy)
[u'just-another-content', u'mycontent']
>>> [v.__name__ for v in workingcopy.values()]
[u'just-another-content', u'mycontent']
Cleanup
=======
Cleanup after test:
>>> site_manager.unregisterUtility(location, IWorkingcopyLocation)
True
>>> site_manager.unregisterAdapter(
... PPM, (zope.interface.Interface, ),
... zope.securitypolicy.interfaces.IPrincipalPermissionManager)
True
>>> site_manager.unregisterAdapter(
... PRM, (zope.interface.Interface, ),
... zope.securitypolicy.interfaces.IPrincipalRoleManager)
True