jumpserver/apps/acls/serializers/command_acl.py

67 lines
2.3 KiB
Python
Raw Normal View History

2022-12-02 03:12:14 +00:00
from django.utils.translation import ugettext_lazy as _
2022-12-06 10:26:07 +00:00
from rest_framework import serializers
2022-12-02 03:12:14 +00:00
2022-12-06 10:26:07 +00:00
from terminal.models import Session
2022-12-02 03:12:14 +00:00
from acls.models import CommandGroup, CommandFilterACL
2022-12-06 10:26:07 +00:00
from common.utils import lazyproperty, get_object_or_none
from common.drf.fields import ObjectRelatedField, LabeledChoiceField
2022-12-06 10:26:07 +00:00
from orgs.utils import tmp_to_root_org
2022-12-02 03:12:14 +00:00
from orgs.mixins.serializers import BulkOrgResourceModelSerializer
2022-12-02 03:53:07 +00:00
from .base import BaseUserAssetAccountACLSerializerMixin as BaseSerializer
2022-12-02 03:12:14 +00:00
2022-12-06 10:26:07 +00:00
__all__ = ["CommandFilterACLSerializer", "CommandGroupSerializer", "CommandReviewSerializer"]
2022-12-02 03:12:14 +00:00
2022-12-02 03:53:07 +00:00
class CommandGroupSerializer(BulkOrgResourceModelSerializer):
type = LabeledChoiceField(
choices=CommandGroup.TypeChoices.choices, default=CommandGroup.TypeChoices.command,
label=_('Type')
)
2022-12-02 03:53:07 +00:00
class Meta:
model = CommandGroup
fields = ['id', 'name', 'type', 'content', 'ignore_case', 'comment']
2022-12-02 03:53:07 +00:00
class CommandFilterACLSerializer(BaseSerializer, BulkOrgResourceModelSerializer):
command_groups = ObjectRelatedField(
queryset=CommandGroup.objects, many=True, required=False, label=_('Command group')
)
2022-12-02 03:12:14 +00:00
2022-12-02 03:53:07 +00:00
class Meta(BaseSerializer.Meta):
2022-12-02 03:12:14 +00:00
model = CommandFilterACL
fields = BaseSerializer.Meta.fields + ['command_groups']
2022-12-06 10:26:07 +00:00
class CommandReviewSerializer(serializers.Serializer):
session_id = serializers.UUIDField(required=True, allow_null=False)
cmd_filter_acl_id = serializers.UUIDField(required=True, allow_null=False)
run_command = serializers.CharField(required=True, allow_null=False)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.session = None
self.cmd_filter_acl = None
def validate_session_id(self, pk):
self.session = self.validate_object(Session, pk)
return pk
def validate_cmd_filter_acl_id(self, pk):
self.cmd_filter_acl = self.validate_object(CommandFilterACL, pk)
return pk
@lazyproperty
def org(self):
return self.session.org
@staticmethod
def validate_object(model, pk):
with tmp_to_root_org():
obj = get_object_or_none(model, id=pk)
if obj:
return obj
error = '{} Model object does not exist'.format(model.__name__)
raise serializers.ValidationError(error)