Skip to content

Commit

Permalink
feat: enable update data source user username in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux committed Dec 14, 2023
1 parent 50524c9 commit 0e65f52
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
13 changes: 9 additions & 4 deletions src/bk-user/bkuser/apps/sync/syncers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

from django.utils import timezone

from bkuser.apps.data_source.constants import TenantUserIdRuleEnum
from bkuser.apps.data_source.models import (
DataSource,
DataSourceDepartment,
Expand Down Expand Up @@ -212,6 +213,9 @@ def __init__(
self.overwrite = overwrite
self.incremental = incremental
self.converter = DataSourceUserConverter(data_source, ctx.logger)
# 由于在部分老版本迁移过来的数据源中租户用户 ID 会由 username + 规则 拼接生成,
# 该类数据源同步时候不可更新 username,而全新数据源对应租户 ID 都是 uuid 则不受影响
self.enable_update_username = bool(data_source.owner_tenant_user_id_rule == TenantUserIdRuleEnum.UUID4_HEX)

def sync(self):
self.ctx.logger.info(f"receive {len(self.raw_users)} users from data source plugin") # noqa: G004
Expand Down Expand Up @@ -312,17 +316,18 @@ def _update_users(self, raw_users: List[RawDataSourceUser]):
# 先进行 diff,不是所有的用户都要被更新,只有有字段不一致的,才需要更新
target_user = user_map[u.code]
if (
# u.username == target_user.username
u.full_name == target_user.full_name
(u.username == target_user.username or not self.enable_update_username)
and u.full_name == target_user.full_name
and u.email == target_user.email
and u.phone == target_user.phone
and u.phone_country_code == target_user.phone_country_code
and u.extras == target_user.extras
):
continue

# FIXME (su) 评估 username 更新策略 https://github.com/TencentBlueKing/bk-user/issues/1325
# u.username = target_user.username
if self.enable_update_username:
u.username = target_user.username

u.full_name = target_user.full_name
u.email = target_user.email
u.phone = target_user.phone
Expand Down
37 changes: 33 additions & 4 deletions src/bk-user/tests/apps/sync/test_syncers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from typing import Dict, List, Set, Tuple

import pytest
from bkuser.apps.data_source.constants import TenantUserIdRuleEnum
from bkuser.apps.data_source.models import (
DataSource,
DataSourceDepartment,
Expand Down Expand Up @@ -139,8 +140,7 @@ def test_update_with_overwrite(
random_raw_user,
):
# 1. 修改用户姓名,电话,邮箱,年龄等信息
# su 注:用户名更新暂时被禁用,需要进一步讨论策略
# raw_users[0].properties["username"] = "zhangsan_rename"
raw_users[0].properties["username"] = "zhangsan_rename"
raw_users[0].properties["full_name"] = "张三的另一个名字"
raw_users[0].properties["email"] = "[email protected]"
raw_users[0].properties["phone"] = "13512345655"
Expand Down Expand Up @@ -181,8 +181,7 @@ def test_update_with_overwrite(

# 验证内置/自定义字段被更新
zhangsan = users.filter(code="zhangsan").first()
# su 注:用户名更新暂时被禁用,需要进一步讨论策略
# assert zhangsan.username == "zhangsan_rename"
assert zhangsan.username == "zhangsan_rename"
assert zhangsan.full_name == "张三的另一个名字"
assert zhangsan.email == "[email protected]"
assert zhangsan.phone == "13512345655"
Expand Down Expand Up @@ -300,6 +299,36 @@ def test_update_with_invalid_dept(self, data_source_sync_task_ctx, full_local_da

assert data_source_sync_task_ctx.logger.has_warning is True

def test_update_with_unable_update_username(self, data_source_sync_task_ctx, full_local_data_source):
"""对于某些特定的数据源,同步并不会更新 username"""
raw_users = [
RawDataSourceUser(
code="zhangsan",
properties={
"username": "zhangsan_rename",
"full_name": "张三重命名",
"email": "[email protected]",
"phone": "07712345678",
"phone_country_code": "44",
},
leaders=[],
departments=[],
)
]
# 修改数据源的特定属性,导致其在同步数据源用户时候无法更新 username
full_local_data_source.owner_tenant_user_id_rule = TenantUserIdRuleEnum.USERNAME
full_local_data_source.save()

DataSourceUserSyncer(
data_source_sync_task_ctx, full_local_data_source, raw_users, overwrite=True, incremental=False
).sync()

zhangsan = DataSourceUser.objects.filter(data_source=full_local_data_source, code="zhangsan").first()
# 不支持数据源用户的 username 更新的情况
assert zhangsan.username == "zhangsan"
assert zhangsan.full_name == "张三重命名"
assert zhangsan.email == "[email protected]"

def test_destroy(self, data_source_sync_task_ctx, full_local_data_source):
raw_users: List[RawDataSourceUser] = []

Expand Down

0 comments on commit 0e65f52

Please sign in to comment.