1818from rest_framework .request import Request
1919from rest_framework .response import Response
2020
21+ from sentry import options
2122from sentry .api .serializers import serialize
2223from sentry .api .utils import generate_organization_url , is_member_disabled_from_limit
2324from sentry .auth import access
2425from sentry .auth .superuser import is_active_superuser
25- from sentry .models import Organization , Project , ProjectStatus , Team , TeamStatus
26+ from sentry .models import Organization , OrganizationStatus , Project , ProjectStatus , Team , TeamStatus
2627from sentry .models .avatars .base import AvatarBase
2728from sentry .models .user import User
2829from sentry .services .hybrid_cloud .organization import (
@@ -215,28 +216,28 @@ def redirect_to_org(self: _HasRespond, request: Request) -> HttpResponse:
215216 elif not features .has ("organizations:create" ):
216217 return self .respond ("sentry/no-organization-access.html" , status = 403 )
217218 else :
218- org_exists = False
219- url = "/organizations/new/"
219+ url = reverse ("sentry-organization-create" )
220220 if using_customer_domain :
221221 url = absolute_uri (url )
222222
223223 if using_customer_domain and request .user and request .user .is_authenticated :
224- organizations = organization_service .get_organizations (
225- user_id = request .user .id , scope = None , only_visible = True
226- )
227224 requesting_org_slug = request .subdomain
228- org_exists = (
229- organization_service .check_organization_by_slug (
230- slug = requesting_org_slug , only_visible = True
231- )
232- is not None
225+ org_context = organization_service .get_organization_by_slug (
226+ slug = requesting_org_slug , only_visible = False , user_id = request .user .id
233227 )
234- if org_exists and organizations :
235- # If the user is a superuser, redirect them to the org's landing page (e.g. issues page)
236- if request .user .is_superuser :
237- url = Organization .get_url (requesting_org_slug )
228+ if org_context and org_context .organization :
229+ if org_context .organization .status == OrganizationStatus .PENDING_DELETION :
230+ url = reverse ("sentry-customer-domain-restore-organization" )
231+ elif org_context .organization .status == OrganizationStatus .DELETION_IN_PROGRESS :
232+ url_prefix = options .get ("system.url-prefix" )
233+ url = reverse ("sentry-organization-create" )
234+ return HttpResponseRedirect (absolute_uri (url , url_prefix = url_prefix ))
238235 else :
239- url = reverse ("sentry-auth-organization" , args = [requesting_org_slug ])
236+ # If the user is a superuser, redirect them to the org's landing page (e.g. issues page)
237+ if request .user .is_superuser :
238+ url = Organization .get_url (requesting_org_slug )
239+ else :
240+ url = reverse ("sentry-auth-organization" , args = [requesting_org_slug ])
240241 url_prefix = generate_organization_url (requesting_org_slug )
241242 url = absolute_uri (url , url_prefix = url_prefix )
242243
@@ -487,8 +488,8 @@ def is_auth_required(
487488
488489 return False
489490
490- def handle_permission_required (self , request : Request , organization : Organization | RpcOrganization , * args : Any , ** kwargs : Any ) -> HttpResponse : # type: ignore[override]
491- if self .needs_sso (request , organization ):
491+ def handle_permission_required (self , request : Request , organization : Organization | RpcOrganization | None , * args : Any , ** kwargs : Any ) -> HttpResponse : # type: ignore[override]
492+ if organization and self .needs_sso (request , organization ):
492493 logger .info (
493494 "access.must-sso" ,
494495 extra = {"organization_id" : organization .id , "user_id" : request .user .id },
@@ -506,6 +507,21 @@ def handle_permission_required(self, request: Request, organization: Organizatio
506507 redirect_uri = make_login_link_with_redirect (path , after_login_redirect )
507508
508509 else :
510+ if is_using_customer_domain (request ):
511+ # In the customer domain world, if an organziation is pending deletion, we redirect the user to the
512+ # organization restoration page.
513+ org_context = organization_service .get_organization_by_slug (
514+ slug = request .subdomain , only_visible = False , user_id = request .user .id
515+ )
516+ if org_context and org_context .member :
517+ if org_context .organization .status == OrganizationStatus .PENDING_DELETION :
518+ url_base = generate_organization_url (org_context .organization .slug )
519+ restore_org_path = reverse ("sentry-customer-domain-restore-organization" )
520+ return self .redirect (f"{ url_base } { restore_org_path } " )
521+ elif org_context .organization .status == OrganizationStatus .DELETION_IN_PROGRESS :
522+ url_base = options .get ("system.url-prefix" )
523+ create_org_path = reverse ("sentry-organization-create" )
524+ return self .redirect (f"{ url_base } { create_org_path } " )
509525 redirect_uri = self .get_no_permission_url (request , * args , ** kwargs )
510526 return self .redirect (redirect_uri )
511527
0 commit comments