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
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.serializers.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):
|
2022-12-04 12:51:22 +00:00
|
|
|
type = LabeledChoiceField(
|
2022-12-04 10:48:48 +00:00
|
|
|
choices=CommandGroup.TypeChoices.choices, default=CommandGroup.TypeChoices.command,
|
|
|
|
label=_('Type')
|
|
|
|
)
|
|
|
|
|
2022-12-02 03:53:07 +00:00
|
|
|
class Meta:
|
|
|
|
model = CommandGroup
|
2022-12-04 06:44:30 +00:00
|
|
|
fields = ['id', 'name', 'type', 'content', 'ignore_case', 'comment']
|
2022-12-02 03:53:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CommandFilterACLSerializer(BaseSerializer, BulkOrgResourceModelSerializer):
|
2022-12-04 06:44:30 +00:00
|
|
|
command_groups = ObjectRelatedField(
|
2022-12-04 10:48:48 +00:00
|
|
|
queryset=CommandGroup.objects, many=True, required=False, label=_('Command group')
|
2022-12-04 06:44:30 +00:00
|
|
|
)
|
2023-01-16 11:02:09 +00:00
|
|
|
command_groups_amount = serializers.IntegerField(
|
|
|
|
source='command_groups.count', read_only=True, label=_('Command group amount')
|
|
|
|
)
|
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
|
2023-01-16 11:02:09 +00:00
|
|
|
fields = BaseSerializer.Meta.fields + ['command_groups', 'command_groups_amount']
|
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)
|