This file is indexed.

/usr/share/pyshared/zope/app/authentication/groupfolder.txt is in python-zope.app.authentication 3.9-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
=============
Group Folders
=============

Group folders provide support for groups information stored in the ZODB.  They
are persistent, and must be contained within the PAUs that use them.

Like other principals, groups are created when they are needed.

Group folders contain group-information objects that contain group information.
We create group information using the `GroupInformation` class:

  >>> import zope.app.authentication.groupfolder
  >>> g1 = zope.app.authentication.groupfolder.GroupInformation("Group 1")

  >>> groups = zope.app.authentication.groupfolder.GroupFolder('group.')
  >>> groups['g1'] = g1

Note that when group-info is added, a GroupAdded event is generated:

  >>> from zope.app.authentication import interfaces
  >>> from zope.component.eventtesting import getEvents
  >>> getEvents(interfaces.IGroupAdded)
  [<GroupAdded 'group.g1'>]

Groups are defined with respect to an authentication service.  Groups
must be accessible via an authentication service and can contain
principals accessible via an authentication service.

To illustrate the group interaction with the authentication service,
we'll create a sample authentication service:

  >>> from zope import interface
  >>> from zope.authentication.interfaces import IAuthentication
  >>> from zope.authentication.interfaces import PrincipalLookupError
  >>> from zope.security.interfaces import IGroupAwarePrincipal
  >>> from zope.app.authentication.groupfolder import setGroupsForPrincipal

  >>> class Principal:
  ...     interface.implements(IGroupAwarePrincipal)
  ...     def __init__(self, id, title='', description=''):
  ...         self.id, self.title, self.description = id, title, description
  ...         self.groups = []

  >>> class PrincipalCreatedEvent:
  ...     def __init__(self, authentication, principal):
  ...         self.authentication = authentication
  ...         self.principal = principal

  >>> from zope.app.authentication import principalfolder

  >>> class Principals:
  ...
  ...     interface.implements(IAuthentication)
  ...
  ...     def __init__(self, groups, prefix='auth.'):
  ...         self.prefix = prefix
  ...         self.principals = {
  ...            'p1': principalfolder.PrincipalInfo('p1', '', '', ''),
  ...            'p2': principalfolder.PrincipalInfo('p2', '', '', ''),
  ...            'p3': principalfolder.PrincipalInfo('p3', '', '', ''),
  ...            'p4': principalfolder.PrincipalInfo('p4', '', '', ''),
  ...            }
  ...         self.groups = groups
  ...         groups.__parent__ = self
  ...
  ...     def getAuthenticatorPlugins(self):
  ...         return [('principals', self.principals), ('groups', self.groups)]
  ...
  ...     def getPrincipal(self, id):
  ...         if not id.startswith(self.prefix):
  ...             raise PrincipalLookupError(id)
  ...         id = id[len(self.prefix):]
  ...         info = self.principals.get(id)
  ...         if info is None:
  ...             info = self.groups.principalInfo(id)
  ...             if info is None:
  ...                raise PrincipalLookupError(id)
  ...         principal = Principal(self.prefix+info.id, 
  ...                               info.title, info.description)
  ...         setGroupsForPrincipal(PrincipalCreatedEvent(self, principal))
  ...         return principal

This class doesn't really implement the full `IAuthentication` interface, but
it implements the `getPrincipal` method used by groups. It works very much
like the pluggable authentication utility.  It creates principals on demand. It
calls `setGroupsForPrincipal`, which is normally called as an event subscriber,
when principals are created. In order for `setGroupsForPrincipal` to find out
group folder, we have to register it as a utility:

  >>> from zope.app.authentication.interfaces import IAuthenticatorPlugin
  >>> from zope.component import provideUtility
  >>> provideUtility(groups, IAuthenticatorPlugin)

We will create and register a new principals utility:

  >>> principals = Principals(groups)
  >>> provideUtility(principals, IAuthentication)

Now we can set the principals on the group:

  >>> g1.principals = ['auth.p1', 'auth.p2']
  >>> g1.principals
  ('auth.p1', 'auth.p2')

Adding principals fires an event.

  >>> getEvents(interfaces.IPrincipalsAddedToGroup)[-1]
  <PrincipalsAddedToGroup ['auth.p1', 'auth.p2'] u'auth.group.g1'>

We can now look up groups for the principals:

  >>> groups.getGroupsForPrincipal('auth.p1')
  (u'group.g1',)

Note that the group id is a concatenation of the group-folder prefix
and the name of the group-information object within the folder.

If we delete a group:

  >>> del groups['g1']

then the groups folder loses the group information for that group's
principals:

  >>> groups.getGroupsForPrincipal('auth.p1')
  ()

but the principal information on the group is unchanged:

  >>> g1.principals
  ('auth.p1', 'auth.p2')

It also fires an event showing that the principals are removed from the group
(g1 is group information, not a zope.security.interfaces.IGroup).

  >>> getEvents(interfaces.IPrincipalsRemovedFromGroup)[-1]
  <PrincipalsRemovedFromGroup ['auth.p1', 'auth.p2'] u'auth.group.g1'>

Adding the group sets the folder principal information.  Let's use a
different group name:

  >>> groups['G1'] = g1

  >>> groups.getGroupsForPrincipal('auth.p1')
  (u'group.G1',)

Here we see that the new name is reflected in the group information.

An event is fired, as usual.

  >>> getEvents(interfaces.IPrincipalsAddedToGroup)[-1]
  <PrincipalsAddedToGroup ['auth.p1', 'auth.p2'] u'auth.group.G1'>

In terms of member events (principals added and removed from groups), we have
now seen that events are fired when a group information object is added and
when it is removed from a group folder; and we have seen that events are fired
when a principal is added to an already-registered group.  Events are also
fired when a principal is removed from an already-registered group.  Let's
quickly see some more examples.

  >>> g1.principals = ('auth.p1', 'auth.p3', 'auth.p4')
  >>> getEvents(interfaces.IPrincipalsAddedToGroup)[-1]
  <PrincipalsAddedToGroup ['auth.p3', 'auth.p4'] u'auth.group.G1'>
  >>> getEvents(interfaces.IPrincipalsRemovedFromGroup)[-1]
  <PrincipalsRemovedFromGroup ['auth.p2'] u'auth.group.G1'>
  >>> g1.principals = ('auth.p1', 'auth.p2')
  >>> getEvents(interfaces.IPrincipalsAddedToGroup)[-1]
  <PrincipalsAddedToGroup ['auth.p2'] u'auth.group.G1'>
  >>> getEvents(interfaces.IPrincipalsRemovedFromGroup)[-1]
  <PrincipalsRemovedFromGroup ['auth.p3', 'auth.p4'] u'auth.group.G1'>

Groups can contain groups:

  >>> g2 = zope.app.authentication.groupfolder.GroupInformation("Group Two")
  >>> groups['G2'] = g2
  >>> g2.principals = ['auth.group.G1']

  >>> groups.getGroupsForPrincipal('auth.group.G1')
  (u'group.G2',)

  >>> old = getEvents(interfaces.IPrincipalsAddedToGroup)[-1]
  >>> old
  <PrincipalsAddedToGroup ['auth.group.G1'] u'auth.group.G2'>

Groups cannot contain cycles:

  >>> g1.principals = ('auth.p1', 'auth.p2', 'auth.group.G2')
  ... # doctest: +NORMALIZE_WHITESPACE
  Traceback (most recent call last):
  ...
  GroupCycle: (u'auth.group.G1', 
               ['auth.p2', u'auth.group.G1', u'auth.group.G2'])

Trying to do so does not fire an event.

  >>> getEvents(interfaces.IPrincipalsAddedToGroup)[-1] is old
  True

They need not be hierarchical:

  >>> ga = zope.app.authentication.groupfolder.GroupInformation("Group A")
  >>> groups['GA'] = ga

  >>> gb = zope.app.authentication.groupfolder.GroupInformation("Group B")
  >>> groups['GB'] = gb
  >>> gb.principals = ['auth.group.GA']

  >>> gc = zope.app.authentication.groupfolder.GroupInformation("Group C")
  >>> groups['GC'] = gc
  >>> gc.principals = ['auth.group.GA']

  >>> gd = zope.app.authentication.groupfolder.GroupInformation("Group D")
  >>> groups['GD'] = gd
  >>> gd.principals = ['auth.group.GA', 'auth.group.GB']

  >>> ga.principals = ['auth.p1']

Group folders provide a very simple search interface.  They perform
simple string searches on group titles and descriptions.

  >>> list(groups.search({'search': 'grou'})) # doctest: +NORMALIZE_WHITESPACE
  [u'group.G1', u'group.G2',
   u'group.GA', u'group.GB', u'group.GC', u'group.GD']

  >>> list(groups.search({'search': 'two'}))
  [u'group.G2']

They also support batching:

  >>> list(groups.search({'search': 'grou'}, 2, 3))
  [u'group.GA', u'group.GB', u'group.GC']


If you don't supply a search key, no results will be returned:

  >>> list(groups.search({}))
  []

Identifying groups
------------------
The function, `setGroupsForPrincipal`, is a subscriber to
principal-creation events.  It adds any group-folder-defined groups to
users in those groups:

  >>> principal = principals.getPrincipal('auth.p1')

  >>> principal.groups
  [u'auth.group.G1', u'auth.group.GA']

Of course, this applies to groups too:

  >>> principal = principals.getPrincipal('auth.group.G1')
  >>> principal.id
  'auth.group.G1'

  >>> principal.groups
  [u'auth.group.G2']

In addition to setting principal groups, the `setGroupsForPrincipal`
function also declares the `IGroup` interface on groups:

  >>> [iface.__name__ for iface in interface.providedBy(principal)]
  ['IGroup', 'IGroupAwarePrincipal']

  >>> [iface.__name__
  ...  for iface in interface.providedBy(principals.getPrincipal('auth.p1'))]
  ['IGroupAwarePrincipal']

Special groups
--------------
Two special groups, Authenticated, and Everyone may apply to users
created by the pluggable-authentication utility.  There is a
subscriber, specialGroups, that will set these groups on any non-group
principals if IAuthenticatedGroup, or IEveryoneGroup utilities are
provided.

Lets define a group-aware principal:

  >>> import zope.security.interfaces
  >>> class GroupAwarePrincipal(Principal):
  ...     interface.implements(zope.security.interfaces.IGroupAwarePrincipal)
  ...     def __init__(self, id):
  ...         Principal.__init__(self, id)
  ...         self.groups = []

If we notify the subscriber with this principal, nothing will happen
because the groups haven't been defined:

  >>> prin = GroupAwarePrincipal('x')
  >>> event = interfaces.FoundPrincipalCreated(42, prin, {})
  >>> zope.app.authentication.groupfolder.specialGroups(event)
  >>> prin.groups
  []

Now, if we define the Everybody group:

  >>> import zope.authentication.interfaces
  >>> class EverybodyGroup(Principal):
  ...     interface.implements(zope.authentication.interfaces.IEveryoneGroup)

  >>> everybody = EverybodyGroup('all')
  >>> provideUtility(everybody, zope.authentication.interfaces.IEveryoneGroup)

Then the group will be added to the principal:

  >>> zope.app.authentication.groupfolder.specialGroups(event)
  >>> prin.groups
  ['all']

Similarly for the authenticated group:

  >>> class AuthenticatedGroup(Principal):
  ...     interface.implements(
  ...         zope.authentication.interfaces.IAuthenticatedGroup)

  >>> authenticated = AuthenticatedGroup('auth')
  >>> provideUtility(authenticated, zope.authentication.interfaces.IAuthenticatedGroup)

Then the group will be added to the principal:

  >>> prin.groups = []
  >>> zope.app.authentication.groupfolder.specialGroups(event)
  >>> prin.groups.sort()
  >>> prin.groups
  ['all', 'auth']

These groups are only added to non-group principals:

  >>> prin.groups = []
  >>> interface.directlyProvides(prin, zope.security.interfaces.IGroup)
  >>> zope.app.authentication.groupfolder.specialGroups(event)
  >>> prin.groups
  []

And they are only added to group aware principals:

  >>> class SolitaryPrincipal:
  ...     interface.implements(zope.security.interfaces.IPrincipal)
  ...     id = title = description = ''

  >>> event = interfaces.FoundPrincipalCreated(42, SolitaryPrincipal(), {})
  >>> zope.app.authentication.groupfolder.specialGroups(event)
  >>> prin.groups
  []

Member-aware groups
-------------------
The groupfolder includes a subscriber that gives group principals the
zope.security.interfaces.IGroupAware interface and an implementation thereof.
This allows groups to be able to get and set their members.

Given an info object and a group...

    >>> class DemoGroupInformation(object):
    ...     interface.implements(
    ...         zope.app.authentication.groupfolder.IGroupInformation)
    ...     def __init__(self, title, description, principals):
    ...         self.title = title
    ...         self.description = description
    ...         self.principals = principals
    ...
    >>> i = DemoGroupInformation(
    ...     'Managers', 'Taskmasters', ('joe', 'jane'))
    ...
    >>> info = zope.app.authentication.groupfolder.GroupInfo(
    ...     'groups.managers', i)
    >>> class DummyGroup(object):
    ...     interface.implements(IGroupAwarePrincipal)
    ...     def __init__(self, id, title=u'', description=u''):
    ...         self.id = id
    ...         self.title = title
    ...         self.description = description
    ...         self.groups = []
    ...
    >>> principal = DummyGroup('foo')
    >>> zope.security.interfaces.IMemberAwareGroup.providedBy(principal)
    False

...when you call the subscriber, it adds the two pseudo-methods to the
principal and makes the principal provide the IMemberAwareGroup interface.

    >>> zope.app.authentication.groupfolder.setMemberSubscriber(
    ...     interfaces.FoundPrincipalCreated(
    ...         'dummy auth (ignored)', principal, info))
    >>> principal.getMembers()
    ('joe', 'jane')
    >>> principal.setMembers(('joe', 'jane', 'jaimie'))
    >>> principal.getMembers()
    ('joe', 'jane', 'jaimie')
    >>> zope.security.interfaces.IMemberAwareGroup.providedBy(principal)
    True

The two methods work with the value on the IGroupInformation object.

    >>> i.principals == principal.getMembers()
    True

Limitation
==========

The current group-folder design has an important limitation!

There is no point in assigning principals to a group 
from a group folder unless the principal is from the same pluggable
authentication utility.

o If a principal is from a higher authentication utility, the user
  will not get the group definition. Why? Because the principals
  group assignments are set when the principal is authenticated. At
  that point, the current site is the site containing the principal
  definition. Groups defined in lower sites will not be consulted,

o It is impossible to assign users from lower authentication
  utilities because they can't be seen when managing the group,
  from the site containing the group.

A better design might be to store user-role assignments independent of
the group definitions and to look for assignments during (url)
traversal.  This could get quite complex though.

While it is possible to have multiple authentication utilities long a
URL path, it is generally better to stick to a simpler model in which
there is only one authentication utility along a URL path (in addition
to the global utility, which is used for bootstrapping purposes).