jumpserver/apps/perms/serializers/asset/permission.py

111 lines
4.8 KiB
Python
Raw Normal View History

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
Dev beta (#3048) * [Update] 统一url地址 * [Update] 修改api * [Update] 使用规范的签名 * [Update] 修改url * [Update] 修改swagger * [Update] 添加serializer class避免报错 * [Update] 修改token * [Update] 支持api key * [Update] 支持生成api key * [Update] 修改api重定向 * [Update] 修改翻译 * [Update] 添加说明文档 * [Update] 修复浏览器关闭后session不失效的问题 * [Update] 修改一些内容 * [Update] 修改 jms脚本 * [Update] 修改重定向 * [Update] 修改搜索trim * [Update] 修改搜索trim * [Update] 添加sys log * [Bugfix] 修改登陆错误 * [Update] 优化User操作private_token的接口 (#3091) * [Update] 优化User操作private_token的接口 * [Update] 优化User操作private_token的接口 2 * [Bugfix] 解决授权了一个节点,当移动节点后,被移动的节点下的资产会放到未分组节点下的问题 * [Update] 升级jquery * [Update] 默认使用page * [Update] 修改使用Orgmodel view set * [Update] 支持 nv的硬盘 https://github.com/jumpserver/jumpserver/issues/1804 * [UPdate] 解决命令执行宽度问题 * [Update] 优化节点 * [Update] 修改nodes过多时创建比较麻烦 * [Update] 修改导入 * [Update] 节点获取更新 * [Update] 修改nodes * [Update] nodes显示full value * [Update] 统一使用nodes select2 函数 * [Update] 修改磁盘大小小数 * [Update] 修改 Node service * [Update] 优化授权节点 * [Update] 修改 node permission * [Update] 修改asset permission * [Stash] * [Update] 修改node assets api * [Update] 修改tree service,支持资产数量 * [Update] 修改暂时完成 * [Update] 修改一些bug
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
from assets.models import Asset, Node, SystemUser
from users.models import User, UserGroup
from ..base import ActionsField, BasePermissionSerializer
2016-11-04 10:33:16 +00:00
__all__ = ['AssetPermissionSerializer']
2019-06-30 12:10:34 +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
fields_mini = ['id', 'name']
fields_small = fields_mini + [
'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
]
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',
'users_amount', 'user_groups_amount', 'assets_amount',
'nodes_amount', 'system_users_amount',
2020-05-09 06:51:19 +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
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
return super().to_internal_value(data)
2021-07-30 11:13:47 +00:00
@staticmethod
def perform_display_create(instance, **kwargs):
# 用户
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'))
).distinct()
instance.users.add(*users_to_set)
# 用户组
2021-07-30 11:13:47 +00:00
user_groups_to_set = UserGroup.objects.filter(
name__in=kwargs.get('user_groups_display')
).distinct()
instance.user_groups.add(*user_groups_to_set)
# 资产
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'))
).distinct()
instance.assets.add(*assets_to_set)
# 节点
2021-07-30 11:13:47 +00:00
nodes_to_set = Node.objects.filter(
full_value__in=kwargs.get('nodes_display')
).distinct()
instance.nodes.add(*nodes_to_set)
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', '')
}
instance = super().create(validated_data)
self.perform_display_create(instance, **display)
return instance
2021-04-29 11:10:45 +00:00