2025-04-14 12:11:23 +00:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
"""
|
|
|
|
|
|
@project: MaxKB
|
|
|
|
|
|
@Author:虎虎
|
|
|
|
|
|
@file: user.py
|
|
|
|
|
|
@date:2025/4/14 19:25
|
|
|
|
|
|
@desc:
|
|
|
|
|
|
"""
|
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2025-04-17 06:27:58 +00:00
|
|
|
|
from drf_spectacular.utils import extend_schema
|
2025-04-14 12:11:23 +00:00
|
|
|
|
from rest_framework.request import Request
|
2025-04-17 06:27:58 +00:00
|
|
|
|
from rest_framework.views import APIView
|
2025-04-14 12:11:23 +00:00
|
|
|
|
|
2025-04-17 06:27:58 +00:00
|
|
|
|
from common.auth.authenticate import TokenAuth
|
2025-04-15 12:37:38 +00:00
|
|
|
|
from common.auth.authentication import has_permissions
|
2025-04-17 09:16:45 +00:00
|
|
|
|
from common.constants.permission_constants import PermissionConstants, Permission, Group, Operate
|
2025-04-14 12:11:23 +00:00
|
|
|
|
from common.result import result
|
2025-05-13 06:39:47 +00:00
|
|
|
|
from maxkb.const import CONFIG
|
2025-04-28 09:36:56 +00:00
|
|
|
|
from models_provider.api.model import DefaultModelResponse
|
|
|
|
|
|
from users.api.user import UserProfileAPI, TestWorkspacePermissionUserApi, DeleteUserApi, EditUserApi, \
|
2025-05-15 04:07:07 +00:00
|
|
|
|
ChangeUserPasswordApi, UserPageApi, UserListApi, UserPasswordResponse, WorkspaceUserAPI
|
2025-04-27 08:26:40 +00:00
|
|
|
|
from users.serializers.user import UserProfileSerializer, UserManageSerializer
|
2025-04-14 12:11:23 +00:00
|
|
|
|
|
2025-05-13 06:39:47 +00:00
|
|
|
|
default_password = CONFIG.get('default_password', 'MaxKB@123..')
|
|
|
|
|
|
|
2025-04-14 12:11:23 +00:00
|
|
|
|
|
|
|
|
|
|
class UserProfileView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_("Get current user information"),
|
2025-04-14 12:11:23 +00:00
|
|
|
|
description=_("Get current user information"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Get current user information"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-14 12:11:23 +00:00
|
|
|
|
responses=UserProfileAPI.get_response())
|
|
|
|
|
|
def get(self, request: Request):
|
2025-04-27 10:27:35 +00:00
|
|
|
|
return result.success(UserProfileSerializer().profile(request.user, request.auth))
|
2025-04-15 12:37:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestPermissionsUserView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_("Get current user information"),
|
2025-04-15 12:37:38 +00:00
|
|
|
|
description=_("Get current user information"),
|
2025-04-17 06:27:58 +00:00
|
|
|
|
operation_id="测试",
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-15 12:37:38 +00:00
|
|
|
|
responses=UserProfileAPI.get_response())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_EDIT)
|
|
|
|
|
|
def get(self, request: Request):
|
2025-04-27 10:27:35 +00:00
|
|
|
|
return result.success(UserProfileSerializer().profile(request.user, request.auth))
|
2025-04-17 02:35:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TestWorkspacePermissionUserView(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary="针对工作空间下权限校验",
|
2025-04-17 02:35:02 +00:00
|
|
|
|
description="针对工作空间下权限校验",
|
|
|
|
|
|
operation_id="针对工作空间下权限校验",
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-17 02:35:02 +00:00
|
|
|
|
responses=UserProfileAPI.get_response(),
|
|
|
|
|
|
parameters=TestWorkspacePermissionUserApi.get_parameters())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_EDIT.get_workspace_permission())
|
|
|
|
|
|
def get(self, request: Request, workspace_id):
|
2025-04-27 10:27:35 +00:00
|
|
|
|
return result.success(UserProfileSerializer().profile(request.user, request.auth))
|
2025-04-27 08:26:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
2025-05-15 04:07:07 +00:00
|
|
|
|
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))
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-27 08:26:40 +00:00
|
|
|
|
class UserManage(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['POST'],
|
2025-04-28 09:36:56 +00:00
|
|
|
|
summary=_("Create user"),
|
2025-04-27 08:26:40 +00:00
|
|
|
|
description=_("Create user"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Create user"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-27 08:26:40 +00:00
|
|
|
|
request=UserProfileAPI.get_request(),
|
|
|
|
|
|
responses=UserProfileAPI.get_response())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_CREATE)
|
|
|
|
|
|
def post(self, request: Request):
|
|
|
|
|
|
return result.success(UserManageSerializer().save(request.data))
|
2025-04-28 09:36:56 +00:00
|
|
|
|
|
2025-05-13 06:39:47 +00:00
|
|
|
|
class Password(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['Get'],
|
|
|
|
|
|
summary=_("Get default password"),
|
|
|
|
|
|
description=_("Get default password"),
|
|
|
|
|
|
operation_id=_("Get default password"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-05-13 06:39:47 +00:00
|
|
|
|
responses=UserPasswordResponse.get_response())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_CREATE)
|
|
|
|
|
|
def get(self, request: Request):
|
|
|
|
|
|
return result.success(data={'password': default_password})
|
|
|
|
|
|
|
2025-04-28 09:36:56 +00:00
|
|
|
|
class Operate(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['DELETE'],
|
|
|
|
|
|
description=_("Delete user"),
|
|
|
|
|
|
summary=_("Delete user"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Delete user"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-28 09:36:56 +00:00
|
|
|
|
parameters=DeleteUserApi.get_parameters(),
|
|
|
|
|
|
responses=DefaultModelResponse.get_response())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_DELETE)
|
|
|
|
|
|
def delete(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(UserManageSerializer.Operate(data={'id': user_id}).delete(with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
|
|
|
|
|
summary=_("Get user information"),
|
|
|
|
|
|
description=_("Get user information"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Get user information"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-28 09:36:56 +00:00
|
|
|
|
request=DeleteUserApi.get_parameters(),
|
|
|
|
|
|
responses=UserProfileAPI.get_response())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_READ)
|
|
|
|
|
|
def get(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(UserManageSerializer.Operate(data={'id': user_id}).one(with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['PUT'],
|
|
|
|
|
|
summary=_("Update user information"),
|
|
|
|
|
|
description=_("Update user information"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Update user information"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-28 09:36:56 +00:00
|
|
|
|
parameters=DeleteUserApi.get_parameters(),
|
|
|
|
|
|
request=EditUserApi.get_request(),
|
|
|
|
|
|
responses=UserProfileAPI.get_response())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_EDIT)
|
|
|
|
|
|
def put(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
UserManageSerializer.Operate(data={'id': user_id}).edit(request.data, with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
class RePassword(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['PUT'],
|
|
|
|
|
|
summary=_("Change password"),
|
|
|
|
|
|
description=_("Change password"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Change password"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-28 09:36:56 +00:00
|
|
|
|
parameters=DeleteUserApi.get_parameters(),
|
|
|
|
|
|
request=ChangeUserPasswordApi.get_request(),
|
|
|
|
|
|
responses=DefaultModelResponse.get_response())
|
|
|
|
|
|
def put(self, request: Request, user_id):
|
|
|
|
|
|
return result.success(
|
|
|
|
|
|
UserManageSerializer.Operate(data={'id': user_id}).re_password(request.data, with_valid=True))
|
|
|
|
|
|
|
|
|
|
|
|
class Page(APIView):
|
|
|
|
|
|
authentication_classes = [TokenAuth]
|
|
|
|
|
|
|
|
|
|
|
|
@extend_schema(methods=['GET'],
|
|
|
|
|
|
summary=_("Get user paginated list"),
|
|
|
|
|
|
description=_("Get user paginated list"),
|
2025-05-09 03:29:05 +00:00
|
|
|
|
operation_id=_("Get user paginated list"), # type: ignore
|
2025-05-15 04:07:07 +00:00
|
|
|
|
tags=[_("User Management")], # type: ignore
|
2025-04-28 09:36:56 +00:00
|
|
|
|
parameters=UserPageApi.get_parameters(),
|
|
|
|
|
|
responses=UserPageApi.get_response())
|
|
|
|
|
|
@has_permissions(PermissionConstants.USER_READ)
|
|
|
|
|
|
def get(self, request: Request, current_page, page_size):
|
|
|
|
|
|
d = UserManageSerializer.Query(
|
|
|
|
|
|
data={'email_or_username': request.query_params.get('email_or_username', None),
|
|
|
|
|
|
'user_id': str(request.user.id)})
|
|
|
|
|
|
return result.success(d.page(current_page, page_size))
|