Skip to content

Commit 7211a16

Browse files
committed
Grant project permissions to team members
1 parent 7105aba commit 7211a16

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

tests/unit/packaging/test_models.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818
from pyramid.authorization import Allow
1919
from pyramid.location import lineage
2020

21+
from warehouse.organizations.models import TeamProjectRoleType
2122
from warehouse.packaging.models import File, ProjectFactory, ReleaseURL
2223

2324
from ...common.db.organizations import (
2425
OrganizationFactory as DBOrganizationFactory,
2526
OrganizationProjectFactory as DBOrganizationProjectFactory,
2627
OrganizationRoleFactory as DBOrganizationRoleFactory,
28+
TeamFactory as DBTeamFactory,
29+
TeamProjectRoleFactory as DBTeamProjectRoleFactory,
30+
TeamRoleFactory as DBTeamRoleFactory,
2731
)
2832
from ...common.db.packaging import (
2933
FileFactory as DBFileFactory,
@@ -117,6 +121,12 @@ def test_acl(self, db_session):
117121
owner3 = DBOrganizationRoleFactory.create(organization=organization)
118122
DBOrganizationProjectFactory.create(organization=organization, project=project)
119123

124+
team = DBTeamFactory.create()
125+
owner4 = DBTeamRoleFactory.create(team=team)
126+
DBTeamProjectRoleFactory.create(
127+
team=team, project=project, role_name=TeamProjectRoleType.Administer
128+
)
129+
120130
acls = []
121131
for location in lineage(project):
122132
try:
@@ -137,6 +147,7 @@ def test_acl(self, db_session):
137147
(Allow, f"user:{owner1.user.id}", ["manage:project", "upload"]),
138148
(Allow, f"user:{owner2.user.id}", ["manage:project", "upload"]),
139149
(Allow, f"user:{owner3.user.id}", ["manage:project", "upload"]),
150+
(Allow, f"user:{owner4.user.id}", ["manage:project", "upload"]),
140151
],
141152
key=lambda x: x[1],
142153
) + sorted(

warehouse/packaging/models.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
OrganizationProject,
6161
OrganizationRole,
6262
OrganizationRoleType,
63+
TeamProjectRole,
6364
)
6465
from warehouse.sitemap.models import SitemapMixin
6566
from warehouse.utils import dotted_navigator
@@ -254,7 +255,19 @@ def __acl__(self):
254255
query = session.query(Role).filter(Role.project == self)
255256
query = query.options(orm.lazyload("project"))
256257
query = query.options(orm.lazyload("user"))
257-
roles = {(role.user_id, role.role_name) for role in query.all()}
258+
permissions = {
259+
(role.user_id, "Administer" if role.role_name == "Owner" else "Upload")
260+
for role in query.all()
261+
}
262+
263+
# Add all of the team members for this project.
264+
query = session.query(TeamProjectRole).filter(TeamProjectRole.project == self)
265+
query = query.options(orm.lazyload("project"))
266+
query = query.options(orm.lazyload("team"))
267+
for role in query.all():
268+
permissions |= {
269+
(user.id, role.role_name.value) for user in role.team.members
270+
}
258271

259272
# Add all organization owners for this project.
260273
if self.organization:
@@ -264,12 +277,10 @@ def __acl__(self):
264277
)
265278
query = query.options(orm.lazyload("organization"))
266279
query = query.options(orm.lazyload("user"))
267-
roles |= {(role.user_id, "Owner") for role in query.all()}
280+
permissions |= {(role.user_id, "Administer") for role in query.all()}
268281

269-
for user_id, role_name in sorted(
270-
roles, key=lambda x: (["Owner", "Maintainer"].index(x[1]), x[0])
271-
):
272-
if role_name == "Owner":
282+
for user_id, permission_name in sorted(permissions, key=lambda x: (x[1], x[0])):
283+
if permission_name == "Administer":
273284
acls.append((Allow, f"user:{user_id}", ["manage:project", "upload"]))
274285
else:
275286
acls.append((Allow, f"user:{user_id}", ["upload"]))

0 commit comments

Comments
 (0)