mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-09-08 02:39:22 +00:00
perf: 修改 display field
This commit is contained in:
@@ -1,18 +1,19 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
from functools import partial
|
||||
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers
|
||||
|
||||
from common.drf.fields import EncryptedField, ObjectRelatedField, LabeledChoiceField
|
||||
from common.drf.serializers import CommonBulkSerializerMixin
|
||||
from common.validators import PhoneValidator
|
||||
from common.utils import pretty_string, get_logger
|
||||
from common.drf.fields import EncryptedField
|
||||
from common.validators import PhoneValidator
|
||||
from rbac.builtin import BuiltinRole
|
||||
from rbac.permissions import RBACPermission
|
||||
from rbac.models import OrgRoleBinding, SystemRoleBinding, Role
|
||||
from ..models import User
|
||||
from rbac.permissions import RBACPermission
|
||||
from ..const import PasswordStrategy
|
||||
from ..models import User
|
||||
|
||||
__all__ = [
|
||||
"UserSerializer",
|
||||
@@ -25,19 +26,8 @@ logger = get_logger(__file__)
|
||||
|
||||
|
||||
class RolesSerializerMixin(serializers.Serializer):
|
||||
system_roles = serializers.ManyRelatedField(
|
||||
child_relation=serializers.PrimaryKeyRelatedField(queryset=Role.system_roles),
|
||||
label=_("System roles"),
|
||||
)
|
||||
org_roles = serializers.ManyRelatedField(
|
||||
required=False,
|
||||
child_relation=serializers.PrimaryKeyRelatedField(queryset=Role.org_roles),
|
||||
label=_("Org roles"),
|
||||
)
|
||||
system_roles_display = serializers.SerializerMethodField(
|
||||
label=_("System roles display")
|
||||
)
|
||||
org_roles_display = serializers.SerializerMethodField(label=_("Org roles display"))
|
||||
system_roles = ObjectRelatedField(queryset=Role.system_roles, label=_("System roles"), many=True)
|
||||
org_roles = ObjectRelatedField(queryset=Role.org_roles, label=_("Org roles"), many=True)
|
||||
|
||||
@staticmethod
|
||||
def get_system_roles_display(user):
|
||||
@@ -60,8 +50,8 @@ class RolesSerializerMixin(serializers.Serializer):
|
||||
if action in ("partial_bulk_update", "bulk_update", "partial_update", "update"):
|
||||
action = "create"
|
||||
model_cls_field_mapper = {
|
||||
SystemRoleBinding: ["system_roles", "system_roles_display"],
|
||||
OrgRoleBinding: ["org_roles", "system_roles_display"],
|
||||
SystemRoleBinding: ["system_roles"],
|
||||
OrgRoleBinding: ["org_roles"],
|
||||
}
|
||||
|
||||
for model_cls, fields_names in model_cls_field_mapper.items():
|
||||
@@ -79,10 +69,8 @@ class RolesSerializerMixin(serializers.Serializer):
|
||||
return fields
|
||||
|
||||
|
||||
class UserSerializer(
|
||||
RolesSerializerMixin, CommonBulkSerializerMixin, serializers.ModelSerializer
|
||||
):
|
||||
password_strategy = serializers.ChoiceField(
|
||||
class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializers.ModelSerializer):
|
||||
password_strategy = LabeledChoiceField(
|
||||
choices=PasswordStrategy.choices,
|
||||
default=PasswordStrategy.email,
|
||||
required=False,
|
||||
@@ -93,9 +81,6 @@ class UserSerializer(
|
||||
mfa_force_enabled = serializers.BooleanField(
|
||||
read_only=True, label=_("MFA force enabled")
|
||||
)
|
||||
mfa_level_display = serializers.ReadOnlyField(
|
||||
source="get_mfa_level_display", label=_("MFA level display")
|
||||
)
|
||||
login_blocked = serializers.BooleanField(read_only=True, label=_("Login blocked"))
|
||||
is_expired = serializers.BooleanField(read_only=True, label=_("Is expired"))
|
||||
can_public_key_auth = serializers.ReadOnlyField(
|
||||
@@ -108,9 +93,6 @@ class UserSerializer(
|
||||
allow_null=True,
|
||||
max_length=1024,
|
||||
)
|
||||
# Todo: 这里看看该怎么搞
|
||||
# can_update = serializers.SerializerMethodField(label=_('Can update'))
|
||||
# can_delete = serializers.SerializerMethodField(label=_('Can delete'))
|
||||
custom_m2m_fields = {
|
||||
"system_roles": [BuiltinRole.system_user],
|
||||
"org_roles": [BuiltinRole.org_user],
|
||||
@@ -122,44 +104,22 @@ class UserSerializer(
|
||||
fields_mini = ["id", "name", "username"]
|
||||
# 只能写的字段, 这个虽然无法在框架上生效,但是更多对我们是提醒
|
||||
fields_write_only = [
|
||||
"password",
|
||||
"public_key",
|
||||
"password", "public_key",
|
||||
]
|
||||
# small 指的是 不需要计算的直接能从一张表中获取到的数据
|
||||
fields_small = (
|
||||
fields_mini
|
||||
+ fields_write_only
|
||||
+ [
|
||||
"email",
|
||||
"wechat",
|
||||
"phone",
|
||||
"mfa_level",
|
||||
"source",
|
||||
"source_display",
|
||||
"can_public_key_auth",
|
||||
"need_update_password",
|
||||
"mfa_enabled",
|
||||
"is_service_account",
|
||||
"is_valid",
|
||||
"is_expired",
|
||||
"is_active", # 布尔字段
|
||||
"date_expired",
|
||||
"date_joined",
|
||||
"last_login", # 日期字段
|
||||
"created_by",
|
||||
"comment", # 通用字段
|
||||
"is_wecom_bound",
|
||||
"is_dingtalk_bound",
|
||||
"is_feishu_bound",
|
||||
"is_otp_secret_key_bound",
|
||||
"wecom_id",
|
||||
"dingtalk_id",
|
||||
"feishu_id",
|
||||
]
|
||||
)
|
||||
fields_small = fields_mini + fields_write_only + [
|
||||
"email", "wechat", "phone", "mfa_level", "source",
|
||||
"need_update_password", "mfa_enabled",
|
||||
"is_service_account", "is_valid",
|
||||
"is_expired", "is_active", # 布尔字段
|
||||
"is_otp_secret_key_bound", "can_public_key_auth",
|
||||
"date_expired", "date_joined",
|
||||
"last_login", # 日期字段
|
||||
"created_by", "comment", # 通用字段
|
||||
"wecom_id", "dingtalk_id", "feishu_id",
|
||||
]
|
||||
# 包含不太常用的字段,可以没有
|
||||
fields_verbose = fields_small + [
|
||||
"mfa_level_display",
|
||||
"mfa_force_enabled",
|
||||
"is_first_login",
|
||||
"date_password_last_updated",
|
||||
@@ -168,25 +128,14 @@ class UserSerializer(
|
||||
# 外键的字段
|
||||
fields_fk = []
|
||||
# 多对多字段
|
||||
fields_m2m = [
|
||||
"groups",
|
||||
"groups_display",
|
||||
"system_roles",
|
||||
"org_roles",
|
||||
"system_roles_display",
|
||||
"org_roles_display",
|
||||
]
|
||||
fields_m2m = ["groups", "system_roles", "org_roles", ]
|
||||
# 在serializer 上定义的字段
|
||||
fields_custom = ["login_blocked", "password_strategy"]
|
||||
fields = fields_verbose + fields_fk + fields_m2m + fields_custom
|
||||
|
||||
read_only_fields = [
|
||||
"date_joined",
|
||||
"last_login",
|
||||
"created_by",
|
||||
"is_first_login",
|
||||
"wecom_id",
|
||||
"dingtalk_id",
|
||||
"date_joined", "last_login", "created_by",
|
||||
"is_first_login", "wecom_id", "dingtalk_id",
|
||||
"feishu_id",
|
||||
]
|
||||
disallow_self_update_fields = ["is_active"]
|
||||
@@ -205,18 +154,9 @@ class UserSerializer(
|
||||
"is_expired": {"label": _("Is expired")},
|
||||
"avatar_url": {"label": _("Avatar url")},
|
||||
"created_by": {"read_only": True, "allow_blank": True},
|
||||
"groups_display": {"label": _("Groups name")},
|
||||
"source_display": {"label": _("Source name")},
|
||||
"org_role_display": {"label": _("Organization role name")},
|
||||
"role_display": {"label": _("Super role name")},
|
||||
"total_role_display": {"label": _("Total role name")},
|
||||
"role": {"default": "User"},
|
||||
"is_wecom_bound": {"label": _("Is wecom bound")},
|
||||
"is_dingtalk_bound": {"label": _("Is dingtalk bound")},
|
||||
"is_feishu_bound": {"label": _("Is feishu bound")},
|
||||
"is_otp_secret_key_bound": {"label": _("Is OTP bound")},
|
||||
"phone": {"validators": [PhoneValidator()]},
|
||||
"system_role_display": {"label": _("System role name")},
|
||||
}
|
||||
|
||||
def validate_password(self, password):
|
||||
@@ -326,8 +266,6 @@ class InviteSerializer(RolesSerializerMixin, serializers.Serializer):
|
||||
help_text=_("For security, only list several users"),
|
||||
)
|
||||
system_roles = None
|
||||
system_roles_display = None
|
||||
org_roles_display = None
|
||||
|
||||
|
||||
class ServiceAccountSerializer(serializers.ModelSerializer):
|
||||
|
Reference in New Issue
Block a user