diff --git a/src/bk-user/bkuser/apps/idp/general/__init__.py b/src/bk-user/bkuser/apps/idp/general/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/bk-user/bkuser/apps/idp/local/__init__.py b/src/bk-user/bkuser/apps/idp/local/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/bk-user/bkuser/plugins/local/utils.py b/src/bk-user/bkuser/plugins/local/utils.py new file mode 100644 index 000000000..967453d8a --- /dev/null +++ b/src/bk-user/bkuser/plugins/local/utils.py @@ -0,0 +1,17 @@ +# -*- 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 hashlib import sha256 + + +def gen_code(username_or_org: str) -> str: + # 本地数据源数据没有提供用户及部门 code 的方式, + # 因此使用 sha256 计算以避免冲突,也便于后续插入 DB 时进行比较 + return sha256(username_or_org.encode("utf-8")).hexdigest() diff --git a/src/bk-user/tests/plugins/local/test_utils.py b/src/bk-user/tests/plugins/local/test_utils.py new file mode 100644 index 000000000..10fe2261e --- /dev/null +++ b/src/bk-user/tests/plugins/local/test_utils.py @@ -0,0 +1,31 @@ +# -*- 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. +""" +import pytest +from bkuser.plugins.local.utils import gen_code + + +@pytest.mark.parametrize( + ("raw", "excepted"), + [ + ("", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"), + ("zhangsan", "82721d844c04f22502244a87061313e08d49c6e8d3fbee47e12201880a5ce6cb"), + ("lisi", "45ade5c9806fd3585a4ce199adbdf058301a01e625bf514252913e641191edd9"), + ("wangwu", "4ada86d5cca8cf4ac00399a0a16738ee8c944df3767b4af7151ca8b148cfe9e3"), + ("公司", "e06ff957ed48e868a41b7e7e4460ce371e398108db542cf1cd1d61795b83e647"), + ("公司/部门A", "2da9c820170b44354632bd3fe26ad09f4836b5977d2f6a5ff20afe7b143ac1e1"), + ("公司/部门A/中心AA", "63986fb4ef27820413deb3f7c57cc36aef2ea898f03d8355e854d73b5c14e09c"), + ("公司/部门A/中心AA/小组AAA ", "e75be6462a8ff8b9b843b3c2e419db455b4477023f98941508bc19cfa3982ec0"), + ], +) +def test_gen_code(raw, excepted): + # 重要:如果该单元测试挂了,说明修改了本地数据源用户 & 部门的 Code 的生成规则 + # 改行为会导致新同步的数据,无法与 DB 中的数据匹配上,将会触发数据重建!!! + assert gen_code(raw) == excepted