jumpserver/apps/perms/serializers/user_permission.py

55 lines
1.8 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.models import Node, Asset, Platform, Account
2022-11-30 09:52:00 +00:00
from assets.serializers.asset.common import AssetProtocolsSerializer
from common.drf.fields import ObjectRelatedField, LabeledChoiceField
2022-11-11 07:04:31 +00:00
from perms.serializers.permission import ActionChoicesField
__all__ = [
2022-12-15 08:02:34 +00:00
'NodePermedSerializer', 'AssetPermedSerializer',
'AccountsPermedSerializer'
]
class AssetPermedSerializer(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 = [
2022-12-15 08:02:34 +00:00
"id", "name", "address", 'domain', 'platform',
2022-11-09 12:58:45 +00:00
"comment", "org_id", "is_active",
]
2022-11-30 16:36:02 +00:00
fields = only_fields + ['protocols', 'category', 'type', 'specific'] + ['org_name']
read_only_fields = fields
2022-12-15 08:02:34 +00:00
class NodePermedSerializer(serializers.ModelSerializer):
class Meta:
model = Node
fields = [
'id', 'name', 'key', 'value', 'org_id', "assets_amount"
]
read_only_fields = fields
class AccountsPermedSerializer(serializers.ModelSerializer):
2022-11-11 07:04:31 +00:00
actions = ActionChoicesField(read_only=True)
class Meta:
model = Account
2022-12-15 08:02:34 +00:00
fields = [
'alias', 'name', 'username', 'has_username',
'has_secret', 'secret_type', 'actions'
]
read_only_fields = fields