LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows
Python ai-agentsdata-engineeringdocument-aigenerative-aiidpjson-extractionllmmcp-serverocrpdf-extractionprompt-engineeringstructured-output
handling removed user's session of organization (#285)
ali537d5f117309
diff --git a/backend/account/authentication_controller.py b/backend/account/authentication_controller.pyindex 1dc76985b..9160f8d13 100644--- a/backend/account/authentication_controller.py+++ b/backend/account/authentication_controller.py@@ -54,7 +54,6 @@ class AuthenticationController:"""This method initializes the controller by selecting the appropriateauthentication plugin based on availability."""self.authentication_helper = AuthenticationHelper()- self.organization_member_service = OrganizationMemberService()if AuthenticationPluginRegistry.is_plugin_available():self.auth_service: AuthenticationService = (AuthenticationPluginRegistry.get_plugin()@@ -122,6 +121,7 @@ class AuthenticationController:return responseuser: User = request.userorg_ids = {org.id for org in organizations}+CacheService.set_user_organizations(user.user_id, list(org_ids))serialized_organizations = GetOrganizationsResponseSerializer(@@ -188,7 +188,15 @@ class AuthenticationController:f"{Common.LOG_EVENTS_ID}": StateStore.get(Common.LOG_EVENTS_ID),},)+ current_organization_id = UserSessionUtils.get_organization_id(request)+ if current_organization_id:+ OrganizationMemberService.remove_user_membership_in_organization_cache(+ user_id=user.user_id, organization_id=current_organization_id+ )UserSessionUtils.set_organization_id(request, organization_id)+ OrganizationMemberService.set_user_membership_in_organization_cache(+ user_id=user.user_id, organization_id=organization_id+ )return responsereturn Response(status=status.HTTP_403_FORBIDDEN)@@ -230,6 +238,12 @@ class AuthenticationController:def user_logout(self, request: Request) -> Response:response = self.auth_service.user_logout(request=request)+ organization_id = UserSessionUtils.get_organization_id(request)+ user_id = UserSessionUtils.get_user_id(request)+ if organization_id:+ OrganizationMemberService.remove_user_membership_in_organization_cache(+ user_id=user_id, organization_id=organization_id+ )django_logout(request)return response@@ -284,7 +298,7 @@ class AuthenticationController:email = user_item.get("email")role = user_item.get("role")if email:- user = self.organization_member_service.get_user_by_email(email=email)+ user = OrganizationMemberService.get_user_by_email(email=email)user_response = {}user_response["email"] = emailstatus = False@@ -313,10 +327,10 @@ class AuthenticationController:user__email__in=user_emails).values_list(OrganizationMemberModel.USER_ID, OrganizationMemberModel.ID)user_ids_list: list[str] = []- ids_list: list[str] = []+ pk_list: list[str] = []for user in user_ids:user_ids_list.append(user[0])- ids_list.append(user[1])+ pk_list.append(user[1])if len(user_ids_list) > 0:is_removed = self.auth_service.remove_users_from_organization(admin=admin_user,@@ -326,19 +340,19 @@ class AuthenticationController:else:is_removed = Falseif is_removed:- OrganizationMember.objects.filter(user__in=ids_list).delete()- # removing user m2m relations , while removing user- for user_id in ids_list:- User.objects.get(pk=user_id).shared_exported_tools.clear()- User.objects.get(pk=user_id).shared_custom_tool.clear()- User.objects.get(pk=user_id).shared_adapters.clear()+ AuthenticationHelper.remove_users_from_organization_by_pks(pk_list)+ for user_id in user_ids_list:+ OrganizationMemberService.remove_user_membership_in_organization_cache(+ user_id, organization_id+ )+return is_removeddef add_user_role(self, admin: User, org_id: str, email: str, role: str) -> Optional[str]:admin_user = OrganizationMember.objects.get(user=admin.id)- user = self.organization_member_service.get_user_by_email(email=email)+ user = OrganizationMemberService.get_user_by_email(email=email)if user:current_roles = self.auth_service.add_organization_user_role(admin_user, org_id, user.user.user_id, [role]@@ -355,9 +369,7 @@ class AuthenticationController:self, admin: User, org_id: str, email: str, role: str) -> Optional[str]:admin_user = OrganizationMember.objects.get(user=admin.id)- organization_member = self.organization_member_service.get_user_by_email(- email=email- )+ organization_member = OrganizationMemberService.get_user_by_email(email=email)if organization_member:current_roles = self.auth_service.remove_organization_user_role(admin_user, org_id, organization_member.user.user_id, [role]@@ -372,7 +384,7 @@ class AuthenticationController:return Nonedef save_orgnanization_user_role(self, user_id: str, role: str) -> None:- organization_user = self.organization_member_service.get_user_by_user_id(+ organization_user = OrganizationMemberService.get_user_by_user_id(user_id=user_id)if organization_user:diff --git a/backend/account/authentication_helper.py b/backend/account/authentication_helper.pyindex 5833c875b..fcf69150e 100644--- a/backend/account/authentication_helper.py+++ b/backend/account/authentication_helper.py@@ -5,6 +5,7 @@ from account.dto import MemberDatafrom account.models import Organization, Userfrom account.user import UserServicefrom platform_settings.platform_auth_service import PlatformAuthenticationService+from tenant_account.organization_member_service import OrganizationMemberServicelogger = logging.getLogger(__name__)@@ -79,3 +80,40 @@ class AuthenticationHelper:"Failed to create default platform key for "f"organization {organization.organization_id}")++ @staticmethod+ def remove_users_from_organization_by_pks(+ user_pks: list[str],+ ) -> None:+ """Remove users from an organization by their primary keys.++ Parameters:+ user_pks (list[str]): The primary keys of the users to remove.+ """+ # removing user from organization+ OrganizationMemberService.remove_users_by_user_pks(user_pks)+ # removing user m2m relations , while removing user+ for user_pk in user_pks:+ User.objects.get(pk=user_pk).shared_exported_tools.clear()+ User.objects.get(pk=user_pk).shared_custom_tool.clear()+ User.objects.get(pk=user_pk).shared_adapters.clear()++ @staticmethod+ def remove_user_from_organization_by_user_id(+ user_id: str, organization_id: str+ ) -> None:+ """Remove users from an organization by their user_id.++ Parameters:+ user_id (str): The user_id of the users to remove.+ """+ # removing user from organization+ OrganizationMemberService.remove_user_by_user_id(user_id)+ # removing user m2m relations , while removing user+ User.objects.get(user_id=user_id).shared_exported_tools.clear()+ User.objects.get(user_id=user_id).shared_custom_tool.clear()+ User.objects.get(user_id=user_id).shared_adapters.clear()+ # removing user from organization cache+ OrganizationMemberService.remove_user_membership_in_organization_cache(+ user_id=user_id, organization_id=organization_id+ )diff --git a/backend/tenant_account/organization_member_service.py b/backend/tenant_account/organization_member_service.pyindex 2d58f9c2e..354fdeaeb 100644--- a/backend/tenant_account/organization_member_service.py+++ b/backend/tenant_account/organization_member_service.py@@ -1,19 +1,20 @@from typing import Optionalfrom tenant_account.models import OrganizationMember+from utils.cache_service import CacheServiceclass OrganizationMemberService:- def __init__(self) -> None:- pass- def get_user_by_email(self, email: str) -> Optional[OrganizationMember]:+ @staticmethod+ def get_user_by_email(email: str) -> Optional[OrganizationMember]:try:return OrganizationMember.objects.get(user__email=email) # type: ignoreexcept OrganizationMember.DoesNotExist:return None- def get_user_by_user_id(self, user_id: str) -> Optional[OrganizationMember]:+ @staticmethod+ def get_user_by_user_id(user_id: str) -> Optional[OrganizationMember]:try:return OrganizationMember.objects.get(user__user_id=user_id) # type: ignoreexcept OrganizationMember.DoesNotExist:@@ -25,3 +26,92 @@ class OrganizationMemberService:return OrganizationMember.objects.get(user=id) # type: ignoreexcept OrganizationMember.DoesNotExist:return None++ @staticmethod+ def delete_user(user: OrganizationMember) -> None:+ """Delete a user from an organization.++ Parameters:+ user (OrganizationMember): The user to delete.+ """+ user.delete()++ @staticmethod+ def remove_users_by_user_pks(user_pks: list[str]) -> None:+ """Remove a users from an organization.++ Parameters:+ user_pks (list[str]): The primary keys of the users to remove.+ """+ OrganizationMember.objects.filter(user__in=user_pks).delete()++ @classmethod+ def remove_user_by_user_id(cls, user_id: str) -> None:+ """Remove a user from an organization.++ Parameters:+ user_id (str): The user_id of the user to remove.+ """+ user = cls.get_user_by_user_id(user_id)+ if user:+ cls.delete_user(user)++ @staticmethod+ def get_organization_user_cache_key(user_id: str, organization_id: str) -> str:+ """Get the cache key for a user in an organization.++ Parameters:+ organization_id (str): The ID of the organization.++ Returns:+ str: The cache key for a user in the organization.+ """+ return f"user_organization:{user_id}:{organization_id}"++ @classmethod+ def check_user_membership_in_organization_cache(+ cls, user_id: str, organization_id: str+ ) -> bool:+ """Check if a user exists in an organization.++ Parameters:+ user_id (str): The ID of the user to check.+ organization_id (str): The ID of the organization to check.++ Returns:+ bool: True if the user exists in the organization, False otherwise.+ """+ user_organization_key = cls.get_organization_user_cache_key(+ user_id, organization_id+ )+ return CacheService.check_a_key_exist(user_organization_key)++ @classmethod+ def set_user_membership_in_organization_cache(+ cls, user_id: str, organization_id: str+ ) -> None:+ """Set a user's membership in an organization in the cache.++ Parameters:+ user_id (str): The ID of the user.+ organization_id (str): The ID of the organization.+ """+ user_organization_key = cls.get_organization_user_cache_key(+ user_id, organization_id+ )+ CacheService.set_key(user_organization_key, {})++ @classmethod+ def remove_user_membership_in_organization_cache(+ cls, user_id: str, organization_id: str+ ) -> None:+ """Remove a user's membership in an organization from the cache.++ Parameters:+ user_id (str): The ID of the user.+ organization_id (str): The ID of the organization.+ """+ user_organization_key = cls.get_organization_user_cache_key(+ user_id, organization_id+ )+ CacheService.delete_a_key(user_organization_key)diff --git a/backend/utils/user_session.py b/backend/utils/user_session.pyindex 542a06100..7df8c2995 100644--- a/backend/utils/user_session.py+++ b/backend/utils/user_session.py@@ -11,3 +11,7 @@ class UserSessionUtils:@staticmethoddef set_organization_id(request: HttpRequest, organization_id: str) -> None:request.session["organization"] = organization_id++ @staticmethod+ def get_user_id(request: HttpRequest) -> Optional[str]:+ return request.session.get("user_id")