fix: 修复创建用户roles必填问题 (#7745)

Co-authored-by: Jiangjie.Bai <bugatti_it@163.com>
pull/7746/head
fit2bot 3 years ago committed by GitHub
parent a20884e2ad
commit 31d219524b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -6,6 +6,7 @@ from rest_framework import serializers
from common.mixins import CommonBulkSerializerMixin from common.mixins import CommonBulkSerializerMixin
from common.validators import PhoneValidator from common.validators import PhoneValidator
from orgs.utils import current_org
from rbac.permissions import RBACPermission from rbac.permissions import RBACPermission
from rbac.models import OrgRoleBinding, SystemRoleBinding from rbac.models import OrgRoleBinding, SystemRoleBinding
from ..models import User from ..models import User
@ -20,10 +21,12 @@ __all__ = [
class RolesSerializerMixin(serializers.Serializer): class RolesSerializerMixin(serializers.Serializer):
system_roles = serializers.ManyRelatedField( system_roles = serializers.ManyRelatedField(
allow_empty=False,
child_relation=serializers.PrimaryKeyRelatedField(queryset=Role.system_roles), child_relation=serializers.PrimaryKeyRelatedField(queryset=Role.system_roles),
label=_('System roles'), label=_('System roles'),
) )
org_roles = serializers.ManyRelatedField( org_roles = serializers.ManyRelatedField(
required=False,
child_relation=serializers.PrimaryKeyRelatedField(queryset=Role.org_roles), child_relation=serializers.PrimaryKeyRelatedField(queryset=Role.org_roles),
label=_('Org roles'), label=_('Org roles'),
) )
@ -67,6 +70,16 @@ class RolesSerializerMixin(serializers.Serializer):
self.pop_roles_if_need(fields) self.pop_roles_if_need(fields)
return fields return fields
@staticmethod
def _validate_org_roles(attrs):
if current_org.is_root():
attrs.pop('org_roles', None)
return attrs
org_roles = attrs.get('org_roles', None)
if not org_roles:
raise serializers.ValidationError({'org_roles': _('This field is required.')})
return attrs
class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializers.ModelSerializer): class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializers.ModelSerializer):
password_strategy = serializers.ChoiceField( password_strategy = serializers.ChoiceField(
@ -175,6 +188,7 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
return attrs return attrs
def validate(self, attrs): def validate(self, attrs):
attrs = self._validate_org_roles(attrs)
attrs = self.change_password_to_raw(attrs) attrs = self.change_password_to_raw(attrs)
attrs = self.clean_auth_fields(attrs) attrs = self.clean_auth_fields(attrs)
attrs.pop('password_strategy', None) attrs.pop('password_strategy', None)
@ -182,8 +196,7 @@ class UserSerializer(RolesSerializerMixin, CommonBulkSerializerMixin, serializer
def save_and_set_custom_m2m_fields(self, validated_data, save_handler): def save_and_set_custom_m2m_fields(self, validated_data, save_handler):
m2m_values = { m2m_values = {
f: validated_data.pop(f, None) f: validated_data.pop(f, None) for f in self.custom_m2m_fields
for f in self.custom_m2m_fields
} }
instance = save_handler(validated_data) instance = save_handler(validated_data)
for field_name, value in m2m_values.items(): for field_name, value in m2m_values.items():

Loading…
Cancel
Save