Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/chat/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<str:application_id>/profile', views.ApplicationProfile.as_view(), name='profile'),
path('chat_message/<str:chat_id>', views.ChatView.as_view(), name='chat'),
path('chat_message/<str:chat_id>/cancel', views.CancelWorkflowView.as_view(), name='cancel_workflow'),
path('open', views.OpenView.as_view(), name='open'),
Expand Down
11 changes: 6 additions & 5 deletions apps/chat/views/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion apps/common/auth/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)):
Expand Down
12 changes: 11 additions & 1 deletion apps/common/auth/constants/chat_permission_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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,
Expand All @@ -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}
2 changes: 1 addition & 1 deletion apps/common/auth/constants/permission_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions apps/common/auth/handle/impl/chat_user_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions apps/common/auth/struct/permission.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Loading