diff --git a/CHANGELOG.md b/CHANGELOG.md index bb001bae28..04e76b2548 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Removed duplicate API call, in the UI on plugin initial load, to `GET /api/internal/v1/alert_receive_channels` +- Increased plugin startup speed ([#1200](https://github.com/grafana/oncall/pull/1200)) ## v1.1.18 (2023-01-18) diff --git a/engine/apps/auth_token/auth.py b/engine/apps/auth_token/auth.py index 97a48bfadc..9835b80c47 100644 --- a/engine/apps/auth_token/auth.py +++ b/engine/apps/auth_token/auth.py @@ -96,6 +96,14 @@ def _get_user(request: Request, organization: Organization) -> User: logger.debug(f"Could not get user from grafana request. Context {context}") raise exceptions.AuthenticationFailed("Non-existent or anonymous user.") + @classmethod + def is_user_from_request_present_in_organization(cls, request: Request, organization: Organization) -> User: + try: + cls._get_user(request, organization) + return True + except exceptions.AuthenticationFailed: + return False + class GrafanaIncidentUser(AnonymousUser): @property diff --git a/engine/apps/grafana_plugin/views/sync.py b/engine/apps/grafana_plugin/views/sync.py index 98d3c23e89..2180efc757 100644 --- a/engine/apps/grafana_plugin/views/sync.py +++ b/engine/apps/grafana_plugin/views/sync.py @@ -7,6 +7,7 @@ from rest_framework.response import Response from rest_framework.views import APIView +from apps.auth_token.auth import PluginAuthentication from apps.grafana_plugin.permissions import PluginTokenVerified from apps.grafana_plugin.tasks.sync import plugin_sync_organization_async from apps.user_management.models import Organization @@ -22,26 +23,29 @@ def post(self, request: Request) -> Response: stack_id = self.instance_context["stack_id"] org_id = self.instance_context["org_id"] is_installed = False - + allow_signup = True try: organization = Organization.objects.get(stack_id=stack_id, org_id=org_id) - if organization.api_token_status == Organization.API_TOKEN_STATUS_OK: is_installed = True - organization.api_token_status = Organization.API_TOKEN_STATUS_PENDING - organization.save(update_fields=["api_token_status"]) + user_is_present_in_org = PluginAuthentication.is_user_from_request_present_in_organization( + request, organization + ) + if not user_is_present_in_org: + organization.api_token_status = Organization.API_TOKEN_STATUS_PENDING + organization.save(update_fields=["api_token_status"]) + + if not organization: + DynamicSetting = apps.get_model("base", "DynamicSetting") + allow_signup = DynamicSetting.objects.get_or_create( + name="allow_plugin_organization_signup", defaults={"boolean_value": True} + )[0].boolean_value + plugin_sync_organization_async.apply_async((organization.pk,)) except Organization.DoesNotExist: logger.info(f"Organization for stack {stack_id} org {org_id} was not found") - allow_signup = True - if not organization: - DynamicSetting = apps.get_model("base", "DynamicSetting") - allow_signup = DynamicSetting.objects.get_or_create( - name="allow_plugin_organization_signup", defaults={"boolean_value": True} - )[0].boolean_value - return Response( status=status.HTTP_202_ACCEPTED, data={