Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

decorator: Add decorator to provide remote_server to endpoint.

Aman Agrawalba11d0fe5d62
diff --git a/corporate/lib/decorator.py b/corporate/lib/decorator.py
index 41b1fb94fb3..b4e3e2ce134 100644
--- a/corporate/lib/decorator.py
+++ b/corporate/lib/decorator.py
@@ -6,9 +6,12 @@ from django.http import HttpRequest, HttpResponse
from django.shortcuts import render
from typing_extensions import Concatenate, ParamSpec
-from corporate.lib.remote_billing_util import get_remote_realm_from_session
+from corporate.lib.remote_billing_util import (
+ get_remote_realm_from_session,
+ get_remote_server_from_session,
+)
from zerver.lib.subdomains import get_subdomain
-from zilencer.models import RemoteRealm
+from zilencer.models import RemoteRealm, RemoteZulipServer
ParamT = ParamSpec("ParamT")
@@ -57,3 +60,26 @@ def authenticated_remote_realm_management_endpoint(
return view_func(request, remote_realm, *args, **kwargs)
return _wrapped_view_func
+
+
+def authenticated_remote_server_management_endpoint(
+ view_func: Callable[Concatenate[HttpRequest, RemoteZulipServer, ParamT], HttpResponse]
+) -> Callable[Concatenate[HttpRequest, ParamT], HttpResponse]: # nocoverage
+ @wraps(view_func)
+ def _wrapped_view_func(
+ request: HttpRequest,
+ /,
+ *args: ParamT.args,
+ **kwargs: ParamT.kwargs,
+ ) -> HttpResponse:
+ if not is_self_hosting_management_subdomain(request):
+ return render(request, "404.html", status=404)
+
+ server_uuid = kwargs.get("server_uuid")
+ if not isinstance(server_uuid, str):
+ raise TypeError("server_uuid must be a string")
+
+ remote_server = get_remote_server_from_session(request, server_uuid=server_uuid)
+ return view_func(request, remote_server, *args, **kwargs)
+
+ return _wrapped_view_func
diff --git a/corporate/lib/remote_billing_util.py b/corporate/lib/remote_billing_util.py
index 1b88b47e050..bf55661c594 100644
--- a/corporate/lib/remote_billing_util.py
+++ b/corporate/lib/remote_billing_util.py
@@ -5,7 +5,7 @@ from django.http import HttpRequest
from django.utils.translation import gettext as _
from zerver.lib.exceptions import JsonableError
-from zilencer.models import RemoteRealm
+from zilencer.models import RemoteRealm, RemoteZulipServer
billing_logger = logging.getLogger("corporate.stripe")
@@ -69,3 +69,23 @@ def get_remote_realm_from_session(
raise JsonableError(_("Registration is deactivated"))
return remote_realm
+
+
+def get_remote_server_from_session(
+ request: HttpRequest,
+ server_uuid: str,
+) -> RemoteZulipServer:
+ identity_dict = get_identity_dict_from_session(request, None, server_uuid)
+ if identity_dict is None:
+ raise JsonableError(_("User not authenticated"))
+
+ remote_server_uuid = identity_dict["remote_server_uuid"]
+ try:
+ remote_server = RemoteZulipServer.objects.get(uuid=remote_server_uuid)
+ except RemoteZulipServer.DoesNotExist:
+ raise JsonableError(_("Invalid remote server."))
+
+ if remote_server.deactivated:
+ raise JsonableError(_("Registration is deactivated"))
+
+ return remote_server