From f460916e84eea5351a5aa0a9f9cd06a251210d7a Mon Sep 17 00:00:00 2001 From: xinwen Date: Mon, 21 Feb 2022 18:51:11 +0800 Subject: [PATCH] fix: swagger --- apps/assets/api/domain.py | 1 + apps/common/mixins/api/permission.py | 3 +++ apps/perms/api/application/user_group_permission.py | 5 ++++- apps/perms/api/asset/user_group_permission.py | 9 +++++++-- apps/rbac/api/role.py | 4 ++++ apps/settings/api/ldap.py | 2 +- apps/terminal/api/command.py | 3 +++ apps/tickets/api/relation.py | 2 +- 8 files changed, 24 insertions(+), 5 deletions(-) diff --git a/apps/assets/api/domain.py b/apps/assets/api/domain.py index b37d895f2..b022f4dec 100644 --- a/apps/assets/api/domain.py +++ b/apps/assets/api/domain.py @@ -37,6 +37,7 @@ class GatewayViewSet(OrgBulkModelViewSet): class GatewayTestConnectionApi(SingleObjectMixin, APIView): + queryset = Gateway.objects.all() object = None rbac_perms = { 'POST': 'assets.change_gateway' diff --git a/apps/common/mixins/api/permission.py b/apps/common/mixins/api/permission.py index 6ffa15e53..64efbe7f5 100644 --- a/apps/common/mixins/api/permission.py +++ b/apps/common/mixins/api/permission.py @@ -25,6 +25,9 @@ class RoleAdminMixin: @lazyproperty def user(self): user_id = self.kwargs.get(self.user_id_url_kwarg) + if hasattr(self, 'swagger_fake_view') and not user_id: + return self.request.user # NOQA + user_model = get_user_model() return user_model.objects.get(id=user_id) diff --git a/apps/perms/api/application/user_group_permission.py b/apps/perms/api/application/user_group_permission.py index 0237227ec..e8061e9fc 100644 --- a/apps/perms/api/application/user_group_permission.py +++ b/apps/perms/api/application/user_group_permission.py @@ -26,7 +26,10 @@ class UserGroupGrantedApplicationsApi(CommonApiMixin, ListAPIView): } def get_queryset(self): - user_group_id = self.kwargs.get('pk', '') + user_group_id = self.kwargs.get('pk') + if not user_group_id: + return Application.objects.none() + queryset = Application.objects\ .filter(Q(granted_by_permissions__user_groups__id=user_group_id))\ .distinct().only(*self.only_fields) diff --git a/apps/perms/api/asset/user_group_permission.py b/apps/perms/api/asset/user_group_permission.py index 60471432b..4b1464594 100644 --- a/apps/perms/api/asset/user_group_permission.py +++ b/apps/perms/api/asset/user_group_permission.py @@ -40,7 +40,9 @@ class UserGroupGrantedAssetsApi(ListAPIView): } def get_queryset(self): - user_group_id = self.kwargs.get('pk', '') + user_group_id = self.kwargs.get('pk') + if not user_group_id: + return Asset.objects.none() asset_perm_ids = list(AssetPermission.objects.valid().filter( user_groups__id=user_group_id @@ -127,7 +129,10 @@ class UserGroupGrantedNodesApi(ListAPIView): } def get_queryset(self): - user_group_id = self.kwargs.get('pk', '') + user_group_id = self.kwargs.get('pk') + if not user_group_id: + return Node.objects.none() + nodes = Node.objects.filter( Q(granted_by_permissions__user_groups__id=user_group_id) | Q(assets__granted_by_permissions__user_groups__id=user_group_id) diff --git a/apps/rbac/api/role.py b/apps/rbac/api/role.py index 251f7eeb7..4bf8c7016 100644 --- a/apps/rbac/api/role.py +++ b/apps/rbac/api/role.py @@ -61,6 +61,7 @@ class OrgRoleViewSet(RoleViewSet): # Sub view set class RolePermissionsViewSet(PermissionViewSet): + filterset_fields = [] rbac_perms = ( ('get_tree', 'role.view_role'), ) @@ -69,6 +70,9 @@ class RolePermissionsViewSet(PermissionViewSet): def get_queryset(self): role_id = self.kwargs.get('role_pk') + if not role_id: + return Role.objects.none() + role = Role.objects.get(id=role_id) self.scope = role.scope self.check_disabled = role.builtin diff --git a/apps/settings/api/ldap.py b/apps/settings/api/ldap.py index 406b2f18e..c6adee04d 100644 --- a/apps/settings/api/ldap.py +++ b/apps/settings/api/ldap.py @@ -101,7 +101,7 @@ class LDAPUserListApi(generics.ListAPIView): def get_queryset(self): if hasattr(self, 'swagger_fake_view'): - return [] + return User.objects.none() cache_police = self.request.query_params.get('cache_police', True) if cache_police in LDAP_USE_CACHE_FLAGS: users = self.get_queryset_from_cache() diff --git a/apps/terminal/api/command.py b/apps/terminal/api/command.py index f796e4b14..5fbb63220 100644 --- a/apps/terminal/api/command.py +++ b/apps/terminal/api/command.py @@ -166,6 +166,9 @@ class CommandViewSet(JMSBulkModelViewSet): def get_queryset(self): command_storage_id = self.request.query_params.get('command_storage_id') + if not command_storage_id: + return Command.objects.none() + storage = CommandStorage.objects.get(id=command_storage_id) if not storage.is_valid(): raise StorageInvalid diff --git a/apps/tickets/api/relation.py b/apps/tickets/api/relation.py index 44f2a47f9..674be316c 100644 --- a/apps/tickets/api/relation.py +++ b/apps/tickets/api/relation.py @@ -11,7 +11,7 @@ from orgs.utils import tmp_to_root_org class TicketSessionRelationViewSet(CreateModelMixin, JMSGenericViewSet): - queryset = TicketSession + queryset = TicketSession.objects.all() serializer_class = TicketSessionRelationSerializer