Skip to content

Commit 8fb8c06

Browse files
committed
refactor: user
1 parent e24f211 commit 8fb8c06

File tree

6 files changed

+78
-16
lines changed

6 files changed

+78
-16
lines changed

apps/system_manage/views/email_setting.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Email(APIView):
3232
operation_id=_('Create or update email settings'), # type: ignore
3333
request=EmailSettingAPI.get_request(),
3434
responses=EmailSettingAPI.get_response(),
35-
tags=[_('Email settings')]) # type: ignore
35+
tags=[_('Email Settings')]) # type: ignore
3636
@has_permissions(PermissionConstants.EMAIL_SETTING_EDIT)
3737
def put(self, request: Request):
3838
return result.success(
@@ -45,7 +45,7 @@ def put(self, request: Request):
4545
operation_id=_('Test email settings'), # type: ignore
4646
request=EmailSettingAPI.get_request(),
4747
responses=DefaultModelResponse.get_response(),
48-
tags=[_('Email settings')] # type: ignore
48+
tags=[_('Email Settings')] # type: ignore
4949
)
5050
@has_permissions(PermissionConstants.EMAIL_SETTING_EDIT)
5151
def post(self, request: Request):
@@ -58,7 +58,7 @@ def post(self, request: Request):
5858
description=_('Get email settings'),
5959
operation_id=_('Get email settings'), # type: ignore
6060
responses=DefaultModelResponse.get_response(),
61-
tags=[_('Email settings')]) # type: ignore
61+
tags=[_('Email Settings')]) # type: ignore
6262
@has_permissions(PermissionConstants.EMAIL_SETTING_READ)
6363
def get(self, request: Request):
6464
return result.success(

apps/users/api/user.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ def get_parameters():
4343
)]
4444

4545

46+
class WorkspaceUserAPI(APIMixin):
47+
@staticmethod
48+
def get_parameters():
49+
return [OpenApiParameter(
50+
name="workspace_id",
51+
description=_('Workspace ID'),
52+
type=OpenApiTypes.STR,
53+
location=OpenApiParameter.PATH,
54+
required=True,
55+
)]
56+
57+
@staticmethod
58+
def get_response():
59+
return WorkspaceUserListResponse
60+
61+
62+
class WorkspaceUser(serializers.Serializer):
63+
id = serializers.CharField(required=True, label=_('id'))
64+
username = serializers.CharField(required=True, label=_('Username'))
65+
66+
67+
class WorkspaceUserListResponse(ResultSerializer):
68+
def get_data(self):
69+
return serializers.ListSerializer(child=WorkspaceUser())
70+
71+
4672
class UserPasswordResponse(APIMixin):
4773

4874
@staticmethod

apps/users/serializers/user.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,3 +372,23 @@ def re_password(self, instance, with_valid=True):
372372
user.password = password_encrypt(instance.get('password'))
373373
user.save()
374374
return True
375+
376+
def get_user_list(self, workspace_id):
377+
"""
378+
获取用户列表
379+
:param workspace_id: 工作空间ID
380+
:return: 用户列表
381+
"""
382+
workspace_user_role_mapping_model = DatabaseModelManage.get_model("workspace_user_role_mapping")
383+
if workspace_user_role_mapping_model:
384+
user_ids = (
385+
workspace_user_role_mapping_model.objects
386+
.filter(workspace_id=workspace_id)
387+
.values_list('user_id', flat=True)
388+
.distinct()
389+
)
390+
else:
391+
user_ids = User.objects.values_list('id', flat=True)
392+
393+
users = User.objects.filter(id__in=user_ids).values('id', 'username')
394+
return list(users)

apps/users/urls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
path('user/profile', views.UserProfileView.as_view(), name="user_profile"),
99
path('user/captcha', views.CaptchaView.as_view(), name='captcha'),
1010
path('user/test', views.TestPermissionsUserView.as_view(), name="test"),
11+
path('workspace/<str:workspace_id>/user_list', views.WorkspaceUserListView.as_view(),
12+
name="test_workspace_id_permission"),
1113
path('workspace/<str:workspace_id>/user/profile', views.TestWorkspacePermissionUserView.as_view(),
1214
name="test_workspace_id_permission"),
1315
path("user_manage", views.UserManage.as_view(), name="user_manage"),

apps/users/views/login.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class LoginView(APIView):
2121
description=_("Log in"),
2222
summary=_("Log in"),
2323
operation_id=_("Log in"), # type: ignore
24-
tags=[_("User management")], # type: ignore
24+
tags=[_("User Management")], # type: ignore
2525
request=LoginAPI.get_request(),
2626
responses=LoginAPI.get_response())
2727
def post(self, request: Request):
@@ -33,7 +33,7 @@ class CaptchaView(APIView):
3333
summary=_("Get captcha"),
3434
description=_("Get captcha"),
3535
operation_id=_("Get captcha"), # type: ignore
36-
tags=[_("User management")], # type: ignore
36+
tags=[_("User Management")], # type: ignore
3737
responses=CaptchaAPI.get_response())
3838
def get(self, request: Request):
3939
return result.success(CaptchaSerializer().generate())

apps/users/views/user.py

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from maxkb.const import CONFIG
1919
from models_provider.api.model import DefaultModelResponse
2020
from users.api.user import UserProfileAPI, TestWorkspacePermissionUserApi, DeleteUserApi, EditUserApi, \
21-
ChangeUserPasswordApi, UserPageApi, UserListApi, UserPasswordResponse
21+
ChangeUserPasswordApi, UserPageApi, UserListApi, UserPasswordResponse, WorkspaceUserAPI
2222
from users.serializers.user import UserProfileSerializer, UserManageSerializer
2323

2424
default_password = CONFIG.get('default_password', 'MaxKB@123..')
@@ -31,7 +31,7 @@ class UserProfileView(APIView):
3131
summary=_("Get current user information"),
3232
description=_("Get current user information"),
3333
operation_id=_("Get current user information"), # type: ignore
34-
tags=[_("User management")], # type: ignore
34+
tags=[_("User Management")], # type: ignore
3535
responses=UserProfileAPI.get_response())
3636
def get(self, request: Request):
3737
return result.success(UserProfileSerializer().profile(request.user, request.auth))
@@ -44,7 +44,7 @@ class TestPermissionsUserView(APIView):
4444
summary=_("Get current user information"),
4545
description=_("Get current user information"),
4646
operation_id="测试",
47-
tags=[_("User management")], # type: ignore
47+
tags=[_("User Management")], # type: ignore
4848
responses=UserProfileAPI.get_response())
4949
@has_permissions(PermissionConstants.USER_EDIT)
5050
def get(self, request: Request):
@@ -58,22 +58,36 @@ class TestWorkspacePermissionUserView(APIView):
5858
summary="针对工作空间下权限校验",
5959
description="针对工作空间下权限校验",
6060
operation_id="针对工作空间下权限校验",
61-
tags=[_("User management")], # type: ignore
61+
tags=[_("User Management")], # type: ignore
6262
responses=UserProfileAPI.get_response(),
6363
parameters=TestWorkspacePermissionUserApi.get_parameters())
6464
@has_permissions(PermissionConstants.USER_EDIT.get_workspace_permission())
6565
def get(self, request: Request, workspace_id):
6666
return result.success(UserProfileSerializer().profile(request.user, request.auth))
6767

6868

69+
class WorkspaceUserListView(APIView):
70+
authentication_classes = [TokenAuth]
71+
72+
@extend_schema(methods=['GET'],
73+
summary=_("Get user list under workspace"),
74+
description=_("Get user list under workspace"),
75+
operation_id=_("Get user list under workspace"), # type: ignore
76+
tags=[_("User Management")], # type: ignore
77+
parameters=WorkspaceUserAPI.get_parameters(),
78+
responses=WorkspaceUserAPI.get_response())
79+
def get(self, request: Request, workspace_id):
80+
return result.success(UserManageSerializer().get_user_list(workspace_id))
81+
82+
6983
class UserManage(APIView):
7084
authentication_classes = [TokenAuth]
7185

7286
@extend_schema(methods=['POST'],
7387
summary=_("Create user"),
7488
description=_("Create user"),
7589
operation_id=_("Create user"), # type: ignore
76-
tags=[_("User management")], # type: ignore
90+
tags=[_("User Management")], # type: ignore
7791
request=UserProfileAPI.get_request(),
7892
responses=UserProfileAPI.get_response())
7993
@has_permissions(PermissionConstants.USER_CREATE)
@@ -87,7 +101,7 @@ class Password(APIView):
87101
summary=_("Get default password"),
88102
description=_("Get default password"),
89103
operation_id=_("Get default password"), # type: ignore
90-
tags=[_("User management")], # type: ignore
104+
tags=[_("User Management")], # type: ignore
91105
responses=UserPasswordResponse.get_response())
92106
@has_permissions(PermissionConstants.USER_CREATE)
93107
def get(self, request: Request):
@@ -100,7 +114,7 @@ class Operate(APIView):
100114
description=_("Delete user"),
101115
summary=_("Delete user"),
102116
operation_id=_("Delete user"), # type: ignore
103-
tags=[_("User management")], # type: ignore
117+
tags=[_("User Management")], # type: ignore
104118
parameters=DeleteUserApi.get_parameters(),
105119
responses=DefaultModelResponse.get_response())
106120
@has_permissions(PermissionConstants.USER_DELETE)
@@ -111,7 +125,7 @@ def delete(self, request: Request, user_id):
111125
summary=_("Get user information"),
112126
description=_("Get user information"),
113127
operation_id=_("Get user information"), # type: ignore
114-
tags=[_("User management")], # type: ignore
128+
tags=[_("User Management")], # type: ignore
115129
request=DeleteUserApi.get_parameters(),
116130
responses=UserProfileAPI.get_response())
117131
@has_permissions(PermissionConstants.USER_READ)
@@ -122,7 +136,7 @@ def get(self, request: Request, user_id):
122136
summary=_("Update user information"),
123137
description=_("Update user information"),
124138
operation_id=_("Update user information"), # type: ignore
125-
tags=[_("User management")], # type: ignore
139+
tags=[_("User Management")], # type: ignore
126140
parameters=DeleteUserApi.get_parameters(),
127141
request=EditUserApi.get_request(),
128142
responses=UserProfileAPI.get_response())
@@ -138,7 +152,7 @@ class RePassword(APIView):
138152
summary=_("Change password"),
139153
description=_("Change password"),
140154
operation_id=_("Change password"), # type: ignore
141-
tags=[_("User management")], # type: ignore
155+
tags=[_("User Management")], # type: ignore
142156
parameters=DeleteUserApi.get_parameters(),
143157
request=ChangeUserPasswordApi.get_request(),
144158
responses=DefaultModelResponse.get_response())
@@ -153,7 +167,7 @@ class Page(APIView):
153167
summary=_("Get user paginated list"),
154168
description=_("Get user paginated list"),
155169
operation_id=_("Get user paginated list"), # type: ignore
156-
tags=[_("User management")], # type: ignore
170+
tags=[_("User Management")], # type: ignore
157171
parameters=UserPageApi.get_parameters(),
158172
responses=UserPageApi.get_response())
159173
@has_permissions(PermissionConstants.USER_READ)

0 commit comments

Comments
 (0)