2018-12-10 02:11:54 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
from rest_framework import viewsets
|
2019-02-13 07:55:11 +00:00
|
|
|
from django.db import transaction
|
2019-06-11 05:30:45 +00:00
|
|
|
from django.conf import settings
|
2018-12-10 02:11:54 +00:00
|
|
|
|
|
|
|
from common.permissions import IsValidUser
|
|
|
|
from ..models import CommandExecution
|
|
|
|
from ..serializers import CommandExecutionSerializer
|
|
|
|
from ..tasks import run_command_execution
|
|
|
|
|
|
|
|
|
|
|
|
class CommandExecutionViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = CommandExecutionSerializer
|
|
|
|
permission_classes = (IsValidUser,)
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
return CommandExecution.objects.filter(
|
|
|
|
user_id=str(self.request.user.id)
|
|
|
|
)
|
|
|
|
|
2019-06-11 05:30:45 +00:00
|
|
|
def check_permissions(self, request):
|
2019-07-01 03:22:05 +00:00
|
|
|
if not settings.SECURITY_COMMAND_EXECUTION and request.user.is_common_user:
|
2019-06-11 05:30:45 +00:00
|
|
|
return self.permission_denied(request, "Command execution disabled")
|
|
|
|
return super().check_permissions(request)
|
|
|
|
|
2018-12-10 02:11:54 +00:00
|
|
|
def perform_create(self, serializer):
|
|
|
|
instance = serializer.save()
|
|
|
|
instance.user = self.request.user
|
|
|
|
instance.save()
|
2019-02-13 07:55:11 +00:00
|
|
|
transaction.on_commit(lambda: run_command_execution.apply_async(
|
2018-12-10 02:11:54 +00:00
|
|
|
args=(instance.id,), task_id=str(instance.id)
|
2019-02-13 07:55:11 +00:00
|
|
|
))
|