Skip to content

Commit

Permalink
feat: create data_source user #1154
Browse files Browse the repository at this point in the history
  • Loading branch information
Canway-shiisa committed Aug 23, 2023
1 parent bc38f8a commit 48aef00
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/apis/web/data_source/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/apis/web/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
]
12 changes: 0 additions & 12 deletions src/bk-user/bkuser/apps/data_source/constants.py

This file was deleted.

6 changes: 2 additions & 4 deletions src/bk-user/bkuser/apps/data_source/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions src/bk-user/bkuser/biz/data_source_organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/biz/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand Down
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/common/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/common/error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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异常"))
# 数据源
Expand Down
11 changes: 5 additions & 6 deletions src/bk-user/bkuser/common/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -26,17 +25,17 @@ 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:
# 按照指定地区码解析手机号
phonenumbers.parse(phone, region)

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

0 comments on commit 48aef00

Please sign in to comment.