LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
UN-3585 [FEAT] Restrict connector creation to org admins (controlled mode) (#2145)
Deepak K3a7f920b0e0e
diff --git a/backend/account_v2/migrations/0007_organization_restrict_connector_creation.py b/backend/account_v2/migrations/0007_organization_restrict_connector_creation.pynew file mode 100644index 000000000..faeefc09a--- /dev/null+++ b/backend/account_v2/migrations/0007_organization_restrict_connector_creation.py@@ -0,0 +1,22 @@+from django.db import migrations, models+++class Migration(migrations.Migration):+ dependencies = [+ ("account_v2", "0006_organization_restrict_llm_adapter_creation"),+ ]++ operations = [+ migrations.AddField(+ model_name="organization",+ name="restrict_connector_creation",+ field=models.BooleanField(+ db_comment=(+ "Controlled mode: when True, only organization admins may "+ "create connectors in this org. Default False preserves "+ "open creation."+ ),+ default=False,+ ),+ ),+ ]diff --git a/backend/account_v2/models.py b/backend/account_v2/models.pyindex def009cd2..e532a9a45 100644--- a/backend/account_v2/models.py+++ b/backend/account_v2/models.py@@ -46,6 +46,13 @@ class Organization(models.Model):"LLM adapters in this org. Default False preserves open creation."),)+ restrict_connector_creation = models.BooleanField(+ default=False,+ db_comment=(+ "Controlled mode: when True, only organization admins may create "+ "connectors in this org. Default False preserves open creation."+ ),+ )class Meta:verbose_name = "Organization"diff --git a/backend/connector_v2/views.py b/backend/connector_v2/views.pyindex f6803b047..e81da3c3b 100644--- a/backend/connector_v2/views.py+++ b/backend/connector_v2/views.py@@ -12,10 +12,13 @@ from permissions.permission import IsOwner, IsOwnerOrSharedUserOrSharedToOrgfrom permissions.resource_share_views import ResourceShareManagementMixinfrom plugins import get_pluginfrom rest_framework import status, viewsets+from rest_framework.exceptions import PermissionDeniedfrom rest_framework.request import Requestfrom rest_framework.response import Responsefrom rest_framework.versioning import URLPathVersioning+from tenant_account_v2.organization_member_service import OrganizationMemberServicefrom utils.filtering import FilterHelper+from utils.user_context import UserContextfrom backend.constants import RequestKeyfrom connector_v2.constants import ConnectorInstanceKey as CIKey@@ -44,6 +47,27 @@ class ConnectorInstanceViewSet(ResourceShareManagementMixin, viewsets.ModelViewSreturn [IsOwnerOrSharedUserOrSharedToOrg()]+ @staticmethod+ def _enforce_connector_creation_restriction(request: Any) -> None:+ """Controlled mode (UN-3585): only org admins may create connectors+ when the org has enabled the restriction. Service accounts (platform+ API-key sessions) bypass; the default (flag off) keeps creation open+ for everyone. Applies to all connector types (all connect to external+ systems).+ """+ if getattr(request.user, "is_service_account", False):+ return+ organization = UserContext.get_organization()+ if (+ organization+ and organization.restrict_connector_creation+ and not OrganizationMemberService.is_user_organization_admin(request.user)+ ):+ raise PermissionDenied(+ "Connector creation is restricted to organization admins. "+ "Please contact your organization admin."+ )+def get_queryset(self) -> QuerySet | None:queryset = ConnectorInstance.objects.for_user(self.request.user)@@ -161,11 +185,18 @@ class ConnectorInstanceViewSet(ResourceShareManagementMixin, viewsets.ModelViewSexcept Exception as exc:logger.error(f"Error while obtaining ConnectorAuth: {exc}")raise OAuthTimeOut+ # Explicitly bind the connector to the request-scoped organization.+ # Defense-in-depth: DefaultOrganizationMixin already declares+ # `organization` as editable=False (so DRF drops any client-supplied+ # value) and its save() backfills the org from UserContext when unset.+ # Binding here makes that explicit at the callsite and keeps the row's+ # org consistent with the org the controlled-mode check evaluated.serializer.save(connector_id=connector_id,connector_metadata=connector_metadata,created_by=self.request.user,modified_by=self.request.user,+ organization=UserContext.get_organization(),) # type: ignore# Clean up OAuth cache after successful create@@ -173,6 +204,10 @@ class ConnectorInstanceViewSet(ResourceShareManagementMixin, viewsets.ModelViewSdef create(self, request: Any) -> Response:# Overriding default exception behavior+ # Fail fast on the admin restriction before validating the payload —+ # the check depends only on the request/org, not on validated data, so+ # a denied caller shouldn't get input-validation feedback first.+ self._enforce_connector_creation_restriction(request)serializer = self.get_serializer(data=request.data)serializer.is_valid(raise_exception=True)try:diff --git a/backend/tenant_account_v2/views.py b/backend/tenant_account_v2/views.pyindex 7cbce0f5a..0e824bd7e 100644--- a/backend/tenant_account_v2/views.py+++ b/backend/tenant_account_v2/views.py@@ -55,13 +55,22 @@ def reset_password(request: Request) -> Response:)+# Boolean org-level controlled-mode flags exposed by ``organization_settings``.+# Each maps a request/response key to the ``Organization`` field it updates.+ORGANIZATION_SETTING_FLAGS = (+ "restrict_llm_adapter_creation",+ "restrict_connector_creation",+)++@api_view(["GET", "PATCH"])@permission_classes([IsAuthenticated, IsOrganizationAdmin])def organization_settings(request: Request) -> Response:"""Read or update org-level settings. Admin-only.- Currently exposes the ``restrict_llm_adapter_creation`` controlled-mode- flag. GET returns the current value; PATCH updates it.+ Exposes the controlled-mode flags in ``ORGANIZATION_SETTING_FLAGS``+ (``restrict_llm_adapter_creation``, ``restrict_connector_creation``). GET+ returns the current values; PATCH updates any subset of them."""organization = UserContext.get_organization()if not organization:@@ -71,27 +80,37 @@ def organization_settings(request: Request) -> Response:)if request.method == "PATCH":- value = request.data.get("restrict_llm_adapter_creation")- if not isinstance(value, bool):+ provided = {+ flag: request.data[flag]+ for flag in ORGANIZATION_SETTING_FLAGS+ if flag in request.data+ }+ if not provided:return Response(status=status.HTTP_400_BAD_REQUEST,- data={"message": "restrict_llm_adapter_creation must be a boolean"},+ data={+ "message": (+ "Provide at least one of: "+ f"{', '.join(ORGANIZATION_SETTING_FLAGS)}"+ )+ },)- organization.restrict_llm_adapter_creation = value+ # Validate everything before mutating so a bad value in a later flag+ # doesn't leave the in-memory org partially updated.+ for flag, value in provided.items():+ if not isinstance(value, bool):+ return Response(+ status=status.HTTP_400_BAD_REQUEST,+ data={"message": f"{flag} must be a boolean"},+ )+ for flag, value in provided.items():+ setattr(organization, flag, value)organization.modified_by = request.user- organization.save(- update_fields=[- "restrict_llm_adapter_creation",- "modified_by",- "modified_at",- ]- )+ organization.save(update_fields=[*provided, "modified_by", "modified_at"])return Response(status=status.HTTP_200_OK,- data={- "restrict_llm_adapter_creation": (organization.restrict_llm_adapter_creation)- },+ data={flag: getattr(organization, flag) for flag in ORGANIZATION_SETTING_FLAGS},)diff --git a/frontend/src/components/settings/platform/PlatformSettings.jsx b/frontend/src/components/settings/platform/PlatformSettings.jsxindex e7ee22dda..80893e5da 100644--- a/frontend/src/components/settings/platform/PlatformSettings.jsx+++ b/frontend/src/components/settings/platform/PlatformSettings.jsx@@ -102,6 +102,11 @@ function PlatformSettings() {// Controlled-mode flag: only admins can create LLM adapters when true.const [restrictLlmCreation, setRestrictLlmCreation] = useState(false);const [isSavingRestriction, setIsSavingRestriction] = useState(false);+ // Controlled-mode flag: only admins can create connectors when true.+ const [restrictConnectorCreation, setRestrictConnectorCreation] =+ useState(false);+ const [isSavingConnectorRestriction, setIsSavingConnectorRestriction] =+ useState(false);const { sessionDetails } = useSessionStore();const { setAlertDetails } = useAlertStore();const axiosPrivate = useAxiosPrivate();@@ -155,9 +160,12 @@ function PlatformSettings() {setRestrictLlmCreation(Boolean(res?.data?.restrict_llm_adapter_creation),);+ setRestrictConnectorCreation(+ Boolean(res?.data?.restrict_connector_creation),+ );}).catch((err) => {- console.warn("Failed to load LLM adapter creation setting", err);+ console.warn("Failed to load organization settings", err);});}, [sessionDetails?.orgId, sessionDetails?.isAdmin]);@@ -192,6 +200,37 @@ function PlatformSettings() {});};+ const handleToggleConnectorRestriction = (checked) => {+ const previous = restrictConnectorCreation;+ setRestrictConnectorCreation(checked); // optimistic+ setIsSavingConnectorRestriction(true);+ axiosPrivate({+ method: "PATCH",+ url: `/api/v1/unstract/${sessionDetails?.orgId}/organization/settings`,+ headers: {+ "X-CSRFToken": sessionDetails?.csrfToken,+ "Content-Type": "application/json",+ },+ data: { restrict_connector_creation: checked },+ })+ .then((res) => {+ setRestrictConnectorCreation(+ Boolean(res?.data?.restrict_connector_creation),+ );+ setAlertDetails({+ type: "success",+ content: "Connector creation setting updated.",+ });+ })+ .catch((err) => {+ setRestrictConnectorCreation(previous); // revert on failure+ setAlertDetails(handleException(err, "Failed to update setting"));+ })+ .finally(() => {+ setIsSavingConnectorRestriction(false);+ });+ };+const handleSaveInterval = () => {if (!batchIntervalMinutes ||@@ -582,6 +621,32 @@ function PlatformSettings() {</div></div>)}+ {isEnterpriseBuild && sessionDetails?.isAdmin && (+ <div className="plt-set-section">+ <Typography.Title level={5}>+ Connector Creation+ </Typography.Title>+ <Typography.Text+ type="secondary"+ className="plt-set-section-subtitle"+ >+ Restrict creation of connectors to organization admins. When+ enabled, non-admin users cannot create connectors.+ </Typography.Text>+ <div className="plt-set-inner-card">+ <div className="plt-set-notif-field-row">+ <Switch+ checked={restrictConnectorCreation}+ loading={isSavingConnectorRestriction}+ onChange={handleToggleConnectorRestriction}+ />+ <Typography.Text>+ Only admins can create connectors+ </Typography.Text>+ </div>+ </div>+ </div>+ )}</div></IslandLayout></div>