Skip to content

Commit

Permalink
feat: mock tenant of current requet user
Browse files Browse the repository at this point in the history
  • Loading branch information
nannan00 committed Aug 18, 2023
1 parent f2da14e commit 52f0654
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apis/web/basic/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

class CurrentUserRetrieveOutputSLZ(serializers.Serializer):
username = serializers.CharField(help_text="用户名")
tenant_id = serializers.CharField(help_text="租户 ID")
1 change: 1 addition & 0 deletions src/bk-user/bkuser/apis/web/basic/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def get(self, request, *args, **kwargs):
current_user = request.user
info = {
"username": current_user.username,
"tenant_id": current_user.get_property("tenant_id"),
}

return Response(CurrentUserRetrieveOutputSLZ(instance=info).data)
12 changes: 6 additions & 6 deletions src/bk-user/bkuser/apis/web/tenant/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ class TenantSearchInputSLZ(serializers.Serializer):
name = serializers.CharField(required=False, help_text="租户名", allow_blank=True)


class TenantSearchManagerOutputSchema(serializers.Serializer):
class TenantSearchManagerOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="用户 ID")
username = serializers.CharField(help_text="用户名")
full_name = serializers.CharField(help_text="姓名")


class TenantSearchDataSourceOutputSchema(serializers.Serializer):
class TenantSearchDataSourceOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="数据源 ID")
name = serializers.CharField(help_text="数据源名称")

Expand All @@ -79,7 +79,7 @@ def get_logo(self, obj: Tenant) -> str:
def get_created_at(self, obj: Tenant) -> str:
return obj.created_at_display

@swagger_serializer_method(serializer_or_field=TenantSearchManagerOutputSchema(many=True))
@swagger_serializer_method(serializer_or_field=TenantSearchManagerOutputSLZ(many=True))
def get_managers(self, obj: Tenant) -> List[Dict]:
tenant_manager_map: Dict[str, List[TenantUserWithInheritedInfo]] = self.context["tenant_manager_map"]
managers = tenant_manager_map.get(obj.id) or []
Expand All @@ -91,7 +91,7 @@ def get_managers(self, obj: Tenant) -> List[Dict]:
for i in managers
]

@swagger_serializer_method(serializer_or_field=TenantSearchDataSourceOutputSchema(many=True))
@swagger_serializer_method(serializer_or_field=TenantSearchDataSourceOutputSLZ(many=True))
def get_data_sources(self, obj: Tenant) -> List[Dict]:
data_source_map: Dict[str, List[DataSourceSimpleInfo]] = self.context["data_source_map"]
data_sources = data_source_map.get(obj.id) or []
Expand All @@ -105,7 +105,7 @@ class TenantUpdateInputSLZ(serializers.Serializer):
feature_flags = TenantFeatureFlagSLZ(help_text="租户特性集")


class TenantRetrieveManagerOutputSchema(serializers.Serializer):
class TenantRetrieveManagerOutputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="用户 ID")
username = serializers.CharField(help_text="租户用户名")
full_name = serializers.CharField(help_text="用户姓名")
Expand All @@ -123,7 +123,7 @@ class TenantRetrieveOutputSLZ(serializers.Serializer):
feature_flags = TenantFeatureFlagSLZ(help_text="租户特性集")
managers = serializers.SerializerMethodField()

@swagger_serializer_method(serializer_or_field=TenantRetrieveManagerOutputSchema(many=True))
@swagger_serializer_method(serializer_or_field=TenantRetrieveManagerOutputSLZ(many=True))
def get_managers(self, obj: Tenant) -> List[Dict]:
tenant_manager_map: Dict[str, List[TenantUserWithInheritedInfo]] = self.context["tenant_manager_map"]
managers = tenant_manager_map.get(obj.id) or []
Expand Down
5 changes: 5 additions & 0 deletions src/bk-user/bkuser/auth/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import logging
import traceback

from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import BaseBackend
from django.db import IntegrityError
Expand Down Expand Up @@ -43,6 +44,10 @@ def authenticate(self, request=None, bk_token=None):
user.set_property(key="language", value=user_info.get("language", ""))
user.set_property(key="time_zone", value=user_info.get("time_zone", ""))

# FIXME: 新版登录后删除该MOCK逻辑
tenant_id = settings.MOCK_USER_TENANTS.get(username) or settings.MOCK_USER_DEFAULT_TENANT
user.set_property(key="tenant_id", value=tenant_id)

return user

except IntegrityError:
Expand Down
3 changes: 3 additions & 0 deletions src/bk-user/bkuser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,3 +468,6 @@
DEFAULT_DATA_SOURCE_USER_LOGO = ""
# 默认手机国际区号
DEFAULT_PHONE_COUNTRY_CODE = env.str("DEFAULT_PHONE_COUNTRY_CODE", default="86")
# FIXME: 待新版登录完成后删除Mock用户租户数据,格式:key1=value1;key2=value2
MOCK_USER_TENANTS = env.dict("MOCK_USER_TENANTS", default={})
MOCK_USER_DEFAULT_TENANT = env.str("MOCK_USER_DEFAULT_TENANT", default="")

0 comments on commit 52f0654

Please sign in to comment.