Skip to content

LLM-Driven Extraction of Unstructured Data — Built for API Deployments & ETL Pipeline Workflows

Changes to support enterprise connections (#1300)

Rahul Johnyf25874da4bfa
diff --git a/backend/account_v2/authentication_controller.py b/backend/account_v2/authentication_controller.py
index 1a3c639dc..f5505256f 100644
--- a/backend/account_v2/authentication_controller.py
+++ b/backend/account_v2/authentication_controller.py
@@ -4,6 +4,7 @@ from typing import Any
from django.conf import settings
from django.contrib.auth import logout as django_logout
from django.db.utils import IntegrityError
+from django.http import HttpRequest
from django.middleware import csrf
from django.shortcuts import redirect
from logs_helper.log_service import LogService
@@ -101,7 +102,7 @@ class AuthenticationController:
)
except Exception as ex:
logger.error(f"Error while handling authorization callback: {ex}")
- return redirect(f"{settings.ERROR_URL}")
+ return redirect("/error")
def user_organizations(self, request: Request) -> Any:
"""List a user's organizations.
@@ -127,8 +128,9 @@ class AuthenticationController:
AuthorizationErrorCode.USR,
AuthorizationErrorCode.INE001,
AuthorizationErrorCode.INE002,
+ AuthorizationErrorCode.INS,
}: # type: ignore
- response.data = ({"domain": ex.data.get("domain"), "code": ex.code},)
+ response.data = {"domain": ex.data.get("domain"), "code": ex.code}
return response
# Return in case even if missed unknown exception in
# self.auth_service.user_organizations(request)
@@ -319,6 +321,7 @@ class AuthenticationController:
admin: User,
org_id: str,
user_list: list[dict[str, str | None]],
+ request: HttpRequest,
) -> list[UserInviteResponse]:
"""Invites users to join an organization.
@@ -348,7 +351,7 @@ class AuthenticationController:
# Check if user is already part of current organization
if not user:
status = self.auth_service.invite_user(
- admin_user, org_id, email, role=role
+ admin_user, org_id, email, role=role, request=request
)
message = "User invitation successful."
@@ -362,8 +365,9 @@ class AuthenticationController:
return response
def remove_users_from_organization(
- self, admin: User, organization_id: str, user_emails: list[str]
+ self, request: Request, organization_id: str, user_emails: list[str]
) -> bool:
+ admin: User = request.user
admin_user = OrganizationMemberService.get_user_by_id(id=admin.id)
user_ids = OrganizationMemberService.get_members_by_user_email(
user_emails=user_emails,
@@ -382,6 +386,7 @@ class AuthenticationController:
admin=admin_user,
organization_id=organization_id,
user_ids=user_ids_list,
+ request=request,
)
else:
is_removed = False
@@ -395,13 +400,14 @@ class AuthenticationController:
return is_removed
def add_user_role(
- self, admin: User, org_id: str, email: str, role: str
+ self, request: Request, org_id: str, email: str, role: str
) -> str | None:
+ admin: User = request.user
admin_user = OrganizationMemberService.get_user_by_id(id=admin.id)
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]
+ admin_user, org_id, user.user.user_id, [role], request
)
if current_roles:
self.save_organization_user_role(
@@ -412,13 +418,14 @@ class AuthenticationController:
return None
def remove_user_role(
- self, admin: User, org_id: str, email: str, role: str
+ self, request: Request, org_id: str, email: str, role: str
) -> str | None:
+ admin: User = request.user
admin_user = OrganizationMemberService.get_user_by_id(id=admin.id)
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]
+ admin_user, org_id, organization_member.user.user_id, [role], request
)
if current_roles:
self.save_organization_user_role(
@@ -441,7 +448,14 @@ class AuthenticationController:
) -> OrganizationMember:
existing_tenant_user = OrganizationMemberService.get_user_by_id(id=user.id)
+ # even if existing tenat user updat him with latest role from auth0 org
if existing_tenant_user:
+ user_roles = self.auth_service.get_organization_role_of_user(
+ user_id=existing_tenant_user.user.user_id,
+ organization_id=organization.organization_id,
+ )
+ existing_tenant_user.role = user_roles[0]
+ existing_tenant_user.save()
return existing_tenant_user
account_user = self.get_or_create_user(user=user)
diff --git a/backend/account_v2/authentication_helper.py b/backend/account_v2/authentication_helper.py
index 5d8a2da10..754b97afc 100644
--- a/backend/account_v2/authentication_helper.py
+++ b/backend/account_v2/authentication_helper.py
@@ -26,6 +26,12 @@ class AuthenticationHelper:
return members
+ @staticmethod
+ def create_or_update_user(email: str, user_id: str, provider: str) -> User:
+ user_service = UserService()
+ user = user_service.create_or_update_user(email, user_id, provider)
+ return user
+
@staticmethod
def get_or_create_user_by_email(user_id: str, email: str) -> User:
"""Get or create a user with the given email.
diff --git a/backend/account_v2/authentication_service.py b/backend/account_v2/authentication_service.py
index 1645c548c..eaef0dde7 100644
--- a/backend/account_v2/authentication_service.py
+++ b/backend/account_v2/authentication_service.py
@@ -205,6 +205,7 @@ class AuthenticationService:
admin: OrganizationMember,
organization_id: str,
user_ids: list[str],
+ request: Request,
) -> bool:
raise MethodNotImplemented()
@@ -277,6 +278,7 @@ class AuthenticationService:
organization_id: str,
user_id: str,
role_ids: list[str],
+ request: Request,
) -> list[str]:
if admin.role == UserRole.ADMIN.value:
return role_ids
@@ -288,6 +290,7 @@ class AuthenticationService:
organization_id: str,
user_id: str,
role_ids: list[str],
+ request: Request,
) -> list[str]:
if admin.role == UserRole.ADMIN.value:
return role_ids
@@ -468,5 +471,6 @@ class AuthenticationService:
org_id: str,
email: str,
role: str | None = None,
+ request: HttpRequest | None = None,
) -> bool:
raise MethodNotImplemented()
diff --git a/backend/account_v2/constants.py b/backend/account_v2/constants.py
index 40dda4cb1..fc45c6320 100644
--- a/backend/account_v2/constants.py
+++ b/backend/account_v2/constants.py
@@ -77,6 +77,7 @@ class AuthorizationErrorCode:
INE001: INVALID EMAIL Exception code when an invalid email address is used
like disposable.
INE002: INVALID EMAIL Exception code when an invalid email address format.
+ INS: Insufficient Access
Error code reference :
frontend/src/components/error/GenericError/GenericError.jsx.
@@ -89,3 +90,4 @@ class AuthorizationErrorCode:
USR = "USR"
INE001 = "INE001"
INE002 = "INE002"
+ INS = "INS"
diff --git a/backend/account_v2/migrations/0002_user_auth_provider.py b/backend/account_v2/migrations/0002_user_auth_provider.py
new file mode 100644
index 000000000..b1f082e78
--- /dev/null
+++ b/backend/account_v2/migrations/0002_user_auth_provider.py
@@ -0,0 +1,17 @@
+# Generated by Django 4.2.1 on 2025-05-13 17:45
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("account_v2", "0001_initial"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="user",
+ name="auth_provider",
+ field=models.CharField(default="", max_length=64),
+ ),
+ ]
diff --git a/backend/account_v2/models.py b/backend/account_v2/models.py
index 8aaacd31c..f0ebf71da 100644
--- a/backend/account_v2/models.py
+++ b/backend/account_v2/models.py
@@ -88,6 +88,8 @@ class User(AbstractUser):
blank=True,
)
+ auth_provider = models.CharField(max_length=64, default="")
+
def __str__(self): # type: ignore
return f"User({self.id}, email: {self.email}, userId: {self.user_id})"
diff --git a/backend/account_v2/user.py b/backend/account_v2/user.py
index 1d2537e82..12eca91c7 100644
--- a/backend/account_v2/user.py
+++ b/backend/account_v2/user.py
@@ -14,6 +14,18 @@ class UserService:
) -> None:
pass
+ def create_or_update_user(self, email: str, user_id: str, provider: str) -> Any:
+ try:
+ user, created = User.objects.get_or_create(
+ email=email, user_id=user_id, username=user_id, auth_provider=provider
+ )
+ if created:
+ Logger.debug("User created successfully")
+ return user
+ except IntegrityError as error:
+ Logger.info(f"[Duplicate Id] Failed to create User Error: {error}")
+ raise error
+
def create_user(self, email: str, user_id: str) -> User:
try:
user: User = User(email=email, user_id=user_id, username=email)
@@ -30,7 +42,7 @@ class UserService:
def get_user_by_email(self, email: str) -> User | None:
try:
- user: User = User.objects.get(email=email)
+ user: User = User.objects.get(email=email, auth_provider="")
return user
except User.DoesNotExist:
return None
diff --git a/backend/backend/settings/base.py b/backend/backend/settings/base.py
index c59f246e1..41edec301 100644
--- a/backend/backend/settings/base.py
+++ b/backend/backend/settings/base.py
@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/
import os
from pathlib import Path
+from urllib.parse import urlparse
from dotenv import find_dotenv, load_dotenv
from utils.common_utils import CommonUtils
@@ -55,12 +56,10 @@ WORKFLOW_ACTION_EXPIRATION_TIME_IN_SECOND = os.environ.get(
"WORKFLOW_ACTION_EXPIRATION_TIME_IN_SECOND", 10800
)
WEB_APP_ORIGIN_URL = os.environ.get("WEB_APP_ORIGIN_URL", "http://localhost:3000")
+parsed_url = urlparse(WEB_APP_ORIGIN_URL)
+WEB_APP_ORIGIN_URL_WITH_WILD_CARD = f"{parsed_url.scheme}://*.{parsed_url.netloc}"
CORS_ALLOWED_ORIGINS = [WEB_APP_ORIGIN_URL]
-LOGIN_NEXT_URL = os.environ.get("LOGIN_NEXT_URL", "http://localhost:3000/org")
-LANDING_URL = os.environ.get("LANDING_URL", "http://localhost:3000/landing")
-ERROR_URL = os.environ.get("ERROR_URL", "http://localhost:3000/error")
-
DJANGO_APP_BACKEND_URL = os.environ.get("DJANGO_APP_BACKEND_URL", "http://localhost:8000")
INTERNAL_SERVICE_API_KEY = os.environ.get("INTERNAL_SERVICE_API_KEY")
@@ -155,7 +154,7 @@ ENCRYPTION_KEY = get_required_setting("ENCRYPTION_KEY")
DEBUG = True
ALLOWED_HOSTS = ["*"]
-CSRF_TRUSTED_ORIGINS = [WEB_APP_ORIGIN_URL]
+CSRF_TRUSTED_ORIGINS = [WEB_APP_ORIGIN_URL, WEB_APP_ORIGIN_URL_WITH_WILD_CARD]
CORS_ALLOW_ALL_ORIGINS = False
# Determine if OpenTelemetry trace context should be included in logs
diff --git a/backend/tenant_account_v2/users_view.py b/backend/tenant_account_v2/users_view.py
index 813c98c85..3cfa42bb0 100644
--- a/backend/tenant_account_v2/users_view.py
+++ b/backend/tenant_account_v2/users_view.py
@@ -36,9 +36,7 @@ class OrganizationUserViewSet(viewsets.ViewSet):
auth_controller = AuthenticationController()
auth_controller = AuthenticationController()
- update_status = auth_controller.add_user_role(
- request.user, org_id, user_email, role
- )
+ update_status = auth_controller.add_user_role(request, org_id, user_email, role)
if update_status:
return Response(
status=status.HTTP_200_OK,
@@ -63,7 +61,7 @@ class OrganizationUserViewSet(viewsets.ViewSet):
auth_controller = AuthenticationController()
update_status = auth_controller.remove_user_role(
- request.user, org_id, user_email, role
+ request, org_id, user_email, role
)
if update_status:
return Response(
@@ -120,6 +118,7 @@ class OrganizationUserViewSet(viewsets.ViewSet):
admin=request.user,
org_id=UserSessionUtils.get_organization_id(request),
user_list=user_list,
+ request=request,
)
response_serializer = UserInviteResponseSerializer(invite_response, many=True)
@@ -146,7 +145,7 @@ class OrganizationUserViewSet(viewsets.ViewSet):
auth_controller = AuthenticationController()
is_updated = auth_controller.remove_users_from_organization(
- admin=request.user,
+ request=request,
organization_id=organization_id,
user_emails=user_emails,
)
diff --git a/backend/utils/user_session.py b/backend/utils/user_session.py
index 8021f3b17..c311f2404 100644
--- a/backend/utils/user_session.py
+++ b/backend/utils/user_session.py
@@ -1,3 +1,5 @@
+from typing import Any
+
from django.conf import settings
from django.db import connection
from django.http import HttpRequest
@@ -35,6 +37,10 @@ class UserSessionUtils:
def get_organization_member_role(request: HttpRequest) -> str | None:
return request.session.get("role")
+ @staticmethod
+ def get_token_data(request: HttpRequest) -> Any | None:
+ return request.session.get("token_data")
+
@classmethod
def is_authorized_path(cls, request: HttpRequest) -> bool:
"""Checks if the requested path is authorized based on the organization
diff --git a/frontend/src/components/error/GenericError/GenericError.jsx b/frontend/src/components/error/GenericError/GenericError.jsx
index a355bc3d4..7058afc87 100644
--- a/frontend/src/components/error/GenericError/GenericError.jsx
+++ b/frontend/src/components/error/GenericError/GenericError.jsx
@@ -43,6 +43,10 @@ function GenericError() {
title: `Invalid Email format`,
subtitle: "Please give a valid email address",
},
+ INS: {
+ title: `Access Denied`,
+ subtitle: "Please contact your administrator to request access.",
+ },
// Add more key-value pairs as needed
};