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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
|
# Copyright 2009 Canonical Ltd. This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).
"""Database class for table ArchivePermission."""
__metaclass__ = type
__all__ = [
'ArchivePermission',
'ArchivePermissionSet',
]
from sqlobject import (
BoolCol,
ForeignKey,
)
from storm.expr import SQL
from storm.locals import (
Int,
Reference,
)
from storm.store import Store
from zope.component import getUtility
from zope.interface import (
alsoProvides,
implements,
)
from canonical.database.constants import UTC_NOW
from canonical.database.datetimecol import UtcDateTimeCol
from canonical.database.enumcol import EnumCol
from canonical.database.sqlbase import (
SQLBase,
sqlvalues,
)
from lp.services.database.lpstorm import (
IMasterStore,
IStore,
)
from canonical.launchpad.webapp.interfaces import (
DEFAULT_FLAVOR,
IStoreSelector,
MAIN_STORE,
)
from lp.app.errors import NotFoundError
from lp.registry.interfaces.distribution import IDistributionSet
from lp.registry.interfaces.sourcepackagename import (
ISourcePackageName,
ISourcePackageNameSet,
)
from lp.soyuz.enums import ArchivePermissionType
from lp.soyuz.interfaces.archive import (
ComponentNotFound,
IArchive,
)
from lp.soyuz.interfaces.archivepermission import (
IArchivePermission,
IArchivePermissionSet,
IArchiveQueueAdmin,
IArchiveUploader,
)
from lp.soyuz.interfaces.component import (
IComponent,
IComponentSet,
)
from lp.soyuz.interfaces.packageset import IPackageset
from lp.soyuz.model.packageset import Packageset
def _extract_type_name(value):
"""Extract the type name of the given value."""
return str(type(value)).split("'")[-2]
class ArchivePermission(SQLBase):
"""See `IArchivePermission`."""
implements(IArchivePermission)
_table = 'ArchivePermission'
_defaultOrder = 'id'
date_created = UtcDateTimeCol(
dbName='date_created', notNull=True, default=UTC_NOW)
archive = ForeignKey(foreignKey='Archive', dbName='archive', notNull=True)
permission = EnumCol(
dbName='permission', unique=False, notNull=True,
schema=ArchivePermissionType)
person = ForeignKey(foreignKey='Person', dbName='person', notNull=True)
component = ForeignKey(
foreignKey='Component', dbName='component', notNull=False)
sourcepackagename = ForeignKey(
foreignKey='SourcePackageName', dbName='sourcepackagename',
notNull=False)
packageset_id = Int(name='packageset', allow_none=True)
packageset = Reference(packageset_id, 'Packageset.id')
explicit = BoolCol(dbName='explicit', notNull=True, default=False)
def _init(self, *args, **kw):
"""Provide the right interface for URL traversal."""
SQLBase._init(self, *args, **kw)
# Provide the additional marker interface depending on what type
# of archive this is. See also the browser:url declarations in
# zcml/archivepermission.zcml.
if self.permission == ArchivePermissionType.UPLOAD:
alsoProvides(self, IArchiveUploader)
elif self.permission == ArchivePermissionType.QUEUE_ADMIN:
alsoProvides(self, IArchiveQueueAdmin)
else:
raise AssertionError(
"Unknown permission type %s" % self.permission)
@property
def component_name(self):
"""See `IArchivePermission`"""
if self.component:
return self.component.name
else:
return None
@property
def source_package_name(self):
"""See `IArchivePermission`"""
if self.sourcepackagename:
return self.sourcepackagename.name
else:
return None
@property
def package_set_name(self):
"""See `IArchivePermission`"""
if self.packageset:
return self.packageset.name
else:
return None
@property
def distro_series_name(self):
"""See `IArchivePermission`"""
if self.packageset:
return self.packageset.distroseries.name
else:
return None
class ArchivePermissionSet:
"""See `IArchivePermissionSet`."""
implements(IArchivePermissionSet)
def checkAuthenticated(self, person, archive, permission, item):
"""See `IArchivePermissionSet`."""
clauses = ["""
ArchivePermission.archive = %s AND
ArchivePermission.permission = %s AND
ArchivePermission.person = TeamParticipation.team AND
TeamParticipation.person = %s
""" % sqlvalues(archive, permission, person)]
prejoins = []
if IComponent.providedBy(item):
clauses.append(
"ArchivePermission.component = %s" % sqlvalues(item))
prejoins.append("component")
elif ISourcePackageName.providedBy(item):
clauses.append(
"ArchivePermission.sourcepackagename = %s" % sqlvalues(item))
prejoins.append("sourcepackagename")
elif IPackageset.providedBy(item):
clauses.append(
"ArchivePermission.packageset = %s" % sqlvalues(item.id))
prejoins.append("packageset")
else:
raise AssertionError(
"'item' %r is not an IComponent, IPackageset or an "
"ISourcePackageName" % item)
query = " AND ".join(clauses)
auth = ArchivePermission.select(
query, clauseTables=["TeamParticipation"],
prejoins=prejoins)
return auth
def _nameToComponent(self, component):
"""Helper to convert a possible string component to IComponent"""
try:
if isinstance(component, basestring):
component = getUtility(IComponentSet)[component]
return component
except NotFoundError:
raise ComponentNotFound(component)
def _nameToSourcePackageName(self, sourcepackagename):
"""Helper to convert a possible string name to ISourcePackageName."""
if isinstance(sourcepackagename, basestring):
sourcepackagename = getUtility(
ISourcePackageNameSet)[sourcepackagename]
return sourcepackagename
def permissionsForPerson(self, archive, person):
"""See `IArchivePermissionSet`."""
store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
return store.find(
ArchivePermission, """
ArchivePermission.archive = %s AND
EXISTS (SELECT TeamParticipation.person
FROM TeamParticipation
WHERE TeamParticipation.person = %s AND
TeamParticipation.team = ArchivePermission.person)
""" % sqlvalues(archive, person))
def _componentsFor(self, archives, person, permission_type):
"""Helper function to get ArchivePermission objects."""
if IArchive.providedBy(archives):
archive_ids = [archives.id]
else:
archive_ids = [archive.id for archive in archives]
return ArchivePermission.select("""
ArchivePermission.archive IN %s AND
ArchivePermission.permission = %s AND
ArchivePermission.component IS NOT NULL AND
EXISTS (SELECT TeamParticipation.person
FROM TeamParticipation
WHERE TeamParticipation.person = %s AND
TeamParticipation.team = ArchivePermission.person)
""" % sqlvalues(archive_ids, permission_type, person),
prejoins=["component"])
def componentsForUploader(self, archive, person):
"""See `IArchivePermissionSet`,"""
return self._componentsFor(
archive, person, ArchivePermissionType.UPLOAD)
def uploadersForComponent(self, archive, component=None):
"See `IArchivePermissionSet`."""
clauses = ["""
ArchivePermission.archive = %s AND
ArchivePermission.permission = %s
""" % sqlvalues(archive, ArchivePermissionType.UPLOAD)]
if component is not None:
component = self._nameToComponent(component)
clauses.append(
"ArchivePermission.component = %s" % sqlvalues(component))
else:
clauses.append("ArchivePermission.component IS NOT NULL")
query = " AND ".join(clauses)
return ArchivePermission.select(query, prejoins=["component"])
def packagesForUploader(self, archive, person):
"""See `IArchive`."""
return ArchivePermission.select("""
ArchivePermission.archive = %s AND
ArchivePermission.permission = %s AND
ArchivePermission.sourcepackagename IS NOT NULL AND
EXISTS (SELECT TeamParticipation.person
FROM TeamParticipation
WHERE TeamParticipation.person = %s AND
TeamParticipation.team = ArchivePermission.person)
""" % sqlvalues(archive, ArchivePermissionType.UPLOAD, person),
prejoins=["sourcepackagename"])
def uploadersForPackage(self, archive, sourcepackagename):
"See `IArchivePermissionSet`."""
sourcepackagename = self._nameToSourcePackageName(sourcepackagename)
results = ArchivePermission.selectBy(
archive=archive, permission=ArchivePermissionType.UPLOAD,
sourcepackagename=sourcepackagename)
return results.prejoin(["sourcepackagename"])
def queueAdminsForComponent(self, archive, component):
"See `IArchivePermissionSet`."""
component = self._nameToComponent(component)
results = ArchivePermission.selectBy(
archive=archive, permission=ArchivePermissionType.QUEUE_ADMIN,
component=component)
return results.prejoin(["component"])
def componentsForQueueAdmin(self, archive, person):
"""See `IArchivePermissionSet`."""
return self._componentsFor(
archive, person, ArchivePermissionType.QUEUE_ADMIN)
def newPackageUploader(self, archive, person, sourcepackagename):
"""See `IArchivePermissionSet`."""
sourcepackagename = self._nameToSourcePackageName(sourcepackagename)
existing = self.checkAuthenticated(
person, archive, ArchivePermissionType.UPLOAD, sourcepackagename)
try:
return existing[0]
except IndexError:
return ArchivePermission(
archive=archive, person=person,
sourcepackagename=sourcepackagename,
permission=ArchivePermissionType.UPLOAD)
def newComponentUploader(self, archive, person, component):
"""See `IArchivePermissionSet`."""
component = self._nameToComponent(component)
existing = self.checkAuthenticated(
person, archive, ArchivePermissionType.UPLOAD, component)
try:
return existing[0]
except IndexError:
return ArchivePermission(
archive=archive, person=person, component=component,
permission=ArchivePermissionType.UPLOAD)
def newQueueAdmin(self, archive, person, component):
"""See `IArchivePermissionSet`."""
component = self._nameToComponent(component)
existing = self.checkAuthenticated(
person, archive, ArchivePermissionType.QUEUE_ADMIN, component)
try:
return existing[0]
except IndexError:
return ArchivePermission(
archive=archive, person=person, component=component,
permission=ArchivePermissionType.QUEUE_ADMIN)
@staticmethod
def _remove_permission(permission):
if permission is None:
# The permission has already been removed, so there's nothing more
# to do here.
return
else:
Store.of(permission).remove(permission)
def deletePackageUploader(self, archive, person, sourcepackagename):
"""See `IArchivePermissionSet`."""
sourcepackagename = self._nameToSourcePackageName(sourcepackagename)
permission = ArchivePermission.selectOneBy(
archive=archive, person=person,
sourcepackagename=sourcepackagename,
permission=ArchivePermissionType.UPLOAD)
self._remove_permission(permission)
def deleteComponentUploader(self, archive, person, component):
"""See `IArchivePermissionSet`."""
component = self._nameToComponent(component)
permission = ArchivePermission.selectOneBy(
archive=archive, person=person, component=component,
permission=ArchivePermissionType.UPLOAD)
self._remove_permission(permission)
def deleteQueueAdmin(self, archive, person, component):
"""See `IArchivePermissionSet`."""
component = self._nameToComponent(component)
permission = ArchivePermission.selectOneBy(
archive=archive, person=person, component=component,
permission=ArchivePermissionType.QUEUE_ADMIN)
self._remove_permission(permission)
def _nameToPackageset(self, packageset):
"""Helper to convert a possible string name to IPackageset."""
if isinstance(packageset, basestring):
# A package set name was passed, assume the current distro series.
ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
name = packageset
store = IStore(Packageset)
packageset = store.find(
Packageset, name=name,
distroseries=ubuntu.currentseries).one()
if packageset is not None:
return packageset
else:
raise NotFoundError("No such package set '%s'" % name)
elif IPackageset.providedBy(packageset):
return packageset
else:
raise ValueError(
'Not a package set: %s' % _extract_type_name(packageset))
def packagesetsForUploader(self, archive, person):
"""See `IArchivePermissionSet`."""
store = IStore(ArchivePermission)
query = '''
SELECT ap.id
FROM archivepermission ap, teamparticipation tp
WHERE
ap.person = tp.team AND tp.person = ?
AND ap.archive = ?
AND ap.packageset IS NOT NULL
'''
query = SQL(query, (person.id, archive.id))
return store.find(
ArchivePermission, ArchivePermission.id.is_in(query))
def uploadersForPackageset(
self, archive, packageset, direct_permissions=True):
"""See `IArchivePermissionSet`."""
packageset = self._nameToPackageset(packageset)
store = IStore(ArchivePermission)
if direct_permissions == True:
query = '''
SELECT ap.id FROM archivepermission ap WHERE ap.packageset = ?
'''
else:
query = '''
SELECT ap.id
FROM archivepermission ap, flatpackagesetinclusion fpsi
WHERE fpsi.child = ? AND ap.packageset = fpsi.parent
'''
query += " AND ap.archive = ?"
query = SQL(query, (packageset.id, archive.id))
return store.find(
ArchivePermission, ArchivePermission.id.is_in(query))
def newPackagesetUploader(
self, archive, person, packageset, explicit=False):
"""See `IArchivePermissionSet`."""
packageset = self._nameToPackageset(packageset)
store = IMasterStore(ArchivePermission)
# First see whether we have a matching permission in the database
# already.
query = '''
SELECT ap.id
FROM archivepermission ap, teamparticipation tp
WHERE
ap.person = tp.team AND tp.person = ?
AND ap.packageset = ? AND ap.archive = ?
'''
query = SQL(query, (person.id, packageset.id, archive.id))
permissions = list(
store.find(
ArchivePermission, ArchivePermission.id.is_in(query)))
if len(permissions) > 0:
# Found permissions in the database, does the 'explicit' flag
# have the requested value?
conflicting = [permission for permission in permissions
if permission.explicit != explicit]
if len(conflicting) > 0:
# At least one permission with conflicting 'explicit' flag
# value exists already.
cperm = conflicting[0]
raise ValueError(
"Permission for package set '%s' already exists for %s "
"but with a different 'explicit' flag value (%s)." %
(packageset.name, cperm.person.name, cperm.explicit))
else:
# No conflicts, does the requested permission exist already?
existing = [permission for permission in permissions
if (permission.explicit == explicit and
permission.person == person and
permission.packageset == packageset)]
assert len(existing) <= 1, (
"Too many permissions for %s and %s" %
(person.name, packageset.name))
if len(existing) == 1:
# The existing permission matches, just return it.
return existing[0]
# The requested permission does not exist yet. Insert it into the
# database.
permission = ArchivePermission(
archive=archive,
person=person, packageset=packageset,
permission=ArchivePermissionType.UPLOAD, explicit=explicit)
store.add(permission)
return permission
def deletePackagesetUploader(
self, archive, person, packageset, explicit=False):
"""See `IArchivePermissionSet`."""
packageset = self._nameToPackageset(packageset)
store = IMasterStore(ArchivePermission)
# Do we have the permission the user wants removed in the database?
permission = store.find(
ArchivePermission, archive=archive, person=person,
packageset=packageset, permission=ArchivePermissionType.UPLOAD,
explicit=explicit).one()
self._remove_permission(permission)
def packagesetsForSourceUploader(
self, archive, sourcepackagename, person):
"""See `IArchivePermissionSet`."""
sourcepackagename = self._nameToSourcePackageName(sourcepackagename)
store = IStore(ArchivePermission)
query = '''
SELECT ap.id
FROM
archivepermission ap, teamparticipation tp,
packagesetsources pss, flatpackagesetinclusion fpsi
WHERE
ap.person = tp.team AND tp.person = ?
AND ap.packageset = fpsi.parent
AND pss.packageset = fpsi.child
AND pss.sourcepackagename = ?
AND ap.archive = ?
'''
query = SQL(
query, (person.id, sourcepackagename.id, archive.id))
return store.find(
ArchivePermission, ArchivePermission.id.is_in(query))
def packagesetsForSource(
self, archive, sourcepackagename, direct_permissions=True):
"""See `IArchivePermissionSet`."""
sourcepackagename = self._nameToSourcePackageName(sourcepackagename)
store = IStore(ArchivePermission)
if direct_permissions:
origin = SQL('ArchivePermission, PackagesetSources')
rset = store.using(origin).find(ArchivePermission, SQL('''
ArchivePermission.packageset = PackagesetSources.packageset
AND PackagesetSources.sourcepackagename = ?
AND ArchivePermission.archive = ?
''', (sourcepackagename.id, archive.id)))
else:
origin = SQL(
'ArchivePermission, PackagesetSources, '
'FlatPackagesetInclusion')
rset = store.using(origin).find(ArchivePermission, SQL('''
ArchivePermission.packageset = FlatPackagesetInclusion.parent
AND PackagesetSources.packageset =
FlatPackagesetInclusion.child
AND PackagesetSources.sourcepackagename = ?
AND ArchivePermission.archive = ?
''', (sourcepackagename.id, archive.id)))
return rset
def isSourceUploadAllowed(
self, archive, sourcepackagename, person, distroseries=None):
"""See `IArchivePermissionSet`."""
sourcepackagename = self._nameToSourcePackageName(sourcepackagename)
store = IStore(ArchivePermission)
if distroseries is None:
ubuntu = getUtility(IDistributionSet).getByName('ubuntu')
distroseries = ubuntu.currentseries
# Put together the parameters for the query that follows.
archive_params = (ArchivePermissionType.UPLOAD, archive.id)
permission_params = (sourcepackagename.id, person.id, distroseries.id)
query_params = (
# Query parameters for the first WHERE clause.
(archive.id, distroseries.id, sourcepackagename.id) +
# Query parameters for the second WHERE clause.
permission_params + archive_params +
# Query parameters for the third WHERE clause.
permission_params + archive_params)
query = '''
SELECT CASE
WHEN (
SELECT COUNT(ap.id)
FROM packagesetsources pss, archivepermission ap, packageset ps
WHERE
ap.archive = %s AND ap.explicit = TRUE
AND ap.packageset = ps.id AND ps.distroseries = %s
AND pss.sourcepackagename = %s
AND pss.packageset = ap.packageset) > 0
THEN (
SELECT COUNT(ap.id)
FROM
packagesetsources pss, archivepermission ap, packageset ps,
teamparticipation tp
WHERE
pss.sourcepackagename = %s
AND ap.person = tp.team AND tp.person = %s
AND ap.packageset = ps.id AND ps.distroseries = %s
AND pss.packageset = ap.packageset AND ap.explicit = TRUE
AND ap.permission = %s AND ap.archive = %s)
ELSE (
SELECT COUNT(ap.id)
FROM
packagesetsources pss, archivepermission ap, packageset ps,
teamparticipation tp, flatpackagesetinclusion fpsi
WHERE
pss.sourcepackagename = %s
AND ap.person = tp.team AND tp.person = %s
AND ap.packageset = ps.id AND ps.distroseries = %s
AND pss.packageset = fpsi.child AND fpsi.parent = ap.packageset
AND ap.permission = %s AND ap.archive = %s)
END AS number_of_permitted_package_sets;
''' % sqlvalues(*query_params)
return store.execute(query).get_one()[0] > 0
|