Skip to content

Commit 117eafc

Browse files
authored
Move organizaton-specific logic out of OIDC view (#19027)
* Move organizaton-specific logic out of OIDC view * Move import
1 parent bb409df commit 117eafc

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

warehouse/oidc/views.py

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
OIDC_ISSUER_SERVICE_NAMES,
3030
lookup_custom_issuer_type,
3131
)
32-
from warehouse.organizations.models import OrganizationProject
3332
from warehouse.packaging.interfaces import IProjectService
3433
from warehouse.packaging.models import ProjectFactory
3534
from warehouse.rate_limiting.interfaces import IRateLimiter
@@ -218,32 +217,14 @@ def mint_token(
218217
# Try creating the new project
219218
project_service = request.find_service(IProjectService)
220219
try:
221-
# Check if this pending publisher is for an organization
222-
if pending_publisher.organization_id:
223-
# For organization-owned projects,
224-
# create without making the user an owner
225-
new_project = project_service.create_project(
226-
pending_publisher.project_name,
227-
pending_publisher.added_by,
228-
request,
229-
creator_is_owner=False,
230-
ratelimited=False,
231-
)
232-
# Add the project to the organization
233-
request.db.add(
234-
OrganizationProject(
235-
organization_id=pending_publisher.organization_id,
236-
project_id=new_project.id,
237-
)
238-
)
239-
else:
240-
# For user-owned projects, create normally
241-
new_project = project_service.create_project(
242-
pending_publisher.project_name,
243-
pending_publisher.added_by,
244-
request,
245-
ratelimited=False,
246-
)
220+
new_project = project_service.create_project(
221+
pending_publisher.project_name,
222+
pending_publisher.added_by,
223+
request,
224+
creator_is_owner=pending_publisher.organization_id is None,
225+
ratelimited=False,
226+
organization_id=pending_publisher.organization_id,
227+
)
247228
except HTTPException as exc:
248229
return _invalid(
249230
errors=[{"code": "invalid-payload", "description": str(exc)}],

warehouse/packaging/interfaces.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from warehouse.rate_limiting.interfaces import RateLimiterException
1010

1111
if typing.TYPE_CHECKING:
12+
from uuid import UUID
13+
1214
from warehouse.packaging.models import Project
1315

1416

@@ -76,7 +78,14 @@ def check_project_name(name):
7678
Check if a project name is valid and available for use.
7779
"""
7880

79-
def create_project(name, creator, request, *, creator_is_owner=True):
81+
def create_project(
82+
name,
83+
creator,
84+
request,
85+
*,
86+
creator_is_owner=True,
87+
organization_id: UUID | None = None,
88+
):
8089
"""
8190
Creates a new project, recording a user as its creator.
8291

warehouse/packaging/services.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
from warehouse.helpdesk.interfaces import IAdminNotificationService
3131
from warehouse.metrics import IMetricsService
3232
from warehouse.oidc.models import PendingOIDCPublisher
33+
from warehouse.organizations.models import OrganizationProject
3334
from warehouse.packaging.interfaces import (
3435
IDocsStorage,
3536
IFileStorage,
@@ -494,7 +495,14 @@ def check_project_name(self, name: str) -> None:
494495
return None
495496

496497
def create_project(
497-
self, name, creator, request, *, creator_is_owner=True, ratelimited=True
498+
self,
499+
name,
500+
creator,
501+
request,
502+
*,
503+
creator_is_owner=True,
504+
ratelimited=True,
505+
organization_id=None,
498506
):
499507
if ratelimited:
500508
self._check_ratelimits(request, creator)
@@ -640,6 +648,7 @@ def create_project(
640648
# The project name is valid: create it and add it
641649
project = Project(name=name)
642650
self.db.add(project)
651+
self.db.flush() # To get the new ID
643652

644653
# TODO: This should be handled by some sort of database trigger or a
645654
# SQLAlchemy hook or the like instead of doing it inline in this
@@ -657,8 +666,15 @@ def create_project(
657666
additional={"created_by": creator.username},
658667
)
659668

660-
# Mark the creator as the newly created project's owner, if configured.
661-
if creator_is_owner:
669+
if organization_id:
670+
# If an organization ID is provided, we never set the creator to owner
671+
self.db.add(
672+
OrganizationProject(
673+
organization_id=organization_id, project_id=project.id
674+
)
675+
)
676+
elif creator_is_owner:
677+
# Mark the creator as the newly created project's owner, if configured.
662678
self.db.add(Role(user=creator, project=project, role_name="Owner"))
663679
# TODO: This should be handled by some sort of database trigger or a
664680
# SQLAlchemy hook or the like instead of doing it inline in this

0 commit comments

Comments
 (0)