Skip to content

Commit

Permalink
fix: apis of tenant only support real data source (#2039)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 authored Feb 7, 2025
1 parent 5036036 commit eb60400
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 16 deletions.
14 changes: 14 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@
from functools import cached_property

from apigw_manager.drf.authentication import ApiGatewayJWTAuthentication
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import ValidationError
from rest_framework.request import Request

from bkuser.apps.data_source.constants import DataSourceTypeEnum
from bkuser.apps.data_source.models import DataSource

from .permissions import ApiGatewayAppVerifiedPermission


Expand All @@ -39,3 +43,13 @@ def tenant_id(self) -> str:
raise ValidationError("X-Bk-Tenant-Id header is required")

return tenant_id

@cached_property
def real_data_source_id(self) -> int:
data_source = (
DataSource.objects.filter(owner_tenant_id=self.tenant_id, type=DataSourceTypeEnum.REAL).only("id").first()
)
if not data_source:
raise ValidationError(_("当前租户不存在实名用户数据源"))

return data_source.id
28 changes: 28 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
# TencentBlueKing is pleased to support the open source community by making
# 蓝鲸智云 - 用户管理 (bk-user) available.
# Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
# Licensed under the MIT License (the "License"); you may not use this file except
# in compliance with the License. You may obtain a copy of the License at
#
# http://opensource.org/licenses/MIT
#
# Unless required by applicable law or agreed to in writing, software distributed under
# the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
# either express or implied. See the License for the specific language governing permissions and
# limitations under the License.
#
# We undertake not to change the open source license (MIT license) applicable
# to the current version of the project delivered to anyone in the future.

from bkuser.common.pagination import CustomPageNumberPagination


def gen_pagination_class(max_page_size: int):
"""根据不同的最大页数生成对应的 Pagination 类"""

class Pagination(CustomPageNumberPagination):
def __init__(self):
self.max_page_size = max_page_size

return Pagination
12 changes: 8 additions & 4 deletions src/bk-user/bkuser/apis/open_v3/views/department.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ class TenantDepartmentRetrieveApi(OpenApiCommonMixin, generics.RetrieveAPIView):
获取部门信息(支持是否包括祖先部门)
"""

queryset = TenantDepartment.objects.all()
lookup_url_kwarg = "id"
serializer_class = TenantDepartmentRetrieveOutputSLZ

def get_queryset(self):
return TenantDepartment.objects.filter(tenant_id=self.tenant_id, data_source_id=self.real_data_source_id)

@swagger_auto_schema(
tags=["open_v3.department"],
Expand Down Expand Up @@ -85,7 +86,9 @@ class TenantDepartmentListApi(OpenApiCommonMixin, generics.ListAPIView):
responses={status.HTTP_200_OK: TenantDepartmentListOutputSLZ(many=True)},
)
def get(self, request, *args, **kwargs):
depts = TenantDepartment.objects.select_related("data_source_department").filter(tenant=self.tenant_id)
depts = TenantDepartment.objects.select_related("data_source_department").filter(
tenant=self.tenant_id, data_source_id=self.real_data_source_id
)

# 分页
page = self.paginate_queryset(depts)
Expand Down Expand Up @@ -116,7 +119,8 @@ def get(self, request, *args, **kwargs):
data = slz.validated_data

tenant_department = get_object_or_404(
TenantDepartment.objects.filter(tenant_id=self.tenant_id), id=kwargs["id"]
TenantDepartment.objects.filter(tenant_id=self.tenant_id, data_source_id=self.real_data_source_id),
id=kwargs["id"],
)

relation = DataSourceDepartmentRelation.objects.get(department_id=tenant_department.data_source_department_id)
Expand Down
32 changes: 20 additions & 12 deletions src/bk-user/bkuser/apis/open_v3/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from rest_framework.response import Response

from bkuser.apis.open_v3.mixins import OpenApiCommonMixin
from bkuser.apis.open_v3.pagination import gen_pagination_class
from bkuser.apis.open_v3.serializers.user import (
TenantUserDepartmentListInputSLZ,
TenantUserDepartmentListOutputSLZ,
Expand All @@ -40,7 +41,6 @@
)
from bkuser.apps.tenant.models import TenantDepartment, TenantUser
from bkuser.biz.organization import DataSourceDepartmentHandler
from bkuser.common.pagination import CustomPageNumberPagination

logger = logging.getLogger(__name__)

Expand All @@ -63,7 +63,11 @@ def get_queryset(self):
# TODO: 由于目前 DisplayName 渲染只与 full_name 相关,所以只查询 full_name
# 后续支持表达式,则需要查询表达式可配置的所有字段
return (
TenantUser.objects.filter(id__in=data["bk_usernames"], tenant_id=self.tenant_id)
TenantUser.objects.filter(
id__in=data["bk_usernames"],
tenant_id=self.tenant_id,
data_source_id=self.real_data_source_id,
)
.select_related("data_source_user")
.only("id", "data_source_user__full_name")
)
Expand All @@ -84,19 +88,20 @@ class TenantUserRetrieveApi(OpenApiCommonMixin, generics.RetrieveAPIView):
根据用户 bk_username 获取用户信息
"""

queryset = TenantUser.objects.all()
lookup_url_kwarg = "id"
serializer_class = TenantUserRetrieveOutputSLZ

def get_queryset(self):
return TenantUser.objects.filter(tenant_id=self.tenant_id, data_source_id=self.real_data_source_id)

@swagger_auto_schema(
tags=["open_v3.user"],
operation_id="retrieve_user",
operation_description="查询用户信息",
responses={status.HTTP_200_OK: TenantUserRetrieveOutputSLZ()},
)
def get(self, request, *args, **kwargs):
tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.tenant_id), id=kwargs["id"])
return Response(TenantUserRetrieveOutputSLZ(tenant_user).data)
return self.retrieve(request, *args, **kwargs)


class TenantUserDepartmentListApi(OpenApiCommonMixin, generics.ListAPIView):
Expand All @@ -106,8 +111,6 @@ class TenantUserDepartmentListApi(OpenApiCommonMixin, generics.ListAPIView):

pagination_class = None

serializer_class = TenantUserDepartmentListOutputSLZ

@swagger_auto_schema(
tags=["open_v3.user"],
operation_id="list_user_department",
Expand All @@ -120,7 +123,10 @@ def get(self, request, *args, **kwargs):
slz.is_valid(raise_exception=True)
data = slz.validated_data

tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.tenant_id), id=kwargs["id"])
tenant_user = get_object_or_404(
TenantUser.objects.filter(tenant_id=self.tenant_id, data_source_id=self.real_data_source_id),
id=kwargs["id"],
)

return Response(
TenantUserDepartmentListOutputSLZ(self._get_dept_info(tenant_user, data["with_ancestors"]), many=True).data
Expand Down Expand Up @@ -193,7 +199,10 @@ class TenantUserLeaderListApi(OpenApiCommonMixin, generics.ListAPIView):
serializer_class = TenantUserLeaderListOutputSLZ

def get_queryset(self) -> QuerySet[TenantUser]:
tenant_user = get_object_or_404(TenantUser.objects.filter(tenant_id=self.tenant_id), id=self.kwargs["id"])
tenant_user = get_object_or_404(
TenantUser.objects.filter(tenant_id=self.tenant_id, data_source_id=self.real_data_source_id),
id=self.kwargs["id"],
)

leader_ids = list(
DataSourceUserLeaderRelation.objects.filter(user=tenant_user.data_source_user).values_list(
Expand All @@ -218,15 +227,14 @@ class TenantUserListApi(OpenApiCommonMixin, generics.ListAPIView):
查询用户列表
"""

pagination_class = CustomPageNumberPagination
pagination_class.max_page_size = 1000
pagination_class = gen_pagination_class(max_page_size=1000)

serializer_class = TenantUserListOutputSLZ

def get_queryset(self) -> QuerySet[TenantUser]:
return (
TenantUser.objects.select_related("data_source_user")
.filter(tenant_id=self.tenant_id)
.filter(tenant_id=self.tenant_id, data_source_id=self.real_data_source_id)
.only("id", "data_source_user__full_name")
)

Expand Down

0 comments on commit eb60400

Please # to comment.