资产列表添加连接

pull/26/head
ibuler 9 years ago
parent f00a2e003d
commit 483ca9677c

@ -9,4 +9,5 @@ urlpatterns = patterns('',
url(r'^log_kill/', log_kill), url(r'^log_kill/', log_kill),
url(r'^record/$', log_record), url(r'^record/$', log_record),
url(r'^web_terminal/$', web_terminal), url(r'^web_terminal/$', web_terminal),
url(r'^get_role_name/$', get_role_name),
) )

@ -4,6 +4,7 @@ from django.template import RequestContext
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from jumpserver.api import * from jumpserver.api import *
from jperm.perm_api import user_have_perm
from django.http import HttpResponseNotFound from django.http import HttpResponseNotFound
from jlog.log_api import renderTemplate from jlog.log_api import renderTemplate
@ -103,8 +104,18 @@ def log_record(request):
return HttpResponse('无日志记录!') return HttpResponse('无日志记录!')
def get_role_name(request):
asset_id = request.GET.get('id', 9999)
asset = get_object(Asset, id=asset_id)
if asset:
role = user_have_perm(request.user, asset=asset)
return HttpResponse(','.join(list(role)))
return HttpResponse('dev,sa')
@require_role()
def web_terminal(request): def web_terminal(request):
asset_id = 15 asset_id = request.GET.get('id')
web_terminal_uri = 'ws://%s/terminal?asset_id=%s' % (WEB_SOCKET_HOST, asset_id) web_terminal_uri = 'ws://%s/terminal?id=%s&role=dev' % (WEB_SOCKET_HOST, asset_id)
return render_to_response('jlog/web_terminal.html', locals()) return render_to_response('jlog/web_terminal.html', locals())

@ -237,3 +237,12 @@ def key_exist(username):
return True return True
else: else:
return False return False
@register.filter(name='check_role')
def check_role(asset_id, user):
"""
ssh key is exist or not
"""
return user

@ -56,7 +56,7 @@ def require_auth(func):
session = session[0] session = session[0]
uid = session.get_decoded().get('_auth_user_id') uid = session.get_decoded().get('_auth_user_id')
user = User.objects.filter(id=uid) user = User.objects.filter(id=uid)
asset_id = request.get_argument('asset_id', 9999) asset_id = int(request.get_argument('id', 9999))
asset = Asset.objects.filter(id=asset_id) asset = Asset.objects.filter(id=asset_id)
if asset: if asset:
@ -240,10 +240,19 @@ class WebTerminalHandler(tornado.websocket.WebSocketHandler):
@require_auth @require_auth
def open(self): def open(self):
print self.user, self.asset print self.user, self.asset
user = User.objects.get(username='lastimac') role_name = self.get_argument('role', 'root')
asset = Asset.objects.get(ip='192.168.244.129') roles = user_have_perm(self.user, self.asset)
role = PermRole.objects.get(name='dev') login_role = ''
self.term = WebTty(user, asset, role) for role in roles:
if role.name == role_name:
login_role = role
break
if not login_role:
print "no role"
self.close()
return
# Todo: 判断
self.term = WebTty(self.user, self.asset, login_role)
self.term.get_connection() self.term.get_connection()
self.term.channel = self.term.ssh.invoke_shell(term='xterm') self.term.channel = self.term.ssh.invoke_shell(term='xterm')
WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound)) WebTerminalHandler.tasks.append(MyThread(target=self.forward_outbound))

@ -130,6 +130,7 @@
<a href="/jasset/asset_detail/?id={{ asset.id }}" class="btn btn-xs btn-primary">详情</a> <a href="/jasset/asset_detail/?id={{ asset.id }}" class="btn btn-xs btn-primary">详情</a>
{% ifnotequal session_role_id 0 %} {% ifnotequal session_role_id 0 %}
<a href="/jasset/asset_edit/?id={{ asset.id }}" class="btn btn-xs btn-info">编辑</a> <a href="/jasset/asset_edit/?id={{ asset.id }}" class="btn btn-xs btn-info">编辑</a>
<a value="{{ asset.id }}" class="conn btn btn-xs btn-warning">连接</a>
<a value="/jasset/asset_del/?id={{ asset.id }}" class="btn btn-xs btn-danger asset_del">删除</a> <a value="/jasset/asset_del/?id={{ asset.id }}" class="btn btn-xs btn-danger asset_del">删除</a>
{% endifnotequal %} {% endifnotequal %}
</td> </td>
@ -167,9 +168,46 @@
} }
) )
} }
});
$('.conn').click(function(evt){
var url='/jlog/get_role_name/?id' + $(this).attr('value');
var href = $(this).attr('href');
var new_url = '/jlog/web_terminal/?id=' + $(this).attr('value') + '&role=';
$.ajax({
type: 'GET',
url: url,
data: {},
success: function(data){
var dataArray = data.split(',');
if (dataArray.length == 1){
window.open(new_url + data, '播放', 'height=400, width=600, top=89px, left=99px,toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
} else if (dataArray.length == 0){
layer.alert('没有授权角色')
} else {
aUrl = '';
$.each(dataArray, function(index, value){
aUrl += '<a onclick="windowOpen(this); return false" class="btn btn-xs btn-primary newa" href=' + new_url + value + '>' + value + '</a> '
});
layer.alert(aUrl, {
skin: 'layui-layer-molv',
title: '多个角色,请选择一个连接',
closeBtn: 0
}) })
}
}
});
return false
});
}); });
function windowOpen(aTab){
var new_url = aTab.href;
window.open(new_url, '播放', 'height=400, width=600, top=89px, left=99px,toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
return false
}
$(".iframe").on('click', function(){ $(".iframe").on('click', function(){
var asset_id_all = getIDall(); var asset_id_all = getIDall();
if (asset_id_all == ''){ if (asset_id_all == ''){
@ -206,6 +244,8 @@
}); });
}); });
$('#asset_del').click(function () { $('#asset_del').click(function () {
var asset_id_all = getIDall(); var asset_id_all = getIDall();
if (asset_id_all == ''){ if (asset_id_all == ''){

Loading…
Cancel
Save