2016-10-19 11:30:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
2016-11-10 08:59:50 +00:00
|
|
|
from rest_framework import serializers
|
2018-10-30 04:06:39 +00:00
|
|
|
|
2018-04-10 12:29:06 +00:00
|
|
|
from common.fields import StringManyToManyField
|
2018-10-30 04:06:39 +00:00
|
|
|
from .models import AssetPermission
|
2018-12-10 02:11:54 +00:00
|
|
|
from assets.models import Node, Asset, SystemUser
|
2018-10-30 04:06:39 +00:00
|
|
|
from assets.serializers import AssetGrantedSerializer
|
2016-11-04 10:33:16 +00:00
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
__all__ = [
|
|
|
|
'AssetPermissionCreateUpdateSerializer', 'AssetPermissionListSerializer',
|
|
|
|
'AssetPermissionUpdateUserSerializer', 'AssetPermissionUpdateAssetSerializer',
|
|
|
|
'AssetPermissionNodeSerializer', 'GrantedNodeSerializer',
|
|
|
|
'GrantedAssetSerializer', 'GrantedSystemUserSerializer',
|
|
|
|
]
|
|
|
|
|
2016-11-04 10:33:16 +00:00
|
|
|
|
2018-02-01 09:14:15 +00:00
|
|
|
class AssetPermissionCreateUpdateSerializer(serializers.ModelSerializer):
|
2016-11-04 10:33:16 +00:00
|
|
|
class Meta:
|
2018-04-08 12:02:40 +00:00
|
|
|
model = AssetPermission
|
2018-05-04 06:54:58 +00:00
|
|
|
exclude = ('created_by', 'date_created')
|
2017-12-19 11:38:09 +00:00
|
|
|
|
|
|
|
|
2018-02-01 09:14:15 +00:00
|
|
|
class AssetPermissionListSerializer(serializers.ModelSerializer):
|
2018-04-10 12:29:06 +00:00
|
|
|
users = StringManyToManyField(many=True, read_only=True)
|
|
|
|
user_groups = StringManyToManyField(many=True, read_only=True)
|
|
|
|
assets = StringManyToManyField(many=True, read_only=True)
|
|
|
|
nodes = StringManyToManyField(many=True, read_only=True)
|
|
|
|
system_users = StringManyToManyField(many=True, read_only=True)
|
2019-01-15 11:01:33 +00:00
|
|
|
is_valid = serializers.BooleanField()
|
|
|
|
is_expired = serializers.BooleanField()
|
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
|
2018-02-01 09:14:15 +00:00
|
|
|
fields = '__all__'
|
2017-12-19 11:38:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUpdateUserSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AssetPermission
|
|
|
|
fields = ['id', 'users']
|
|
|
|
|
|
|
|
|
|
|
|
class AssetPermissionUpdateAssetSerializer(serializers.ModelSerializer):
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = AssetPermission
|
|
|
|
fields = ['id', 'assets']
|
|
|
|
|
2018-10-30 04:06:39 +00:00
|
|
|
|
|
|
|
class AssetPermissionNodeSerializer(serializers.ModelSerializer):
|
|
|
|
asset = AssetGrantedSerializer(required=False)
|
|
|
|
assets_amount = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
tree_id = serializers.SerializerMethodField()
|
|
|
|
tree_parent = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Node
|
|
|
|
fields = [
|
|
|
|
'id', 'key', 'value', 'asset', 'is_node', 'org_id',
|
|
|
|
'tree_id', 'tree_parent', 'assets_amount',
|
|
|
|
]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_assets_amount(obj):
|
|
|
|
return obj.assets_amount
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_tree_id(obj):
|
|
|
|
return obj.key
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_tree_parent(obj):
|
|
|
|
return obj.parent_key
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
|
2018-12-17 10:20:44 +00:00
|
|
|
class NodeGrantedSerializer(serializers.ModelSerializer):
|
|
|
|
"""
|
|
|
|
授权资产组
|
|
|
|
"""
|
|
|
|
assets_granted = AssetGrantedSerializer(many=True, read_only=True)
|
|
|
|
assets_amount = serializers.SerializerMethodField()
|
|
|
|
parent = serializers.SerializerMethodField()
|
|
|
|
name = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Node
|
|
|
|
fields = [
|
|
|
|
'id', 'key', 'name', 'value', 'parent',
|
|
|
|
'assets_granted', 'assets_amount', 'org_id',
|
|
|
|
]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_assets_amount(obj):
|
|
|
|
return len(obj.assets_granted)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_name(obj):
|
|
|
|
return obj.name
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_parent(obj):
|
|
|
|
return obj.parent.id
|
|
|
|
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
class GrantedNodeSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Node
|
|
|
|
fields = [
|
|
|
|
'id', 'name', 'key', 'value',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class GrantedAssetSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Asset
|
|
|
|
fields = [
|
|
|
|
'id', 'hostname', 'ip', 'port', 'protocol', 'platform',
|
|
|
|
'domain', 'is_active', 'comment'
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class GrantedSystemUserSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = SystemUser
|
|
|
|
fields = [
|
|
|
|
'id', 'name', 'username', 'protocol', 'priority',
|
|
|
|
'login_mode', 'comment'
|
|
|
|
]
|