Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Block /signup, add ReadOnlyException for api
Maciej Korycinskif5db88889f7f
diff --git a/saleor/core/exceptions.py b/saleor/core/exceptions.pyindex 8497316fdf..2b143602d5 100644--- a/saleor/core/exceptions.py+++ b/saleor/core/exceptions.py@@ -1,3 +1,5 @@+from django.utils.translation import ugettext_lazy as _+from ..checkout.error_codes import CheckoutErrorCode@@ -6,3 +8,10 @@ class InsufficientStock(Exception):super().__init__("Insufficient stock for %r" % (item,))self.item = itemself.code = CheckoutErrorCode.INSUFFICIENT_STOCK+++class ReadOnlyException(Exception):+ def __init__(self, msg=None):+ if msg is None:+ msg = _("API runs in read only mode")+ super().__init__(msg)diff --git a/saleor/core/middleware.py b/saleor/core/middleware.pyindex 2ce9a72f3a..a0780d6657 100644--- a/saleor/core/middleware.py+++ b/saleor/core/middleware.py@@ -10,14 +10,14 @@ from django.template.response import TemplateResponsefrom django.urls import reversefrom django.utils import timezonefrom django.utils.functional import SimpleLazyObject-from django.utils.translation import get_language+from django.utils.translation import get_language, ugettext_lazy as _from django_countries.fields import Country-from graphql_jwt.exceptions import PermissionDeniedfrom ..discount.utils import fetch_discountsfrom ..extensions.manager import get_extensions_managerfrom ..graphql.views import GraphQLViewfrom . import analytics+from .exceptions import ReadOnlyExceptionfrom .utils import get_client_ip, get_country_by_ip, get_currency_for_countrylogger = logging.getLogger(__name__)@@ -121,19 +121,21 @@ class ReadOnlyMiddleware:self.blocked_django_url_patterns = [re.compile(r"^/dashboard"),re.compile(r"^/([\w-]+/)?account/$"),+ re.compile(r"^/([\w-]+/)?account/signup/$"),]+ self.allowed_mutations_startswith = ["checkout", "tokenCreate", "tokenVerify"]def __call__(self, request):- response = self.get_response(request)- return response+ return self.get_response(request)def process_view(self, request, *_args, **_kwargs):if request.path == reverse("api"):if not self._is_graphql_request_blocked(request):return None-error = GraphQLView.format_error(- PermissionDenied("Be aware admin pirate! API runs in read only mode!")+ ReadOnlyException(+ _("Be aware admin pirate! API runs in read only mode!")+ ))return JsonResponse(data=error, status=200, safe=False)@@ -161,7 +163,6 @@ class ReadOnlyMiddleware:return is_post and any(self._is_url_blocked(request_url))def _is_graphql_request_blocked(self, request):- allowed_mutations_startswith = ["checkout", "tokenCreate"]body = GraphQLView.parse_body(request)if not isinstance(body, list):@@ -184,7 +185,7 @@ class ReadOnlyMiddleware:blocked = not any([part_name in selection_name- for part_name in allowed_mutations_startswith+ for part_name in self.allowed_mutations_startswith])if blocked:diff --git a/saleor/graphql/views.py b/saleor/graphql/views.pyindex f462e56531..ae91aec256 100644--- a/saleor/graphql/views.py+++ b/saleor/graphql/views.py@@ -212,6 +212,7 @@ class GraphQLView(View):result["extensions"] = {"exception": {"code": type(exc).__name__, "stacktrace ": lines}}+return resultdiff --git a/templates/read_only/dashboard/read_only_splash.html b/templates/read_only/dashboard/read_only_splash.htmlindex e3cddb7e23..8f5af0061f 100644--- a/templates/read_only/dashboard/read_only_splash.html+++ b/templates/read_only/dashboard/read_only_splash.html@@ -45,7 +45,7 @@<body class="{% block body_class %}body-home{% endblock %}"><div class="read-only"><div class="read-only-description">- <h2>{% blocktrans %}Be aware admin pirate! <br> Dashboard runs in <span>read only mode!</span>{% endblocktrans %}</h2>+ <h2>{% blocktrans %}Be aware admin pirate! <br> Saleor runs in <span>read only mode!</span>{% endblocktrans %}</h2><a href="{{ back_url }}" class="btn">{% trans 'Close' %}</a></div><div class="{{ image_class }}">{% include image_path %}</div>