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: 限制 Logo 文件尺寸大小 #1469

Merged
merged 6 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
)
from bkuser.apps.tenant.constants import UserFieldDataType
from bkuser.apps.tenant.models import TenantUserCustomField
from bkuser.biz.validators import validate_data_source_user_username
from bkuser.biz.validators import validate_data_source_user_username, validate_logo
from bkuser.common.validators import validate_phone_with_country_code

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -154,7 +154,12 @@ class UserCreateInputSLZ(serializers.Serializer):
help_text="手机号国际区号", required=False, default=settings.DEFAULT_PHONE_COUNTRY_CODE
)
phone = serializers.CharField(help_text="手机号")
logo = serializers.CharField(help_text="用户 Logo", required=False, default=settings.DEFAULT_DATA_SOURCE_USER_LOGO)
logo = serializers.CharField(
help_text="用户 Logo",
required=False,
default=settings.DEFAULT_DATA_SOURCE_USER_LOGO,
validators=[validate_logo],
)
extras = serializers.JSONField(help_text="自定义字段", default=dict)

department_ids = serializers.ListField(help_text="部门ID列表", child=serializers.IntegerField(), default=[])
Expand Down Expand Up @@ -258,7 +263,13 @@ class UserUpdateInputSLZ(serializers.Serializer):
email = serializers.CharField(help_text="邮箱")
phone_country_code = serializers.CharField(help_text="手机国际区号")
phone = serializers.CharField(help_text="手机号")
logo = serializers.CharField(help_text="用户 Logo", allow_blank=True, required=False, default="")
logo = serializers.CharField(
help_text="用户 Logo",
allow_blank=True,
required=False,
default=settings.DEFAULT_DATA_SOURCE_USER_LOGO,
validators=[validate_logo],
)
extras = serializers.JSONField(help_text="自定义字段")

department_ids = serializers.ListField(help_text="部门ID列表", child=serializers.IntegerField())
Expand Down
3 changes: 2 additions & 1 deletion src/bk-user/bkuser/apis/web/personal_center/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from bkuser.apis.web.organization.serializers import TenantUserDepartmentOutputSLZ, TenantUserLeaderOutputSLZ
from bkuser.apps.tenant.models import TenantUser
from bkuser.biz.tenant import TenantUserHandler
from bkuser.biz.validators import validate_logo
from bkuser.common.validators import validate_phone_with_country_code


Expand Down Expand Up @@ -145,4 +146,4 @@ def validate(self, attrs):


class TenantUserLogoUpdateInputSLZ(serializers.Serializer):
logo = serializers.CharField(help_text="用户 Logo")
logo = serializers.CharField(help_text="用户 Logo", validators=[validate_logo])
16 changes: 13 additions & 3 deletions src/bk-user/bkuser/apis/web/tenant/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from bkuser.apps.tenant.models import Tenant, TenantUser
from bkuser.biz.data_source import DataSourceSimpleInfo
from bkuser.biz.tenant import TenantUserWithInheritedInfo
from bkuser.biz.validators import validate_data_source_user_username
from bkuser.biz.validators import validate_data_source_user_username, validate_logo
from bkuser.common.passwd import PasswordValidator
from bkuser.plugins.base import get_default_plugin_cfg
from bkuser.plugins.constants import DataSourcePluginEnum
Expand Down Expand Up @@ -83,7 +83,13 @@ def validate_notification(self, notification: Dict[str, Any]) -> Dict[str, Any]:
class TenantCreateInputSLZ(serializers.Serializer):
id = serializers.CharField(help_text="租户 ID")
name = serializers.CharField(help_text="租户名称")
logo = serializers.CharField(help_text="租户 Logo", required=False, allow_blank=True, default="")
logo = serializers.CharField(
help_text="租户 Logo",
required=False,
allow_blank=True,
default=settings.DEFAULT_TENANT_LOGO,
validators=[validate_logo],
)
managers = serializers.ListField(help_text="管理人列表", child=TenantManagerCreateInputSLZ(), allow_empty=False)
feature_flags = TenantFeatureFlagSLZ(help_text="租户特性集")
password_initial_config = TenantManagerPasswordInitialConfigSLZ()
Expand Down Expand Up @@ -161,7 +167,11 @@ def get_data_sources(self, obj: Tenant) -> List[Dict]:
class TenantUpdateInputSLZ(serializers.Serializer):
name = serializers.CharField(help_text="租户名称")
logo = serializers.CharField(
help_text="租户 Logo", required=False, allow_blank=True, default=settings.DEFAULT_TENANT_LOGO
help_text="租户 Logo",
required=False,
allow_blank=True,
default=settings.DEFAULT_TENANT_LOGO,
validators=[validate_logo],
)
manager_ids = serializers.ListField(child=serializers.CharField(), help_text="租户用户 ID 列表", allow_empty=False)
feature_flags = TenantFeatureFlagSLZ(help_text="租户特性集")
Expand Down
18 changes: 18 additions & 0 deletions src/bk-user/bkuser/biz/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
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 base64
import logging
import re

from django.conf import settings
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import ValidationError

Expand All @@ -36,3 +38,19 @@ def validate_tenant_custom_field_name(value):
"{} 不符合 自定义字段 的命名规范: 由3-32位字母、数字、下划线(_)字符组成,以字母开头,字母或数字结尾" # noqa: E501
).format(value),
)


def validate_logo(value):
if not value:
return

try:
decoded_data = base64.b64decode(value)
except Exception:
# Decoding failed or invalid Base64-encoded image
logger.exception("invalid image")
raise ValidationError(_("无效logo文件"))
narasux marked this conversation as resolved.
Show resolved Hide resolved

# Check if the size exceeds the specified limit
if len(decoded_data) / 1024 > settings.MAX_LOGO_SIZE:
narasux marked this conversation as resolved.
Show resolved Hide resolved
raise ValidationError(_("logo 文件大小超过限制{}KB").format(settings.MAX_LOGO_SIZE))
narasux marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions src/bk-user/bkuser/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@

# ------------------------------------------ 业务逻辑配置 ------------------------------------------

# logo文件大小限制,单位为: KB
MAX_LOGO_SIZE = env.int("MAX_LOGO_SIZE", 256)

# 数据源插件默认Logo,值为base64格式图片数据
DEFAULT_DATA_SOURCE_PLUGIN_LOGO = ""
# 租户默认Logo,值为base64格式图片数据
Expand Down
Loading