Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
brookylin committed Apr 29, 2024
2 parents 7846dba + 8cbfa3b commit 4c66052
Show file tree
Hide file tree
Showing 33 changed files with 2,207 additions and 757 deletions.
2 changes: 1 addition & 1 deletion src/bk-user/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ENV NPM_VERSION 9.6.7

COPY src/pages /
WORKDIR /
RUN npm install
RUN npm install --legacy-peer-deps
RUN npm run build

FROM python:3.10.12-slim-bullseye
Expand Down
59 changes: 55 additions & 4 deletions src/bk-user/bkuser/apis/web/organization/serializers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"""

from .departments import (
OptionalTenantDepartmentListInputSLZ,
OptionalTenantDepartmentListOutputSLZ,
TenantDepartmentCreateInputSLZ,
TenantDepartmentCreateOutputSLZ,
TenantDepartmentListInputSLZ,
Expand All @@ -18,15 +20,42 @@
TenantDepartmentSearchOutputSLZ,
TenantDepartmentUpdateInputSLZ,
)

# noqa: F401
from .tenants import TenantListOutputSLZ, TenantRetrieveOutputSLZ # noqa: F401
from .users import TenantUserSearchInputSLZ, TenantUserSearchOutputSLZ # noqa: F401
from .relations import (
TenantDeptUserRelationBatchCreateInputSLZ,
TenantDeptUserRelationBatchDeleteInputSLZ,
TenantDeptUserRelationBatchPatchInputSLZ,
TenantDeptUserRelationBatchUpdateInputSLZ,
)
from .tenants import (
RequiredTenantUserFieldOutputSLZ,
TenantListOutputSLZ,
TenantRetrieveOutputSLZ,
)
from .users import (
OptionalTenantUserListInputSLZ,
OptionalTenantUserListOutputSLZ,
TenantUserBatchCreateInputSLZ,
TenantUserBatchCreatePreviewInputSLZ,
TenantUserBatchCreatePreviewOutputSLZ,
TenantUserBatchDeleteInputSLZ,
TenantUserCreateInputSLZ,
TenantUserCreateOutputSLZ,
TenantUserListInputSLZ,
TenantUserListOutputSLZ,
TenantUserOrganizationPathOutputSLZ,
TenantUserPasswordResetInputSLZ,
TenantUserRetrieveOutputSLZ,
TenantUserSearchInputSLZ,
TenantUserSearchOutputSLZ,
TenantUserStatusUpdateOutputSLZ,
TenantUserUpdateInputSLZ,
)

__all__ = [
# 租户
"TenantListOutputSLZ",
"TenantRetrieveOutputSLZ",
"RequiredTenantUserFieldOutputSLZ",
# 租户部门
"TenantDepartmentListInputSLZ",
"TenantDepartmentListOutputSLZ",
Expand All @@ -35,7 +64,29 @@
"TenantDepartmentUpdateInputSLZ",
"TenantDepartmentSearchInputSLZ",
"TenantDepartmentSearchOutputSLZ",
"OptionalTenantDepartmentListInputSLZ",
"OptionalTenantDepartmentListOutputSLZ",
# 租户用户
"OptionalTenantUserListInputSLZ",
"OptionalTenantUserListOutputSLZ",
"TenantUserSearchInputSLZ",
"TenantUserSearchOutputSLZ",
"TenantUserListInputSLZ",
"TenantUserListOutputSLZ",
"TenantUserCreateInputSLZ",
"TenantUserCreateOutputSLZ",
"TenantUserRetrieveOutputSLZ",
"TenantUserUpdateInputSLZ",
"TenantUserPasswordResetInputSLZ",
"TenantUserOrganizationPathOutputSLZ",
"TenantUserStatusUpdateOutputSLZ",
"TenantUserBatchCreateInputSLZ",
"TenantUserBatchCreatePreviewInputSLZ",
"TenantUserBatchCreatePreviewOutputSLZ",
"TenantUserBatchDeleteInputSLZ",
# 租户部门 - 用户关系
"TenantDeptUserRelationBatchCreateInputSLZ",
"TenantDeptUserRelationBatchUpdateInputSLZ",
"TenantDeptUserRelationBatchPatchInputSLZ",
"TenantDeptUserRelationBatchDeleteInputSLZ",
]
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,17 @@ def get_tenant_name(self, obj: TenantDepartment) -> str:
@swagger_serializer_method(serializer_or_field=serializers.CharField)
def get_organization_path(self, obj: TenantDepartment) -> str:
return self.context["org_path_map"].get(obj.id, obj.data_source_department.name)


class OptionalTenantDepartmentListInputSLZ(serializers.Serializer):
keyword = serializers.CharField(help_text="搜索关键字", min_length=2, max_length=64, required=False)


class OptionalTenantDepartmentListOutputSLZ(serializers.Serializer):
id = serializers.IntegerField(help_text="租户部门 ID")
name = serializers.CharField(help_text="部门名称", source="data_source_department.name")
organization_path = serializers.SerializerMethodField(help_text="组织路径")

@swagger_serializer_method(serializer_or_field=serializers.CharField)
def get_organization_path(self, obj: TenantDepartment) -> str:
return self.context["org_path_map"].get(obj.id, obj.data_source_department.name)
93 changes: 93 additions & 0 deletions src/bk-user/bkuser/apis/web/organization/serializers/relations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
"""
TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-用户管理(Bk-User) available.
Copyright (C) 2017-2021 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.
"""
from typing import List

from django.conf import settings
from django.utils.translation import gettext_lazy as _
from rest_framework import serializers
from rest_framework.exceptions import ValidationError

from bkuser.apps.tenant.models import TenantDepartment, TenantUser
from bkuser.common.serializers import StringArrayField


def _validate_tenant_user_ids(user_ids: List[str], tenant_id: str, data_source_id: int) -> None:
"""校验租户用户 ID 列表中数据是否合法"""
exists_tenant_users = TenantUser.objects.filter(
id__in=user_ids, tenant_id=tenant_id, data_source_id=data_source_id
)
if invalid_user_ids := set(user_ids) - set(exists_tenant_users.values_list("id", flat=True)):
raise ValidationError(_("用户 ID {} 在当前租户中不存在").format(", ".join(invalid_user_ids)))


def _validate_tenant_department_ids(department_ids: List[int], tenant_id: str, data_source_id: int) -> None:
"""校验租户部门 ID 列表中数据是否合法"""
exists_tenant_depts = TenantDepartment.objects.filter(
id__in=department_ids, tenant_id=tenant_id, data_source_id=data_source_id
)
if invalid_dept_ids := set(department_ids) - set(exists_tenant_depts.values_list("id", flat=True)):
raise ValidationError(_("部门 ID {} 在当前租户中不存在").format(invalid_dept_ids))


class TenantDeptUserRelationBatchCreateInputSLZ(serializers.Serializer):
"""追加目标组织"""

user_ids = serializers.ListField(
help_text="用户 ID 列表",
child=serializers.CharField(help_text="租户用户 ID"),
min_length=1,
max_length=settings.ORGANIZATION_BATCH_OPERATION_API_LIMIT,
)
target_department_ids = serializers.ListField(
help_text="目标部门 ID 列表",
child=serializers.IntegerField(help_text="目标部门 ID"),
min_length=1,
max_length=10,
)

def validate_user_ids(self, user_ids: List[str]) -> List[str]:
_validate_tenant_user_ids(user_ids, self.context["tenant_id"], self.context["data_source_id"])
return user_ids

def validate_target_department_ids(self, department_ids: List[int]) -> List[int]:
_validate_tenant_department_ids(department_ids, self.context["tenant_id"], self.context["data_source_id"])
return department_ids


class TenantDeptUserRelationBatchUpdateInputSLZ(TenantDeptUserRelationBatchCreateInputSLZ):
"""清空并加入组织"""


class TenantDeptUserRelationBatchPatchInputSLZ(TenantDeptUserRelationBatchCreateInputSLZ):
"""移至目标组织"""

source_department_id = serializers.IntegerField(help_text="当前部门 ID")

def validate_source_department_id(self, department_id: int) -> int:
_validate_tenant_department_ids([department_id], self.context["tenant_id"], self.context["data_source_id"])
return department_id


class TenantDeptUserRelationBatchDeleteInputSLZ(serializers.Serializer):
"""移出当前组织"""

user_ids = StringArrayField(
help_text="用户 ID 列表", min_items=1, max_items=settings.ORGANIZATION_BATCH_OPERATION_API_LIMIT
)
source_department_id = serializers.IntegerField(help_text="当前部门 ID")

def validate_user_ids(self, user_ids: List[str]) -> List[str]:
_validate_tenant_user_ids(user_ids, self.context["tenant_id"], self.context["data_source_id"])
return user_ids

def validate_source_department_id(self, department_id: int) -> int:
_validate_tenant_department_ids([department_id], self.context["tenant_id"], self.context["data_source_id"])
return department_id
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ def get_data_source(self, obj: Tenant) -> Dict[str, Any] | None:
return None

return TenantDataSourceSLZ(data_source).data


class RequiredTenantUserFieldOutputSLZ(serializers.Serializer):
name = serializers.CharField(help_text="字段名称")
display_name = serializers.CharField(help_text="字段展示名")
tips = serializers.CharField(help_text="提示信息")
Loading

0 comments on commit 4c66052

Please sign in to comment.