jumpserver/apps/terminal/views/terminal.py

139 lines
4.3 KiB
Python
Raw Normal View History

2016-10-16 14:12:13 +00:00
# ~*~ coding: utf-8 ~*~
#
import time
2017-03-31 03:25:25 +00:00
from django.views.generic import ListView, UpdateView, DeleteView, \
2017-12-26 16:14:46 +00:00
DetailView, View
2016-10-16 14:12:13 +00:00
from django.utils.translation import ugettext as _
2017-12-26 16:14:46 +00:00
from django.shortcuts import redirect
2017-03-31 03:25:25 +00:00
from django.urls import reverse_lazy, reverse
2016-10-16 14:12:13 +00:00
2016-12-26 16:59:52 +00:00
from common.mixins import JSONResponseMixin
2017-12-01 13:22:32 +00:00
from ..models import Terminal
from ..forms import TerminalForm
from common.permissions import PermissionsMixin, IsSuperUser
2017-12-01 13:22:32 +00:00
__all__ = [
"TerminalListView", "TerminalUpdateView", "TerminalDetailView",
"TerminalDeleteView", "TerminalConnectView", "TerminalAcceptView",
2018-10-10 07:37:20 +00:00
"WebTerminalView", 'WebSFTPView',
2017-12-01 13:22:32 +00:00
]
2016-10-16 14:12:13 +00:00
class TerminalListView(PermissionsMixin, ListView):
2016-10-16 14:12:13 +00:00
model = Terminal
template_name = 'terminal/terminal_list.html'
2016-12-26 16:59:52 +00:00
form_class = TerminalForm
permission_classes = [IsSuperUser]
2016-10-16 14:12:13 +00:00
def get_context_data(self, **kwargs):
context = super(TerminalListView, self).get_context_data(**kwargs)
2016-12-26 16:59:52 +00:00
context.update({
'app': _('Sessions'),
2016-12-26 16:59:52 +00:00
'action': _('Terminal list'),
'form': self.form_class()
})
2016-10-16 14:12:13 +00:00
return context
2016-10-19 10:33:14 +00:00
class TerminalUpdateView(PermissionsMixin, UpdateView):
2016-10-19 10:33:14 +00:00
model = Terminal
form_class = TerminalForm
template_name = 'terminal/terminal_update.html'
success_url = reverse_lazy('terminal:terminal-list')
permission_classes = [IsSuperUser]
2016-10-19 10:33:14 +00:00
def get_context_data(self, **kwargs):
context = super(TerminalUpdateView, self).get_context_data(**kwargs)
context.update({'app': _('Sessions'), 'action': _('Update terminal')})
return context
class TerminalDetailView(PermissionsMixin, DetailView):
model = Terminal
template_name = 'terminal/terminal_detail.html'
context_object_name = 'terminal'
permission_classes = [IsSuperUser]
def get_context_data(self, **kwargs):
context = super(TerminalDetailView, self).get_context_data(**kwargs)
context.update({
'app': _('Sessions'),
'action': _('Terminal detail')
})
2016-10-19 10:33:14 +00:00
return context
2016-10-24 11:32:53 +00:00
class TerminalDeleteView(PermissionsMixin, DeleteView):
2016-10-24 11:32:53 +00:00
model = Terminal
template_name = 'delete_confirm.html'
success_url = reverse_lazy('terminal:terminal-list')
permission_classes = [IsSuperUser]
2016-12-26 16:59:52 +00:00
class TerminalAcceptView(PermissionsMixin, JSONResponseMixin, UpdateView):
2016-12-26 16:59:52 +00:00
model = Terminal
form_class = TerminalForm
template_name = 'terminal/terminal_modal_accept.html'
permission_classes = [IsSuperUser]
2016-12-26 16:59:52 +00:00
def form_valid(self, form):
terminal = form.save()
2017-12-25 04:22:49 +00:00
terminal.create_app_user()
2016-12-26 16:59:52 +00:00
terminal.is_accepted = True
2016-12-27 16:28:52 +00:00
terminal.is_active = True
2016-12-26 16:59:52 +00:00
terminal.save()
data = {
'success': True,
'msg': 'success'
}
return self.render_json_response(data)
def form_invalid(self, form):
data = {
'success': False,
2016-12-29 16:19:47 +00:00
'msg': str(form.errors),
2016-12-26 16:59:52 +00:00
}
return self.render_json_response(data)
class TerminalConnectView(PermissionsMixin, DetailView):
2018-09-29 02:44:52 +00:00
"""
Abandon
"""
2017-03-31 03:25:25 +00:00
template_name = 'flash_message_standalone.html'
model = Terminal
permission_classes = [IsSuperUser]
2017-03-31 03:25:25 +00:00
def get_context_data(self, **kwargs):
if self.object.type == 'Web':
context = {
'title': _('Redirect to web terminal'),
2017-04-07 08:59:09 +00:00
'messages': _('Redirect to web terminal') + self.object.url,
2017-03-31 03:25:25 +00:00
'auto_redirect': True,
'interval': 3,
'redirect_url': self.object.url
}
else:
context = {
'title': _('Connect ssh terminal'),
'messages': _('You should use your ssh client tools '
'connect terminal: {} <br /> <br />'
'{}'.format(self.object.name, self.object.url)),
'redirect_url': reverse('terminal:terminal-list')
2017-03-31 03:25:25 +00:00
}
kwargs.update(context)
return super(TerminalConnectView, self).get_context_data(**kwargs)
2017-12-26 16:14:46 +00:00
class WebTerminalView(View):
2017-12-26 16:14:46 +00:00
def get(self, request, *args, **kwargs):
redirect_url = '/luna/?_={}&'.format(int(time.time()))
return redirect(redirect_url + request.GET.urlencode())
2018-10-10 07:37:20 +00:00
class WebSFTPView(View):
2018-10-10 07:37:20 +00:00
def get(self, request, *args, **kwargs):
return redirect('/koko/elfinder/sftp/?' + request.GET.urlencode())