Skip to content

Commit

Permalink
minor: remove data source name global unique limit (#1410)
Browse files Browse the repository at this point in the history
  • Loading branch information
narasux authored Nov 22, 2023
1 parent e549b18 commit e806bfb
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/bk-user/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ i18n-mo: ## 将 django.po 文件编译成 django.mo 文件
python manage.py compilemessages

i18n-all: i18n-po i18n-mo ## 执行 i18n-po & i18n-mo

package-plugin: ## 打包自定义数据源插件
kubectl create configmap bk-user-plugin-${name} --from-file=bkuser/plugins/${name} --dry-run=client -o yaml > bk-user-plugin-${name}.yaml

package-idp-plugin: ## 打包自定义 IDP 插件
kubectl create configmap bk-user-idp-plugin-${name} --from-file=bkuser/idp_plugins/${name} --dry-run=client -o yaml > bk-user-idp-plugin-${name}.yaml
Empty file modified src/bk-user/bin/start.sh
100644 → 100755
Empty file.
13 changes: 6 additions & 7 deletions src/bk-user/bkuser/apis/web/data_source/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DataSourceSearchOutputSLZ(serializers.Serializer):
cooperation_tenants = serializers.SerializerMethodField(help_text="协作公司")
status = serializers.CharField(help_text="数据源状态")
updater = serializers.CharField(help_text="更新者")
updated_at = serializers.SerializerMethodField(help_text="更新时间")
updated_at = serializers.CharField(help_text="更新时间", source="updated_at_display")

def get_plugin_name(self, obj: DataSource) -> str:
return self.context["data_source_plugin_map"].get(obj.plugin_id, "")
Expand All @@ -64,9 +64,6 @@ def get_cooperation_tenants(self, obj: DataSource) -> List[str]:
# TODO 目前未支持数据源跨租户协作,因此该数据均为空
return []

def get_updated_at(self, obj: DataSource) -> str:
return obj.updated_at_display


class DataSourceFieldMappingSLZ(serializers.Serializer):
"""单个数据源字段映射"""
Expand Down Expand Up @@ -116,7 +113,7 @@ class DataSourceCreateInputSLZ(serializers.Serializer):
sync_config = DataSourceSyncConfigSLZ(help_text="数据源同步配置", required=False)

def validate_name(self, name: str) -> str:
if DataSource.objects.filter(name=name).exists():
if DataSource.objects.filter(name=name, owner_tenant_id=self.context["tenant_id"]).exists():
raise ValidationError(_("同名数据源已存在"))

return name
Expand Down Expand Up @@ -195,7 +192,7 @@ def validate_name(self, name: str) -> str:
if name == self.context["current_name"]:
return name

if DataSource.objects.filter(name=name).exists():
if DataSource.objects.filter(name=name, owner_tenant_id=self.context["tenant_id"]).exists():
raise ValidationError(_("同名数据源已存在"))

return name
Expand Down Expand Up @@ -305,7 +302,9 @@ def validate(self, attrs):
except PDValidationError as e:
raise ValidationError(_("密码规则配置不合法: {}").format(stringify_pydantic_error(e)))
else:
attrs["password_rule"] = get_default_plugin_cfg(DataSourcePluginEnum.LOCAL).password_rule.to_rule() # type: ignore
attrs["password_rule"] = get_default_plugin_cfg( # type: ignore
DataSourcePluginEnum.LOCAL,
).password_rule.to_rule()

return attrs

Expand Down
5 changes: 1 addition & 4 deletions src/bk-user/bkuser/apis/web/tenant/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ class TenantRetrieveManagerOutputSLZ(serializers.Serializer):
class TenantRetrieveOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="租户 ID")
name = serializers.CharField(help_text="租户名")
updated_at = serializers.SerializerMethodField(help_text="更新时间")
updated_at = serializers.CharField(help_text="更新时间", source="updated_at_display")
logo = serializers.SerializerMethodField(help_text="租户 Logo")
feature_flags = TenantFeatureFlagSLZ(help_text="租户特性集")
managers = serializers.SerializerMethodField(help_text="租户管理员")
Expand All @@ -203,9 +203,6 @@ def get_managers(self, obj: Tenant) -> List[Dict]:
def get_logo(self, obj: Tenant) -> str:
return obj.logo or settings.DEFAULT_TENANT_LOGO

def get_updated_at(self, obj: Tenant) -> str:
return obj.updated_at_display


class TenantUserSearchInputSLZ(serializers.Serializer):
keyword = serializers.CharField(help_text="搜索关键字", required=False)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.20 on 2023-11-21 04:01

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('data_source', '0002_init_builtin_data_source_plugin'),
]

operations = [
migrations.AlterField(
model_name='datasource',
name='name',
field=models.CharField(max_length=128, verbose_name='数据源名称'),
),
migrations.AlterUniqueTogether(
name='datasource',
unique_together={('name', 'owner_tenant_id')},
),
]
3 changes: 2 additions & 1 deletion src/bk-user/bkuser/apps/data_source/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def create(self, *args, **kwargs):


class DataSource(AuditedModel):
name = models.CharField("数据源名称", max_length=128, unique=True)
name = models.CharField("数据源名称", max_length=128)
owner_tenant_id = models.CharField("归属租户", max_length=64, db_index=True)
status = models.CharField(
"数据源状态",
Expand All @@ -73,6 +73,7 @@ class DataSource(AuditedModel):

class Meta:
ordering = ["id"]
unique_together = [("name", "owner_tenant_id")]

@property
def is_local(self) -> bool:
Expand Down
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/apps/permission/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def has_object_permission(self, request, view, obj): # noqa: C901
return False

if action == PermAction.MANAGE_PLATFORM:
return is_super_manager(tenant_id, username)
return is_super_manager(cur_tenant_id, username)
if action == PermAction.MANAGE_TENANT:
return is_tenant_manager(tenant_id, username)
if action == PermAction.USE_PLATFORM:
Expand Down
12 changes: 9 additions & 3 deletions src/bk-user/bkuser/apps/sync/syncers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from bkuser.apps.sync.constants import DataSourceSyncObjectType, SyncOperation, TenantSyncObjectType
from bkuser.apps.sync.context import DataSourceSyncTaskContext, TenantSyncTaskContext
from bkuser.apps.sync.converters import DataSourceUserConverter
from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantUser
from bkuser.apps.tenant.models import Tenant, TenantDepartment, TenantUser, TenantUserValidityPeriodConfig
from bkuser.common.constants import PERMANENT_TIME
from bkuser.plugins.models import RawDataSourceDepartment, RawDataSourceUser
from bkuser.utils.tree import bfs_traversal_tree, build_forest_with_parent_relations
Expand Down Expand Up @@ -532,5 +532,11 @@ def sync(self):
self.ctx.recorder.add(SyncOperation.CREATE, TenantSyncObjectType.USER, waiting_create_tenant_users)

def _get_user_account_expired_at(self) -> datetime.datetime:
"""FIXME (su) 支持读取账号有效期配置,然后累加到 timezone.now() 上,目前是直接返回 PERMANENT_TIME"""
return PERMANENT_TIME
"""若存在账号有效期配置且已启用,则累加到 timezone.now() 上,否则直接返回 PERMANENT_TIME"""
cfg = TenantUserValidityPeriodConfig.objects.filter(tenant=self.tenant).first()
# NOTE: cfg.validity_period == -1 表示永久有效期
if not (cfg and cfg.enabled and cfg.validity_period > 0):
return PERMANENT_TIME

expired_at = timezone.now() + datetime.timedelta(days=cfg.validity_period)
return expired_at if expired_at < PERMANENT_TIME else PERMANENT_TIME
2 changes: 1 addition & 1 deletion src/bk-user/bkuser/monitoring/healthz/probes.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class MysqlProbe(MySQLProbe):

class _RedisProbe(RedisProbe):
name = "bkuser-redis"
redis_url = f"redis://{settings.REDIS_HOST}:{settings.REDIS_PORT}/{settings.REDIS_DB}"
redis_url = f"redis://:{settings.REDIS_PASSWORD}@{settings.REDIS_HOST}:{settings.REDIS_PORT}/{settings.REDIS_DB}"


class _RedisSentinelProbe(RedisSentinelProbe):
Expand Down

0 comments on commit e806bfb

Please sign in to comment.