From 48aef0072cb39cf09a0b8916d62ac13793fd74a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E4=BD=A9=E7=8F=8A=5Bshiisa=5D?= Date: Wed, 23 Aug 2023 14:29:49 +0800 Subject: [PATCH] feat: create data_source user #1154 --- src/bk-user/bkuser/apis/web/data_source/views.py | 2 +- src/bk-user/bkuser/apis/web/urls.py | 2 +- src/bk-user/bkuser/apps/data_source/constants.py | 12 ------------ src/bk-user/bkuser/apps/data_source/models.py | 6 ++---- src/bk-user/bkuser/biz/data_source_organization.py | 3 +-- src/bk-user/bkuser/biz/validators.py | 2 +- src/bk-user/bkuser/common/constants.py | 5 +++++ src/bk-user/bkuser/common/error_codes.py | 5 +++++ src/bk-user/bkuser/common/validators.py | 11 +++++------ 9 files changed, 21 insertions(+), 27 deletions(-) delete mode 100644 src/bk-user/bkuser/apps/data_source/constants.py diff --git a/src/bk-user/bkuser/apis/web/data_source/views.py b/src/bk-user/bkuser/apis/web/data_source/views.py index 826be3bb6..61e8489ed 100644 --- a/src/bk-user/bkuser/apis/web/data_source/views.py +++ b/src/bk-user/bkuser/apis/web/data_source/views.py @@ -40,7 +40,7 @@ def post(self, request, *args, **kwargs): data = slz.validated_data # 不允许对非本地数据源进行用户新增操作 - if not data_source.user_editable: + if not data_source.editable: raise error_codes.CANNOT_CREATE_USER # 校验是否已存在该用户 if DataSourceUser.objects.filter(username=data["username"], data_source=data_source).exists(): diff --git a/src/bk-user/bkuser/apis/web/urls.py b/src/bk-user/bkuser/apis/web/urls.py index 151b0329b..e9c37eb17 100644 --- a/src/bk-user/bkuser/apis/web/urls.py +++ b/src/bk-user/bkuser/apis/web/urls.py @@ -15,6 +15,6 @@ path("basic/", include("bkuser.apis.web.basic.urls")), # 租户 path("tenants/", include("bkuser.apis.web.tenant.urls")), - path("data-sources/", include("bkuser.apis.web.data_source.urls")), path("tenant-organization/", include("bkuser.apis.web.organization.urls")), + path("data-sources/", include("bkuser.apis.web.data_source.urls")), ] diff --git a/src/bk-user/bkuser/apps/data_source/constants.py b/src/bk-user/bkuser/apps/data_source/constants.py deleted file mode 100644 index bea284805..000000000 --- a/src/bk-user/bkuser/apps/data_source/constants.py +++ /dev/null @@ -1,12 +0,0 @@ -# -*- 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. -""" -CHINESE_REGION = "CN" -CHINESE_PHONE_LENGTH = 11 diff --git a/src/bk-user/bkuser/apps/data_source/models.py b/src/bk-user/bkuser/apps/data_source/models.py index 710627ffb..3d6662fe3 100644 --- a/src/bk-user/bkuser/apps/data_source/models.py +++ b/src/bk-user/bkuser/apps/data_source/models.py @@ -42,10 +42,8 @@ class Meta: ordering = ["id"] @property - def user_editable(self) -> bool: - if self.plugin.id == "local": - return True - return False + def editable(self) -> bool: + return self.plugin.id == "local" class DataSourceUser(TimestampedModel): diff --git a/src/bk-user/bkuser/biz/data_source_organization.py b/src/bk-user/bkuser/biz/data_source_organization.py index 1aa1eebc2..7df2d972e 100644 --- a/src/bk-user/bkuser/biz/data_source_organization.py +++ b/src/bk-user/bkuser/biz/data_source_organization.py @@ -51,8 +51,7 @@ def create_user( # TODO:补充日志 with transaction.atomic(): # 创建数据源用户 - create_user_info_map = {"data_source": data_source, **base_user_info.model_dump()} - user = DataSourceUser.objects.create(**create_user_info_map) + user = DataSourceUser.objects.create(data_source=data_source, **base_user_info.model_dump()) # 批量创建数据源用户-部门关系 department_user_relation_objs = [ diff --git a/src/bk-user/bkuser/biz/validators.py b/src/bk-user/bkuser/biz/validators.py index 1e4f91f70..9227cedaa 100644 --- a/src/bk-user/bkuser/biz/validators.py +++ b/src/bk-user/bkuser/biz/validators.py @@ -15,7 +15,7 @@ from rest_framework.exceptions import ValidationError TENANT_ID_REGEX = r"^[a-zA-Z][a-zA-Z0-9-]{2,31}" -DATA_SOURCE_USERNAME_REGEX = r"^[a-zA-Z][a-zA-Z0-9-]{2,31}" +DATA_SOURCE_USERNAME_REGEX = r"^(\d|[a-zA-Z])([a-zA-Z0-9._-]){0,31}" logger = logging.getLogger(__name__) diff --git a/src/bk-user/bkuser/common/constants.py b/src/bk-user/bkuser/common/constants.py index f9a3d0311..a509732ab 100644 --- a/src/bk-user/bkuser/common/constants.py +++ b/src/bk-user/bkuser/common/constants.py @@ -20,3 +20,8 @@ class BkLanguageEnum(str, StructuredEnum): # 永久:2100-01-01 00:00:00 PERMANENT_TIME = datetime.datetime(year=2100, month=1, day=1, hour=0, minute=0, second=0) + +# 中国手机号地区码 +CHINESE_REGION = "CN" +# 中国手机号长度 +CHINESE_PHONE_LENGTH = 11 diff --git a/src/bk-user/bkuser/common/error_codes.py b/src/bk-user/bkuser/common/error_codes.py index 0b4ee658d..e870a6d9a 100644 --- a/src/bk-user/bkuser/common/error_codes.py +++ b/src/bk-user/bkuser/common/error_codes.py @@ -66,6 +66,11 @@ class ErrorCodes: code_category=ErrorCodeCategoryEnum.INTERNAL, status_code=HTTPStatus.INTERNAL_SERVER_ERROR, ) + # 通用校验异常 + PHONE_COUNTRY_CODE_PARSE_ERROR = ErrorCode(_("手机地区码解析异常")) + PHONE_LENGTH_ERROR = ErrorCode(_("手机长度异常")) + PHONE_PARSE_ERROR = ErrorCode(_("手机号码解析异常")) + # 调用外部系统API REMOTE_REQUEST_ERROR = ErrorCode(_("调用外部系统API异常")) # 数据源 diff --git a/src/bk-user/bkuser/common/validators.py b/src/bk-user/bkuser/common/validators.py index 6fec49de0..99e10d904 100644 --- a/src/bk-user/bkuser/common/validators.py +++ b/src/bk-user/bkuser/common/validators.py @@ -11,11 +11,10 @@ import logging import phonenumbers -from django.utils.translation import gettext_lazy as _ from phonenumbers import NumberParseException, region_code_for_country_code -from rest_framework import serializers -from bkuser.apps.data_source.constants import CHINESE_PHONE_LENGTH, CHINESE_REGION +from bkuser.common.constants import CHINESE_PHONE_LENGTH, CHINESE_REGION +from bkuser.common.error_codes import error_codes logger = logging.getLogger(__name__) @@ -26,12 +25,12 @@ def validate_phone_with_country_code(phone: str, country_code: str): except ValueError: logger.debug("failed to parse phone_country_code: %s, ", country_code) - raise serializers.ValidationError(_("手机地区码 {} 不符合解析规则").format(country_code)) + raise error_codes.PHONE_COUNTRY_CODE_PARSE_ERROR # phonenumbers库在验证号码的时:过短会解析为有效号码,超过250的字节才算超长 # =》所以这里需要显式做中国号码的长度校验 if region == CHINESE_REGION and len(phone) != CHINESE_PHONE_LENGTH: - raise serializers.ValidationError(_("手机号 {} 不符合长度要求").format(phone)) + raise error_codes.PHONE_LENGTH_ERROR try: # 按照指定地区码解析手机号 @@ -39,4 +38,4 @@ def validate_phone_with_country_code(phone: str, country_code: str): except NumberParseException: # pylint: disable=broad-except logger.debug("failed to parse phone number: %s", phone) - raise serializers.ValidationError(_("手机号 {} 不符合规则").format(phone)) + raise error_codes.PHONE_PARSE_ERROR