2016-10-19 11:30:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2016-11-10 08:59:50 +00:00
|
|
|
from rest_framework import serializers
|
2020-11-11 02:27:18 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2021-07-30 11:13:47 +00:00
|
|
|
from django.db.models import Q
|
2021-04-29 11:10:45 +00:00
|
|
|
|
2019-08-21 12:27:21 +00:00
|
|
|
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
|
2019-07-02 08:45:26 +00:00
|
|
|
from perms.models import AssetPermission, Action
|
2021-02-08 06:59:20 +00:00
|
|
|
from assets.models import Asset, Node, SystemUser
|
|
|
|
from users.models import User, UserGroup
|
2022-01-13 10:32:12 +00:00
|
|
|
from ..base import ActionsField, BasePermissionSerializer
|
2016-11-04 10:33:16 +00:00
|
|
|
|
2021-12-22 09:35:32 +00:00
|
|
|
__all__ = ['AssetPermissionSerializer']
|
2019-06-30 12:10:34 +00:00
|
|
|
|
|
|
|
|
2022-01-13 10:32:12 +00:00
|
|
|
class AssetPermissionSerializer(BasePermissionSerializer):
|
2021-07-30 11:13:47 +00:00
|
|
|
actions = ActionsField(required=False, allow_null=True, label=_("Actions"))
|
|
|
|
is_valid = serializers.BooleanField(read_only=True, label=_("Is valid"))
|
2021-03-11 12:09:23 +00:00
|
|
|
is_expired = serializers.BooleanField(read_only=True, label=_('Is expired'))
|
2021-07-30 11:13:47 +00:00
|
|
|
users_display = serializers.ListField(child=serializers.CharField(), label=_('Users display'), required=False)
|
|
|
|
user_groups_display = serializers.ListField(child=serializers.CharField(), label=_('User groups display'), required=False)
|
|
|
|
assets_display = serializers.ListField(child=serializers.CharField(), label=_('Assets display'), required=False)
|
|
|
|
nodes_display = serializers.ListField(child=serializers.CharField(), label=_('Nodes display'), required=False)
|
|
|
|
system_users_display = serializers.ListField(child=serializers.CharField(), label=_('System users display'), required=False)
|
2018-04-10 12:29:06 +00:00
|
|
|
|
2018-02-01 09:14:15 +00:00
|
|
|
class Meta:
|
2018-04-08 12:02:40 +00:00
|
|
|
model = AssetPermission
|
2021-04-28 08:42:54 +00:00
|
|
|
fields_mini = ['id', 'name']
|
|
|
|
fields_small = fields_mini + [
|
2020-10-20 06:15:29 +00:00
|
|
|
'is_active', 'is_expired', 'is_valid', 'actions',
|
|
|
|
'created_by', 'date_created', 'date_expired',
|
2021-09-09 06:37:43 +00:00
|
|
|
'date_start', 'comment', 'from_ticket'
|
2020-05-09 06:51:19 +00:00
|
|
|
]
|
2021-04-28 08:42:54 +00:00
|
|
|
fields_m2m = [
|
2021-07-30 11:13:47 +00:00
|
|
|
'users', 'users_display', 'user_groups', 'user_groups_display', 'assets',
|
|
|
|
'assets_display', 'nodes', 'nodes_display', 'system_users', 'system_users_display',
|
2020-10-20 06:15:29 +00:00
|
|
|
'users_amount', 'user_groups_amount', 'assets_amount',
|
|
|
|
'nodes_amount', 'system_users_amount',
|
2020-05-09 06:51:19 +00:00
|
|
|
]
|
2021-04-28 08:42:54 +00:00
|
|
|
fields = fields_small + fields_m2m
|
2021-09-09 06:37:43 +00:00
|
|
|
read_only_fields = ['created_by', 'date_created', 'from_ticket']
|
2020-11-11 02:27:18 +00:00
|
|
|
extra_kwargs = {
|
|
|
|
'is_expired': {'label': _('Is expired')},
|
|
|
|
'is_valid': {'label': _('Is valid')},
|
|
|
|
'actions': {'label': _('Actions')},
|
|
|
|
'users_amount': {'label': _('Users amount')},
|
|
|
|
'user_groups_amount': {'label': _('User groups amount')},
|
|
|
|
'assets_amount': {'label': _('Assets amount')},
|
|
|
|
'nodes_amount': {'label': _('Nodes amount')},
|
|
|
|
'system_users_amount': {'label': _('System users amount')},
|
|
|
|
}
|
2020-05-09 06:51:19 +00:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def setup_eager_loading(cls, queryset):
|
|
|
|
""" Perform necessary eager loading of data. """
|
2021-07-30 11:13:47 +00:00
|
|
|
queryset = queryset.prefetch_related(
|
|
|
|
'users', 'user_groups', 'assets', 'nodes', 'system_users'
|
|
|
|
)
|
2020-05-09 06:51:19 +00:00
|
|
|
return queryset
|
2021-04-28 08:42:54 +00:00
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
2021-06-15 09:59:43 +00:00
|
|
|
if 'system_users_display' in data:
|
|
|
|
# system_users_display 转化为 system_users
|
|
|
|
system_users = data.get('system_users', [])
|
|
|
|
system_users_display = data.pop('system_users_display')
|
|
|
|
|
|
|
|
for name in system_users_display:
|
|
|
|
system_user = SystemUser.objects.filter(name=name).first()
|
|
|
|
if system_user and system_user.id not in system_users:
|
|
|
|
system_users.append(system_user.id)
|
|
|
|
data['system_users'] = system_users
|
2021-04-28 08:42:54 +00:00
|
|
|
return super().to_internal_value(data)
|
|
|
|
|
2021-07-30 11:13:47 +00:00
|
|
|
@staticmethod
|
|
|
|
def perform_display_create(instance, **kwargs):
|
2021-04-28 08:42:54 +00:00
|
|
|
# 用户
|
|
|
|
users_to_set = User.objects.filter(
|
2021-07-30 11:13:47 +00:00
|
|
|
Q(name__in=kwargs.get('users_display')) |
|
|
|
|
Q(username__in=kwargs.get('users_display'))
|
2021-04-28 08:42:54 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.users.add(*users_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 用户组
|
2021-07-30 11:13:47 +00:00
|
|
|
user_groups_to_set = UserGroup.objects.filter(
|
|
|
|
name__in=kwargs.get('user_groups_display')
|
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.user_groups.add(*user_groups_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 资产
|
|
|
|
assets_to_set = Asset.objects.filter(
|
2021-07-30 11:13:47 +00:00
|
|
|
Q(ip__in=kwargs.get('assets_display')) |
|
|
|
|
Q(hostname__in=kwargs.get('assets_display'))
|
2021-04-28 08:42:54 +00:00
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.assets.add(*assets_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
# 节点
|
2021-07-30 11:13:47 +00:00
|
|
|
nodes_to_set = Node.objects.filter(
|
|
|
|
full_value__in=kwargs.get('nodes_display')
|
|
|
|
).distinct()
|
2021-04-30 03:32:33 +00:00
|
|
|
instance.nodes.add(*nodes_to_set)
|
2021-04-28 08:42:54 +00:00
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
display = {
|
2021-07-30 11:13:47 +00:00
|
|
|
'users_display': validated_data.pop('users_display', ''),
|
|
|
|
'user_groups_display': validated_data.pop('user_groups_display', ''),
|
|
|
|
'assets_display': validated_data.pop('assets_display', ''),
|
|
|
|
'nodes_display': validated_data.pop('nodes_display', '')
|
2021-04-28 08:42:54 +00:00
|
|
|
}
|
|
|
|
instance = super().create(validated_data)
|
|
|
|
self.perform_display_create(instance, **display)
|
|
|
|
return instance
|
2021-04-29 11:10:45 +00:00
|
|
|
|