Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enable update data source user username in some cases #1482

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/apps/data_source/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ def is_local(self) -> bool:
"""检查类型是否为本地数据源"""
return self.plugin.id == DataSourcePluginEnum.LOCAL

@property
def is_username_frozen(self) -> bool:
"""用户名在初始化后不可再次更新,对于租户用户 ID 为 uuid 的数据源无效"""
return bool(self.owner_tenant_user_id_rule != TenantUserIdRuleEnum.UUID4_HEX)

def get_plugin_cfg(self) -> BasePluginConfig:
"""获取插件配置

Expand Down
12 changes: 8 additions & 4 deletions src/bk-user/bkuser/apps/sync/syncers.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,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 = not data_source.is_username_frozen

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 +315,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"] = "zhangsan_rename@m.com"
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 == "zhangsan_rename@m.com"
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": "zhangsan@m.com",
"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 == "zhangsan@m.com"

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

Expand Down
Loading