Skip to content

Commit

Permalink
feat: get TenantID from header for OpenAPI (#2033)
Browse files Browse the repository at this point in the history
  • Loading branch information
rolin999 authored Jan 14, 2025
1 parent 6faf444 commit f4d92c0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
17 changes: 17 additions & 0 deletions src/bk-user/bkuser/apis/open_v3/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,28 @@
#
# 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 functools import cached_property

from apigw_manager.drf.authentication import ApiGatewayJWTAuthentication
from rest_framework.exceptions import ValidationError
from rest_framework.request import Request

from .permissions import ApiGatewayAppVerifiedPermission


class OpenApiCommonMixin:
authentication_classes = [ApiGatewayJWTAuthentication]
permission_classes = [ApiGatewayAppVerifiedPermission]

request: Request

TenantHeaderKey = "HTTP_X_BK_TENANT_ID"

@cached_property
def tenant_id(self) -> str:
tenant_id = self.request.META.get(self.TenantHeaderKey)

if not tenant_id:
raise ValidationError("X-Bk-Tenant-Id header is required")

return tenant_id
9 changes: 5 additions & 4 deletions src/bk-user/bkuser/apis/open_v3/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def get_queryset(self):
# TODO: 由于目前 DisplayName 渲染只与 full_name 相关,所以只查询 full_name
# 后续支持表达式,则需要查询表达式可配置的所有字段
return (
TenantUser.objects.filter(id__in=data["bk_usernames"])
TenantUser.objects.filter(id__in=data["bk_usernames"], tenant_id=self.tenant_id)
.select_related("data_source_user")
.only("id", "data_source_user__full_name")
)
Expand Down Expand Up @@ -93,7 +93,8 @@ class TenantUserRetrieveApi(OpenApiCommonMixin, generics.RetrieveAPIView):
responses={status.HTTP_200_OK: TenantUserRetrieveOutputSLZ()},
)
def get(self, request, *args, **kwargs):
return self.retrieve(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)


class TenantUserDepartmentListApi(OpenApiCommonMixin, generics.ListAPIView):
Expand All @@ -117,7 +118,7 @@ def get(self, request, *args, **kwargs):
slz.is_valid(raise_exception=True)
data = slz.validated_data

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

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

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

leader_ids = list(
DataSourceUserLeaderRelation.objects.filter(user=tenant_user.data_source_user).values_list(
Expand Down
3 changes: 2 additions & 1 deletion src/bk-user/tests/apis/open_v3/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@


@pytest.fixture
def api_client():
def api_client(random_tenant):
client = APIClient()
client.defaults["HTTP_X_BK_TENANT_ID"] = random_tenant.id
with mock.patch.object(OpenApiCommonMixin, "authentication_classes", []), mock.patch.object(
OpenApiCommonMixin, "permission_classes", []
):
Expand Down

0 comments on commit f4d92c0

Please sign in to comment.