jumpserver/apps/perms/serializers/user_permission.py

57 lines
1.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers
2022-11-09 12:58:45 +00:00
from assets.const import Category, AllTypes
from assets.serializers.asset.common import AssetProtocolsSerializer
from assets.models import Node, Asset, Platform, Account
from common.drf.fields import ObjectRelatedField, LabeledChoiceField
2022-11-11 07:04:31 +00:00
from perms.serializers.permission import ActionChoicesField
__all__ = [
2022-11-09 12:58:45 +00:00
'NodeGrantedSerializer', 'AssetGrantedSerializer',
'ActionsSerializer', 'AccountsPermedSerializer'
]
class AssetGrantedSerializer(serializers.ModelSerializer):
""" 被授权资产的数据结构 """
platform = ObjectRelatedField(required=False, queryset=Platform.objects, label=_('Platform'))
protocols = AssetProtocolsSerializer(many=True, required=False, label=_('Protocols'))
2022-11-09 12:58:45 +00:00
category = LabeledChoiceField(choices=Category.choices, read_only=True, label=_('Category'))
type = LabeledChoiceField(choices=AllTypes.choices(), read_only=True, label=_('Type'))
class Meta:
model = Asset
only_fields = [
"id", "name", "address",
2022-11-09 12:58:45 +00:00
'domain', 'platform',
"comment", "org_id", "is_active",
]
2022-11-09 12:58:45 +00:00
fields = only_fields + ['category', 'type'] + ['org_name']
read_only_fields = fields
class NodeGrantedSerializer(serializers.ModelSerializer):
class Meta:
model = Node
fields = [
'id', 'name', 'key', 'value', 'org_id', "assets_amount"
]
read_only_fields = fields
class ActionsSerializer(serializers.Serializer):
2022-11-11 07:04:31 +00:00
actions = ActionChoicesField(read_only=True)
class AccountsPermedSerializer(serializers.ModelSerializer):
2022-11-11 07:04:31 +00:00
actions = ActionChoicesField(read_only=True)
class Meta:
model = Account
fields = ['id', 'name', 'username', 'secret_type', 'has_secret', 'actions']
read_only_fields = fields