2018-11-23 02:25:35 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
import logging
|
2023-02-20 05:36:50 +00:00
|
|
|
|
2021-12-29 12:07:56 +00:00
|
|
|
from rest_framework import status
|
2023-08-01 08:40:38 +00:00
|
|
|
from rest_framework.decorators import action
|
2021-12-29 12:07:56 +00:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-02-20 05:36:50 +00:00
|
|
|
from rest_framework.views import APIView, Response
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2023-02-20 05:36:50 +00:00
|
|
|
from common.api import JMSBulkModelViewSet
|
2023-08-01 08:40:38 +00:00
|
|
|
from common.const.http import POST
|
2018-11-23 02:25:35 +00:00
|
|
|
from common.utils import get_object_or_none
|
2021-12-29 12:07:56 +00:00
|
|
|
from orgs.utils import tmp_to_root_org
|
2022-10-25 04:57:34 +00:00
|
|
|
from terminal import serializers
|
2023-08-01 08:40:38 +00:00
|
|
|
from terminal.const import TaskNameType
|
2023-02-20 05:36:50 +00:00
|
|
|
from terminal.models import Session, Task
|
2022-10-25 04:57:34 +00:00
|
|
|
from terminal.utils import is_session_approver
|
2018-11-23 02:25:35 +00:00
|
|
|
|
2023-08-01 08:40:38 +00:00
|
|
|
__all__ = ['TaskViewSet', 'KillSessionAPI', 'KillSessionForTicketAPI', ]
|
2018-11-23 02:25:35 +00:00
|
|
|
logger = logging.getLogger(__file__)
|
|
|
|
|
|
|
|
|
2023-02-20 05:36:50 +00:00
|
|
|
class TaskViewSet(JMSBulkModelViewSet):
|
2018-11-23 02:25:35 +00:00
|
|
|
queryset = Task.objects.all()
|
|
|
|
serializer_class = serializers.TaskSerializer
|
2021-10-22 06:27:54 +00:00
|
|
|
filterset_fields = ('is_finished',)
|
2023-08-01 08:40:38 +00:00
|
|
|
serializer_classes = {
|
|
|
|
'create_toggle_task': serializers.LockTaskSessionSerializer,
|
|
|
|
'handle_ticket_task': serializers.LockTaskSessionSerializer,
|
|
|
|
'default': serializers.TaskSerializer
|
|
|
|
}
|
|
|
|
rbac_perms = {
|
|
|
|
"create_toggle_task": "terminal.terminate_session",
|
|
|
|
}
|
|
|
|
|
|
|
|
@action(methods=[POST], detail=False, url_path='toggle-lock-session')
|
|
|
|
def create_toggle_task(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
session_id = serializer.validated_data['session_id']
|
|
|
|
task_name = serializer.validated_data['task_name']
|
|
|
|
session_ids = [session_id, ]
|
|
|
|
validated_session = create_sessions_tasks(session_ids, request.user, task_name=task_name)
|
|
|
|
return Response({"ok": validated_session})
|
|
|
|
|
|
|
|
@action(methods=[POST], detail=False, permission_classes=[IsAuthenticated, ],
|
|
|
|
url_path='toggle-lock-session-for-ticket', )
|
|
|
|
def handle_ticket_task(self, request, *args, **kwargs):
|
|
|
|
serializer = self.get_serializer(data=request.data)
|
|
|
|
serializer.is_valid(raise_exception=True)
|
|
|
|
session_id = serializer.validated_data['session_id']
|
|
|
|
task_name = serializer.validated_data['task_name']
|
|
|
|
user_id = request.user.id
|
|
|
|
|
2023-10-07 06:47:17 +00:00
|
|
|
if not is_session_approver(session_id, user_id):
|
|
|
|
return Response({}, status=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
|
|
|
with tmp_to_root_org():
|
|
|
|
validated_session = create_sessions_tasks([session_id], request.user, task_name=task_name)
|
2023-08-01 08:40:38 +00:00
|
|
|
return Response({"ok": validated_session})
|
2018-11-23 02:25:35 +00:00
|
|
|
|
|
|
|
|
2023-08-01 08:40:38 +00:00
|
|
|
def create_sessions_tasks(session_ids, user, task_name=TaskNameType.kill_session):
|
2021-12-29 12:07:56 +00:00
|
|
|
validated_session = []
|
|
|
|
|
|
|
|
for session_id in session_ids:
|
|
|
|
session = get_object_or_none(Session, id=session_id)
|
|
|
|
if session and not session.is_finished:
|
|
|
|
validated_session.append(session_id)
|
|
|
|
Task.objects.create(
|
2023-08-01 08:40:38 +00:00
|
|
|
name=task_name, args=session.id, terminal=session.terminal,
|
2021-12-29 12:07:56 +00:00
|
|
|
kwargs={
|
2023-08-01 08:40:38 +00:00
|
|
|
'terminated_by': str(user),
|
|
|
|
'created_by': str(user)
|
2021-12-29 12:07:56 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
return validated_session
|
|
|
|
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
class KillSessionAPI(APIView):
|
2022-02-17 12:13:31 +00:00
|
|
|
model = Task
|
|
|
|
rbac_perms = {
|
|
|
|
'POST': 'terminal.terminate_session'
|
|
|
|
}
|
2021-12-29 12:07:56 +00:00
|
|
|
|
2022-02-23 09:09:09 +00:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
session_ids = request.data
|
2023-08-01 08:40:38 +00:00
|
|
|
validated_session = create_sessions_tasks(session_ids, request.user)
|
2022-02-23 09:09:09 +00:00
|
|
|
return Response({"ok": validated_session})
|
|
|
|
|
2021-12-29 12:07:56 +00:00
|
|
|
|
|
|
|
class KillSessionForTicketAPI(APIView):
|
2023-02-20 05:36:50 +00:00
|
|
|
permission_classes = (IsAuthenticated,)
|
2021-12-29 12:07:56 +00:00
|
|
|
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
session_ids = request.data
|
|
|
|
user_id = request.user.id
|
|
|
|
|
|
|
|
for session_id in session_ids:
|
|
|
|
if not is_session_approver(session_id, user_id):
|
|
|
|
return Response({}, status=status.HTTP_403_FORBIDDEN)
|
|
|
|
|
|
|
|
with tmp_to_root_org():
|
2023-08-01 08:40:38 +00:00
|
|
|
validated_session = create_sessions_tasks(session_ids, request.user)
|
2021-12-29 12:07:56 +00:00
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
return Response({"ok": validated_session})
|