From ef63fce7c46c69cc880b569c350f8aede8d2feea Mon Sep 17 00:00:00 2001 From: halcyon <864072399@qq.com> Date: Tue, 7 Apr 2015 19:15:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug=EF=BC=8C=E5=AE=9E?= =?UTF-8?q?=E6=97=B6=E6=90=9C=E7=B4=A2=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jasset/models.py | 13 +- jasset/urls.py | 1 + jasset/views.py | 95 ++++++++++--- jumpserver.conf | 2 +- jumpserver/templatetags/mytags.py | 26 ++-- jumpserver/views.py | 1 - templates/index.html | 25 ++-- templates/jasset/group_add.html | 55 +++++--- templates/jasset/host_list_common.html | 172 ++++++++++++++++++++++++ templates/jasset/host_list_nop.html | 177 +++++++++++++++++++++++++ templates/jasset/host_search.html | 18 ++- templates/jperm/perm_apply.html | 23 ++-- templates/jperm/perm_log_offline.html | 6 +- templates/jperm/perm_log_online.html | 4 +- templates/jperm/sudo_list.html | 4 +- 15 files changed, 532 insertions(+), 90 deletions(-) create mode 100644 templates/jasset/host_list_common.html create mode 100644 templates/jasset/host_list_nop.html diff --git a/jasset/models.py b/jasset/models.py index 5c2be96b2..ab65792da 100644 --- a/jasset/models.py +++ b/jasset/models.py @@ -1,6 +1,6 @@ import datetime from django.db import models -from juser.models import UserGroup, DEPT +from juser.models import User, UserGroup, DEPT class IDC(models.Model): @@ -43,4 +43,13 @@ class Asset(models.Model): comment = models.CharField(max_length=100, blank=True, null=True) def __unicode__(self): - return self.ip \ No newline at end of file + return self.ip + + +class AssetAlias(models.Model): + user = models.ForeignKey(User) + host = models.ForeignKey(Asset) + alias = models.CharField(max_length=100, blank=True, null=True) + + def __unicode__(self): + return self.comment \ No newline at end of file diff --git a/jasset/urls.py b/jasset/urls.py index cf78be50e..1002ee691 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -22,4 +22,5 @@ urlpatterns = patterns('', url(r'^host_del/(\w+)/$', host_del), url(r'^host_edit/$', host_edit), url(r'^host_edit/batch/$', batch_host_edit), + url(r'^host_edit_common/batch/$', batch_host_edit_common), ) \ No newline at end of file diff --git a/jasset/views.py b/jasset/views.py index ea5e32fec..913ef5927 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -6,9 +6,9 @@ from django.db.models import Q from django.template import RequestContext from django.shortcuts import render_to_response -from jasset.models import IDC, Asset, BisGroup +from jasset.models import IDC, Asset, BisGroup, AssetAlias from juser.models import UserGroup, DEPT -from jperm.models import Perm +from jperm.models import Perm, SudoPerm from jumpserver.views import pages from jumpserver.api import * @@ -88,6 +88,7 @@ def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_activ a.dept = depts a.save() + @require_admin def add_host(request): login_types = {'L': 'LDAP', 'M': 'MAP'} @@ -189,12 +190,6 @@ def batch_host_edit(request): j_group = request.POST.getlist(j_group) j_active = request.POST.get(j_active).strip() j_comment = request.POST.get(j_comment).strip() - print j_dept, j_group - # - # if is_group_admin(request) and not validate(request, asset=[j_id]): - # emg = u'删除失败,您无权操作!' - # print 'hehe' - # return HttpResponseRedirect('/jasset/host_list/') if j_type == 'M': j_user = "editable[" + str(i) + "][j_user]" @@ -202,13 +197,36 @@ def batch_host_edit(request): j_user = request.POST.get(j_user).strip() password = request.POST.get(j_password).strip() j_password = cryptor.encrypt(password) - jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment, j_user, j_password) + jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment, j_user, + j_password) else: jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment) return render_to_response('jasset/host_list.html') +@require_login +def batch_host_edit_common(request): + user_id = request.session.get('user_id', '') + u = User.objects.get(id=user_id) + if request.method == 'POST': + len_table = request.POST.get('len_table') + for i in range(int(len_table)): + j_id = "editable[" + str(i) + "][j_id]" + j_alias = "editable[" + str(i) + "][j_alias]" + j_id = request.POST.get(j_id).strip() + j_alias = request.POST.get(j_alias).strip() + a = Asset.objects.get(id=j_id) + asset_alias = AssetAlias.objects.filter(user=u, host=a) + if asset_alias: + asset_alias = asset_alias[0] + asset_alias.alias = j_alias + asset_alias.save() + else: + AssetAlias.objects.create(user=u, host=a, alias=j_alias) + return render_to_response('jasset/host_list_common.html') + + @require_login def list_host(request): header_title, path1, path2 = u'查看主机', u'资产管理', u'查看主机' @@ -222,6 +240,8 @@ def list_host(request): if did: dept = DEPT.objects.get(id=did) posts = dept.asset_set.all() + return render_to_response('jasset/host_list_nop.html', locals(), context_instance=RequestContext(request)) + elif gid: posts = [] user_group = UserGroup.objects.get(id=gid) @@ -230,32 +250,47 @@ def list_host(request): for post in perm.asset_group.asset_set.all(): posts.append(post) posts = list(set(posts)) + return render_to_response('jasset/host_list_nop.html', locals(), context_instance=RequestContext(request)) + elif sid: - pass + posts = [] + user_group = UserGroup.objects.get(id=sid) + perms = Perm.objects.filter(user_group=user_group) + for perm in perms: + for post in perm.asset_group.asset_set.all(): + posts.append(post) + posts = list(set(posts)) + return render_to_response('jasset/host_list_nop.html', locals(), context_instance=RequestContext(request)) else: if is_super_user(request): if keyword: posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | - Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).distinct().order_by('ip') + Q(bis_group__name__contains=keyword) | Q( + comment__contains=keyword)).distinct().order_by('ip') else: posts = Asset.objects.all().order_by('ip') + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + return render_to_response('jasset/host_list.html', locals(), context_instance=RequestContext(request)) elif is_group_admin(request): if keyword: posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | - Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip') + Q(bis_group__name__contains=keyword) | Q( + comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip') else: posts = Asset.objects.all().filter(dept=dept).order_by('ip') + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + return render_to_response('jasset/host_list.html', locals(), context_instance=RequestContext(request)) + elif is_common_user(request): user_id = request.session.get('user_id') username = User.objects.get(id=user_id).name posts = user_perm_asset_api(username) - - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - - return render_to_response('jasset/host_list.html', locals(), context_instance=RequestContext(request)) + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + return render_to_response('jasset/host_list_common.html', locals(), + context_instance=RequestContext(request)) @require_admin @@ -478,21 +513,32 @@ def list_group(request): dept = DEPT.objects.get(id=dept_id) keyword = request.GET.get('keyword', '') gid = request.GET.get('gid') + sid = request.GET.get('sid') if gid: posts = [] user_group = UserGroup.objects.get(id=gid) perms = Perm.objects.filter(user_group=user_group) for perm in perms: posts.append(perm.asset_group) + + elif sid: + posts = [] + user_group = UserGroup.objects.get(id=sid) + perms = Perm.objects.filter(user_group=user_group) + for perm in perms: + posts.append(perm.asset_group) + else: if is_super_user(request): if keyword: - posts = BisGroup.objects.exclude(name='ALL').filter(Q(name__contains=keyword) | Q(comment__contains=keyword)) + posts = BisGroup.objects.exclude(name='ALL').filter( + Q(name__contains=keyword) | Q(comment__contains=keyword)) else: posts = BisGroup.objects.exclude(name='ALL').order_by('id') elif is_group_admin(request): if keyword: - posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept) + posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter( + dept=dept) else: posts = BisGroup.objects.filter(dept=dept).order_by('id') contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) @@ -506,7 +552,7 @@ def edit_group(request): group = BisGroup.objects.get(id=group_id) all = Asset.objects.all() dept_id = get_user_dept(request) - eposts = contact_list = Asset.objects.filter(bis_group=group).order_by('ip') + eposts = Asset.objects.filter(bis_group=group).order_by('ip') if is_super_user(request): edept = DEPT.objects.all() @@ -552,6 +598,7 @@ def detail_group(request): return render_to_response('jasset/group_detail.html', locals(), context_instance=RequestContext(request)) +@require_admin def detail_idc(request): header_title, path1, path2 = u'IDC详情', u'资产管理', u'IDC详情' login_types = {'L': 'LDAP', 'M': 'MAP'} @@ -605,6 +652,7 @@ def group_del(request, offset): return HttpResponseRedirect('/jasset/jgroup_list/') +@require_login def host_search(request): keyword = request.GET.get('keyword') login_types = {'L': 'LDAP', 'M': 'MAP'} @@ -618,6 +666,15 @@ def host_search(request): posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | Q(bis_group__name__contains=keyword) | Q( comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip') + elif is_common_user(request): + user_id = request.session.get('user_id') + username = User.objects.get(id=user_id).name + post_perm = user_perm_asset_api(username) + post_all = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | + Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)) \ + .distinct().order_by('ip') + posts = list(set(post_all) & set(post_perm)) + print posts contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) return render_to_response('jasset/host_search.html', locals(), context_instance=RequestContext(request)) \ No newline at end of file diff --git a/jumpserver.conf b/jumpserver.conf index 148d51eb7..78b98001d 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -9,7 +9,7 @@ database = jumpserver [ldap] ldap_enable = 1 -host_url = ldap://192.168.173.129:389 +host_url = ldap://192.168.8.230:389 base_dn = dc=jumpserver, dc=org root_dn = cn=admin,dc=jumpserver,dc=org root_pw = secret234 diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 7b61693ea..159bbd5e3 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -7,6 +7,7 @@ import time from django import template from juser.models import User, UserGroup, DEPT from jumpserver.api import * +from jasset.models import AssetAlias register = template.Library() @@ -162,6 +163,17 @@ def ugrp_perm_asset_count(user_group_id): return len(set(assets)) +@register.filter(name='get_user_alias') +def get_user_alias(post, user_id): + user = User.objects.get(id=user_id) + host = Asset.objects.get(id=post.id) + alias = AssetAlias.objects.filter(user=user, host=host) + if alias: + return alias[0].alias + else: + return '' + + @register.filter(name='group_type_to_str') def group_type_to_str(type_name): group_types = { @@ -187,25 +199,11 @@ def ast_to_list_1(lis): return ast.literal_eval(lis) -# @register.filter(name='perm_asset_count') -# def perm_asset_count(user_id): -# return len(perm_user_asset(user_id)) - @register.filter(name='string_length') def string_length(string, length): return '%s ...' % string[0:length] -@register.filter(name='get_dic_user') -def get_dic_user(dic): - return dic.get('user') - - -@register.filter(name='get_dic_times') -def get_dic_times(dic): - return dic.get('times') - - @register.filter(name='to_name') def to_name(user_id): try: diff --git a/jumpserver/views.py b/jumpserver/views.py index 8cd1f76c9..3dc7b7e77 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -83,7 +83,6 @@ def index(request): login_10 = Log.objects.order_by('-start_time')[:10] # a week top 10 - # user_top_ten_more = [] for user_info in user_top_ten: username = user_info.get('user') last = Log.objects.filter(user=username).latest('start_time') diff --git a/templates/index.html b/templates/index.html index aa62330bf..c7b5829fe 100644 --- a/templates/index.html +++ b/templates/index.html @@ -69,9 +69,9 @@ {% for data in user_top_five %}
最近一次登录
diff --git a/templates/jasset/group_add.html b/templates/jasset/group_add.html index d3defd781..b7c42d7cb 100644 --- a/templates/jasset/group_add.html +++ b/templates/jasset/group_add.html @@ -45,28 +45,36 @@