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-3542 [FIX] Prevent last org admin from demoting themselves (#2048)
Chandrasekharan M2f996d3848ae
diff --git a/backend/account_v2/authentication_controller.py b/backend/account_v2/authentication_controller.pyindex f24f0e8c7..c421ba5bc 100644--- a/backend/account_v2/authentication_controller.py+++ b/backend/account_v2/authentication_controller.py@@ -41,6 +41,7 @@ from account_v2.dto import (UserInviteResponse,UserRoleData,)+from account_v2.enums import UserRolefrom account_v2.exceptions import OrganizationNotExistfrom account_v2.models import Organization, Userfrom account_v2.organization import OrganizationService@@ -417,9 +418,25 @@ class AuthenticationController:return is_removed+ def _ensure_not_last_admin_demotion(self, email: str, new_role: str) -> None:+ # Block demoting the only admin so the org never loses all admins.+ # is_admin_by_role() defers to the active auth plugin's role semantics.+ if self.is_admin_by_role(new_role):+ return+ target = OrganizationMemberService.get_user_by_email(email=email)+ if target and self.is_admin_by_role(target.role):+ admin_count = sum(+ 1+ for m in OrganizationMemberService.get_members()+ if self.is_admin_by_role(m.role)+ )+ if admin_count <= 1:+ raise Forbidden("Cannot demote the only admin of the organization")+def add_user_role(self, request: Request, org_id: str, email: str, role: str) -> str | None:+ self._ensure_not_last_admin_demotion(email=email, new_role=role)admin: User = request.useradmin_user = OrganizationMemberService.get_user_by_id(id=admin.id)user = OrganizationMemberService.get_user_by_email(email=email)@@ -438,6 +455,10 @@ class AuthenticationController:def remove_user_role(self, request: Request, org_id: str, email: str, role: str) -> str | None:+ if self.is_admin_by_role(role):+ self._ensure_not_last_admin_demotion(+ email=email, new_role=UserRole.USER.value+ )admin: User = request.useradmin_user = OrganizationMemberService.get_user_by_id(id=admin.id)organization_member = OrganizationMemberService.get_user_by_email(email=email)diff --git a/backend/account_v2/tests.py b/backend/account_v2/tests.pyindex a39b155ac..a32f9472b 100644--- a/backend/account_v2/tests.py+++ b/backend/account_v2/tests.py@@ -1 +1,73 @@-# Create your tests here.+"""Tests for the last-admin demotion guard in AuthenticationController.++An organization must never lose its only admin via a role change. The guard+(``_ensure_not_last_admin_demotion``) is exercised directly and through the+``add_user_role`` / ``remove_user_role`` entrypoints it protects.+"""++import secrets+from types import SimpleNamespace++from django.test import TestCase+from tenant_account_v2.models import OrganizationMember+from utils.user_context import UserContext++from account_v2.authentication_controller import AuthenticationController+from account_v2.custom_exceptions import Forbidden+from account_v2.enums import UserRole+from account_v2.models import Organization, User+++def _make_member(org: Organization, email: str, role: str) -> OrganizationMember:+ user = User.objects.create_user(+ username=email, email=email, password=secrets.token_urlsafe()+ )+ return OrganizationMember.objects.create(organization=org, user=user, role=role)+++class LastAdminGuardTests(TestCase):+ def setUp(self) -> None:+ self.org = Organization.objects.create(+ name="org-a", display_name="Org A", organization_id="org-a"+ )+ UserContext.set_organization_identifier(self.org.organization_id)+ self.controller = AuthenticationController()+ self.admin = _make_member(self.org, "[email protected]", UserRole.ADMIN.value)++ def test_sole_admin_cannot_be_demoted(self) -> None:+ with self.assertRaises(Forbidden):+ self.controller._ensure_not_last_admin_demotion(+ email=self.admin.user.email, new_role=UserRole.USER.value+ )++ def test_demotion_allowed_when_another_admin_remains(self) -> None:+ _make_member(self.org, "[email protected]", UserRole.ADMIN.value)+ self.controller._ensure_not_last_admin_demotion(+ email=self.admin.user.email, new_role=UserRole.USER.value+ )++ def test_non_admin_role_change_is_unaffected(self) -> None:+ member = _make_member(self.org, "[email protected]", UserRole.USER.value)+ self.controller._ensure_not_last_admin_demotion(+ email=member.user.email, new_role=UserRole.USER.value+ )++ def test_add_user_role_blocks_sole_admin_demotion(self) -> None:+ request = SimpleNamespace(user=self.admin.user)+ with self.assertRaises(Forbidden):+ self.controller.add_user_role(+ request,+ self.org.organization_id,+ self.admin.user.email,+ UserRole.USER.value,+ )++ def test_remove_user_role_blocks_sole_admin_demotion(self) -> None:+ request = SimpleNamespace(user=self.admin.user)+ with self.assertRaises(Forbidden):+ self.controller.remove_user_role(+ request,+ self.org.organization_id,+ self.admin.user.email,+ UserRole.ADMIN.value,+ )