refactor: user

This commit is contained in:
wxg0103 2025-05-15 12:07:07 +08:00
parent e24f211674
commit 8fb8c06e0b
6 changed files with 78 additions and 16 deletions

View File

@ -32,7 +32,7 @@ class SystemSetting(APIView):
operation_id=_('Create or update email settings'), # type: ignore
request=EmailSettingAPI.get_request(),
responses=EmailSettingAPI.get_response(),
tags=[_('Email settings')]) # type: ignore
tags=[_('Email Settings')]) # type: ignore
@has_permissions(PermissionConstants.EMAIL_SETTING_EDIT)
def put(self, request: Request):
return result.success(
@ -45,7 +45,7 @@ class SystemSetting(APIView):
operation_id=_('Test email settings'), # type: ignore
request=EmailSettingAPI.get_request(),
responses=DefaultModelResponse.get_response(),
tags=[_('Email settings')] # type: ignore
tags=[_('Email Settings')] # type: ignore
)
@has_permissions(PermissionConstants.EMAIL_SETTING_EDIT)
def post(self, request: Request):
@ -58,7 +58,7 @@ class SystemSetting(APIView):
description=_('Get email settings'),
operation_id=_('Get email settings'), # type: ignore
responses=DefaultModelResponse.get_response(),
tags=[_('Email settings')]) # type: ignore
tags=[_('Email Settings')]) # type: ignore
@has_permissions(PermissionConstants.EMAIL_SETTING_READ)
def get(self, request: Request):
return result.success(

View File

@ -43,6 +43,32 @@ class UserProfileAPI(APIMixin):
)]
class WorkspaceUserAPI(APIMixin):
@staticmethod
def get_parameters():
return [OpenApiParameter(
name="workspace_id",
description=_('Workspace ID'),
type=OpenApiTypes.STR,
location=OpenApiParameter.PATH,
required=True,
)]
@staticmethod
def get_response():
return WorkspaceUserListResponse
class WorkspaceUser(serializers.Serializer):
id = serializers.CharField(required=True, label=_('id'))
username = serializers.CharField(required=True, label=_('Username'))
class WorkspaceUserListResponse(ResultSerializer):
def get_data(self):
return serializers.ListSerializer(child=WorkspaceUser())
class UserPasswordResponse(APIMixin):
@staticmethod

View File

@ -372,3 +372,23 @@ class UserManageSerializer(serializers.Serializer):
user.password = password_encrypt(instance.get('password'))
user.save()
return True
def get_user_list(self, workspace_id):
"""
获取用户列表
:param workspace_id: 工作空间ID
:return: 用户列表
"""
workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping")
if workspace_user_role_mapping_model:
user_ids = (
workspace_user_role_mapping_model.objects
.filter(workspace_id=workspace_id)
.values_list('user_id', flat=True)
.distinct()
)
else:
user_ids = User.objects.values_list('id', flat=True)
users = User.objects.filter(id__in=user_ids).values('id', 'username')
return list(users)

View File

@ -8,6 +8,8 @@ urlpatterns = [
path('user/profile', views.UserProfileView.as_view(), name="user_profile"),
path('user/captcha', views.CaptchaView.as_view(), name='captcha'),
path('user/test', views.TestPermissionsUserView.as_view(), name="test"),
path('workspace/<str:workspace_id>/user_list', views.WorkspaceUserListView.as_view(),
name="test_workspace_id_permission"),
path('workspace/<str:workspace_id>/user/profile', views.TestWorkspacePermissionUserView.as_view(),
name="test_workspace_id_permission"),
path("user_manage", views.UserManage.as_view(), name="user_manage"),

View File

@ -21,7 +21,7 @@ class LoginView(APIView):
description=_("Log in"),
summary=_("Log in"),
operation_id=_("Log in"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
request=LoginAPI.get_request(),
responses=LoginAPI.get_response())
def post(self, request: Request):
@ -33,7 +33,7 @@ class CaptchaView(APIView):
summary=_("Get captcha"),
description=_("Get captcha"),
operation_id=_("Get captcha"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
responses=CaptchaAPI.get_response())
def get(self, request: Request):
return result.success(CaptchaSerializer().generate())

View File

@ -18,7 +18,7 @@ from common.result import result
from maxkb.const import CONFIG
from models_provider.api.model import DefaultModelResponse
from users.api.user import UserProfileAPI, TestWorkspacePermissionUserApi, DeleteUserApi, EditUserApi, \
ChangeUserPasswordApi, UserPageApi, UserListApi, UserPasswordResponse
ChangeUserPasswordApi, UserPageApi, UserListApi, UserPasswordResponse, WorkspaceUserAPI
from users.serializers.user import UserProfileSerializer, UserManageSerializer
default_password = CONFIG.get('default_password', 'MaxKB@123..')
@ -31,7 +31,7 @@ class UserProfileView(APIView):
summary=_("Get current user information"),
description=_("Get current user information"),
operation_id=_("Get current user information"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
responses=UserProfileAPI.get_response())
def get(self, request: Request):
return result.success(UserProfileSerializer().profile(request.user, request.auth))
@ -44,7 +44,7 @@ class TestPermissionsUserView(APIView):
summary=_("Get current user information"),
description=_("Get current user information"),
operation_id="测试",
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
responses=UserProfileAPI.get_response())
@has_permissions(PermissionConstants.USER_EDIT)
def get(self, request: Request):
@ -58,7 +58,7 @@ class TestWorkspacePermissionUserView(APIView):
summary="针对工作空间下权限校验",
description="针对工作空间下权限校验",
operation_id="针对工作空间下权限校验",
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
responses=UserProfileAPI.get_response(),
parameters=TestWorkspacePermissionUserApi.get_parameters())
@has_permissions(PermissionConstants.USER_EDIT.get_workspace_permission())
@ -66,6 +66,20 @@ class TestWorkspacePermissionUserView(APIView):
return result.success(UserProfileSerializer().profile(request.user, request.auth))
class WorkspaceUserListView(APIView):
authentication_classes = [TokenAuth]
@extend_schema(methods=['GET'],
summary=_("Get user list under workspace"),
description=_("Get user list under workspace"),
operation_id=_("Get user list under workspace"), # type: ignore
tags=[_("User Management")], # type: ignore
parameters=WorkspaceUserAPI.get_parameters(),
responses=WorkspaceUserAPI.get_response())
def get(self, request: Request, workspace_id):
return result.success(UserManageSerializer().get_user_list(workspace_id))
class UserManage(APIView):
authentication_classes = [TokenAuth]
@ -73,7 +87,7 @@ class UserManage(APIView):
summary=_("Create user"),
description=_("Create user"),
operation_id=_("Create user"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
request=UserProfileAPI.get_request(),
responses=UserProfileAPI.get_response())
@has_permissions(PermissionConstants.USER_CREATE)
@ -87,7 +101,7 @@ class UserManage(APIView):
summary=_("Get default password"),
description=_("Get default password"),
operation_id=_("Get default password"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
responses=UserPasswordResponse.get_response())
@has_permissions(PermissionConstants.USER_CREATE)
def get(self, request: Request):
@ -100,7 +114,7 @@ class UserManage(APIView):
description=_("Delete user"),
summary=_("Delete user"),
operation_id=_("Delete user"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
parameters=DeleteUserApi.get_parameters(),
responses=DefaultModelResponse.get_response())
@has_permissions(PermissionConstants.USER_DELETE)
@ -111,7 +125,7 @@ class UserManage(APIView):
summary=_("Get user information"),
description=_("Get user information"),
operation_id=_("Get user information"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
request=DeleteUserApi.get_parameters(),
responses=UserProfileAPI.get_response())
@has_permissions(PermissionConstants.USER_READ)
@ -122,7 +136,7 @@ class UserManage(APIView):
summary=_("Update user information"),
description=_("Update user information"),
operation_id=_("Update user information"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
parameters=DeleteUserApi.get_parameters(),
request=EditUserApi.get_request(),
responses=UserProfileAPI.get_response())
@ -138,7 +152,7 @@ class UserManage(APIView):
summary=_("Change password"),
description=_("Change password"),
operation_id=_("Change password"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
parameters=DeleteUserApi.get_parameters(),
request=ChangeUserPasswordApi.get_request(),
responses=DefaultModelResponse.get_response())
@ -153,7 +167,7 @@ class UserManage(APIView):
summary=_("Get user paginated list"),
description=_("Get user paginated list"),
operation_id=_("Get user paginated list"), # type: ignore
tags=[_("User management")], # type: ignore
tags=[_("User Management")], # type: ignore
parameters=UserPageApi.get_parameters(),
responses=UserPageApi.get_response())
@has_permissions(PermissionConstants.USER_READ)