mirror of
https://github.com/jumpserver/jumpserver.git
synced 2025-10-22 16:31:33 +00:00
feat: 资产登录工单页面增加监控与中断
This commit is contained in:
@@ -3,14 +3,18 @@
|
||||
import logging
|
||||
from rest_framework.views import APIView, Response
|
||||
from rest_framework_bulk import BulkModelViewSet
|
||||
from rest_framework import status
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
|
||||
from common.utils import get_object_or_none
|
||||
from common.permissions import IsOrgAdminOrAppUser
|
||||
from ..models import Session, Task
|
||||
from .. import serializers
|
||||
from terminal.utils import is_session_approver
|
||||
from orgs.utils import tmp_to_root_org
|
||||
|
||||
|
||||
__all__ = ['TaskViewSet', 'KillSessionAPI']
|
||||
__all__ = ['TaskViewSet', 'KillSessionAPI', 'KillSessionForTicketAPI']
|
||||
logger = logging.getLogger(__file__)
|
||||
|
||||
|
||||
@@ -21,20 +25,45 @@ class TaskViewSet(BulkModelViewSet):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
|
||||
|
||||
def kill_sessions(session_ids, user):
|
||||
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(
|
||||
name="kill_session", args=session.id, terminal=session.terminal,
|
||||
kwargs={
|
||||
'terminated_by': str(user)
|
||||
}
|
||||
)
|
||||
return validated_session
|
||||
|
||||
|
||||
class KillSessionAPI(APIView):
|
||||
permission_classes = (IsOrgAdminOrAppUser,)
|
||||
model = Task
|
||||
|
||||
def post(self, request, *args, **kwargs):
|
||||
validated_session = []
|
||||
for session_id in request.data:
|
||||
session = get_object_or_none(Session, id=session_id)
|
||||
if session and not session.is_finished:
|
||||
validated_session.append(session_id)
|
||||
self.model.objects.create(
|
||||
name="kill_session", args=session.id, terminal=session.terminal,
|
||||
kwargs={
|
||||
'terminated_by': str(request.user)
|
||||
}
|
||||
)
|
||||
session_ids = request.data
|
||||
user = request.user
|
||||
validated_session = kill_sessions(session_ids, user)
|
||||
return Response({"ok": validated_session})
|
||||
|
||||
|
||||
class KillSessionForTicketAPI(APIView):
|
||||
permission_classes = (IsAuthenticated, )
|
||||
|
||||
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():
|
||||
validated_session = kill_sessions(session_ids, request.user)
|
||||
|
||||
return Response({"ok": validated_session})
|
||||
|
||||
|
Reference in New Issue
Block a user