From fc68da513d864e3ec6d6c55350932196e8cc3624 Mon Sep 17 00:00:00 2001 From: shaohuzhang1 Date: Fri, 7 Aug 2026 17:53:02 +0800 Subject: [PATCH] feat: Dialogue profile using dialogue authenticator --- apps/chat/urls.py | 2 +- apps/chat/views/chat.py | 11 ++++++----- apps/common/auth/authentication.py | 2 +- .../auth/constants/chat_permission_constants.py | 12 +++++++++++- apps/common/auth/constants/permission_constants.py | 2 +- apps/common/auth/handle/impl/chat_user_token.py | 8 ++++---- apps/common/auth/struct/permission.py | 4 ++-- 7 files changed, 26 insertions(+), 15 deletions(-) diff --git a/apps/chat/urls.py b/apps/chat/urls.py index 225bae1e1c4..b78d07d760b 100644 --- a/apps/chat/urls.py +++ b/apps/chat/urls.py @@ -12,7 +12,7 @@ path('mcp', mcp_view), path('auth/anonymous', views.AnonymousAuthentication.as_view()), path('profile', views.AuthProfile.as_view()), - path('application/profile', views.ApplicationProfile.as_view(), name='profile'), + path('application//profile', views.ApplicationProfile.as_view(), name='profile'), path('chat_message/', views.ChatView.as_view(), name='chat'), path('chat_message//cancel', views.CancelWorkflowView.as_view(), name='cancel_workflow'), path('open', views.OpenView.as_view(), name='open'), diff --git a/apps/chat/views/chat.py b/apps/chat/views/chat.py index 19e6c22f290..96676e832bd 100644 --- a/apps/chat/views/chat.py +++ b/apps/chat/views/chat.py @@ -28,7 +28,9 @@ from chat.serializers.chat_authentication import AnonymousAuthenticationSerializer, ApplicationProfileSerializer, \ AuthProfileSerializer from common.auth import ChatTokenAuth +from common.auth.authentication import has_permissions from common.auth.common import FileToken +from common.auth.constants.chat_permission_constants import ChatPermissionConstants from common.constants.authentication_type import AuthenticationType from common.constants.cache_version import Cache_Version from common.auth.common import ChatAuthentication @@ -155,11 +157,10 @@ class ApplicationProfile(APIView): responses=None, tags=[_('Chat')] # type: ignore ) - def get(self, request: Request): - if isinstance(request.auth, ChatAuthentication): - return result.success(ApplicationProfileSerializer( - data={'application_id': request.auth.application_id}).profile()) - raise AppAuthenticationFailed(401, "身份异常") + @has_permissions(ChatPermissionConstants.get_aggregate_permissions()) + def get(self, request: Request, application_id: str): + return result.success(ApplicationProfileSerializer( + data={'application_id': application_id}).profile()) class AuthProfile(APIView): diff --git a/apps/common/auth/authentication.py b/apps/common/auth/authentication.py index 9bfc1203233..df31003640c 100644 --- a/apps/common/auth/authentication.py +++ b/apps/common/auth/authentication.py @@ -19,7 +19,7 @@ def _build(items, request, kwargs, compare) -> AggregatePermission: roles, permissions, aggregates = [], [], [] for it in items: if callable(it) and not isinstance(it, AggregatePermission): - it = it(request, kwargs) + it = it(request, **kwargs) if isinstance(it, AggregatePermission): aggregates.append(it) elif isinstance(it, (RoleConstants, Role)): diff --git a/apps/common/auth/constants/chat_permission_constants.py b/apps/common/auth/constants/chat_permission_constants.py index 75245522f61..4a1d3e16ab9 100644 --- a/apps/common/auth/constants/chat_permission_constants.py +++ b/apps/common/auth/constants/chat_permission_constants.py @@ -10,6 +10,7 @@ from common.auth.constants.group_constants import Group from common.auth.constants.operate_constants import Operate +from common.auth.struct.aggregate_permission import AggregatePermission from common.auth.struct.permission import Permission @@ -29,7 +30,7 @@ def get_permission(self): return self._build_workspace_permission('application_id') def _build_workspace_permission(self, resource_id_key=None): - def permission_factory(_, kwargs): + def permission_factory(_, **kwargs): return Permission(group=self.value.group, sub_group=self.value.sub_group, operate=self.value.operate, @@ -38,3 +39,12 @@ def permission_factory(_, kwargs): resource_id=kwargs.get(resource_id_key) if resource_id_key else None) return permission_factory + + @staticmethod + def get_aggregate_permissions(): + return AggregatePermission( + permissions=[_permission.get_permission() for _permission in ChatPermissionConstants]) + + +# 权限字符串与权限对象的Map +CHAT_PERMISSION_STR_MAP = {_permission.value.__str__(): _permission for _permission in ChatPermissionConstants} diff --git a/apps/common/auth/constants/permission_constants.py b/apps/common/auth/constants/permission_constants.py index 0df6c3a7bb0..985c49f38a6 100644 --- a/apps/common/auth/constants/permission_constants.py +++ b/apps/common/auth/constants/permission_constants.py @@ -3603,7 +3603,7 @@ def __init__(self, value, meta): self.meta = meta def _build_workspace_permission(self, resource_id_key=None): - def permission_factory(_, kwargs): + def permission_factory(_, **kwargs): return Permission( group=self.value.group, sub_group=self.value.sub_group, diff --git a/apps/common/auth/handle/impl/chat_user_token.py b/apps/common/auth/handle/impl/chat_user_token.py index f48eef2e442..6726a23023a 100644 --- a/apps/common/auth/handle/impl/chat_user_token.py +++ b/apps/common/auth/handle/impl/chat_user_token.py @@ -11,7 +11,7 @@ from django.db.models import QuerySet, Q from application.models import ApplicationAccessToken, ChatUserType -from common.auth.constants.chat_permission_constants import ChatPermissionConstants +from common.auth.constants.chat_permission_constants import ChatPermissionConstants, CHAT_PERMISSION_STR_MAP from common.auth.constants.group_constants import Group from common.auth.constants.operate_constants import Operate from common.auth.constants.permission_constants import PERMISSION_STR_MAP @@ -66,12 +66,12 @@ def handle(self, request, token: str, get_token_details): login_value = application_access_token.get('login_value') or [] for _value in login_value: permission_str = f'{Group.CHAT_USER}_{_value.upper()}' - permission = PERMISSION_STR_MAP.get(permission_str) + permission = CHAT_PERMISSION_STR_MAP.get(permission_str) if permission: - permission_list.append(permission) + permission_list.append(permission.value) else: permission_list.append(ChatPermissionConstants.CHAT_USER_ANONYMOUS.value) k = f"{Group.CHAT_USER}:r:{application_access_token.application_id}" - permissions[k] = reduce(lambda x, y: x | y, [p.value.bit() for p in permission_list], 0) + permissions[k] = reduce(lambda x, y: x | y, [p.bit() for p in permission_list], 0) return Principal(auth_details.get('user_id'), _type), Auth(set(), permissions) diff --git a/apps/common/auth/struct/permission.py b/apps/common/auth/struct/permission.py index 39a66a43670..e1bb4ce3d99 100644 --- a/apps/common/auth/struct/permission.py +++ b/apps/common/auth/struct/permission.py @@ -33,8 +33,8 @@ def bit(self): return 1 << self.bit_index def get_resource_permission_key(self, resource_id): - workspace = f"w:{self.workspace_id}" if self.workspace_id else "" - resource = f"r:{self.resource_id}" if self.resource_id else "" + workspace = f":w:{self.workspace_id}" if self.workspace_id else "" + resource = f":r:{self.resource_id}" if self.resource_id else "" return f"{self.group}{workspace}{resource}" def __str__(self):