From 78dc6e515499df74c7278f5c20fe8f39863cdf78 Mon Sep 17 00:00:00 2001 From: halcyon <864072399@qq.com> Date: Sat, 14 Mar 2015 17:54:27 +0800 Subject: [PATCH] bugs --- docs/AddUserAsset.py | 29 ++++++++++++++++++++---- jasset/models.py | 7 +++--- jasset/views.py | 39 +++++++++++++++++++++++++------- jumpserver.conf | 2 +- templates/foot_script.html | 4 ++-- templates/jasset/group_add.html | 16 +++++++++++-- templates/jasset/group_list.html | 2 ++ templates/jasset/host_add.html | 14 ++++++++++-- templates/jasset/host_edit.html | 16 +++++++++++++ templates/jasset/host_list.html | 8 ++++--- templates/nav.html | 24 ++++++++++---------- 11 files changed, 123 insertions(+), 38 deletions(-) diff --git a/docs/AddUserAsset.py b/docs/AddUserAsset.py index e53772d83..77de5371a 100644 --- a/docs/AddUserAsset.py +++ b/docs/AddUserAsset.py @@ -12,12 +12,31 @@ django.setup() from juser.views import db_add_user, md5_crypt, CRYPTOR, db_add_group from jasset.models import Asset, IDC, BisGroup -from juser.models import UserGroup, DEPT +from juser.models import UserGroup, DEPT, User from jasset.views import jasset_group_add from jperm.models import CmdGroup from jlog.models import Log +def install(): + IDC.objects.create(name='ALL', comment='ALL') + IDC.objects.create(name='默认', comment='默认') + DEPT.objects.create(name="默认", comment="默认部门") + DEPT.objects.create(name="超管部", comment="超级管理员部门") + dept = DEPT.objects.get(name='超管部') + dept2 = DEPT.objects.get(name='默认') + UserGroup.objects.create(name='ALL', dept=dept, comment='ALL') + UserGroup.objects.create(name='默认', dept=dept, comment='默认') + + BisGroup.objects.create(name='ALL', dept=dept, comment='ALL') + BisGroup.objects.create(name='默认', dept=dept, comment='默认') + + User(id=5000, username="admin", password=md5_crypt('admin'), + name='admin', email='admin@jumpserver.org', role='SU', is_active=True, dept=dept).save() + User(id=5001, username="group_admin", password=md5_crypt('group_admin'), + name='group_admin', email='group_admin@jumpserver.org', role='DA', is_active=True, dept=dept2).save() + + def test_add_idc(): for i in range(1, 20): name = 'IDC' + str(i) @@ -67,11 +86,10 @@ def test_add_user(): def test_add_asset_group(): - BisGroup.objects.create(name='ALL', type='A', comment='ALL') - user_all = Asset.objects.all() + dept = DEPT.objects.get(name='默认') for i in range(1, 20): name = 'AssetGroup' + str(i) - group = BisGroup(name=name, type='A', comment=name) + group = BisGroup(name=name, dept=dept, comment=name) group.save() print 'Add: %s' % name @@ -80,11 +98,13 @@ def test_add_asset(): idc_all = IDC.objects.all() test_idc = random.choice(idc_all) bis_group_all = BisGroup.objects.all() + dept_all = DEPT.objects.all() for i in range(1, 500): ip = '192.168.1.' + str(i) asset = Asset(ip=ip, port=22, login_type='L', idc=test_idc, is_active=True, comment='test') asset.save() asset.bis_group = [random.choice(bis_group_all) for i in range(2)] + asset.dept = [random.choice(dept_all) for i in range(2)] print "Add: %s" % ip @@ -108,6 +128,7 @@ def test_add_log(): if __name__ == '__main__': + install() test_add_dept() test_add_group() test_add_user() diff --git a/jasset/models.py b/jasset/models.py index d0e6cd425..221c322e7 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 +from juser.models import UserGroup, DEPT class IDC(models.Model): @@ -17,6 +17,7 @@ class BisGroup(models.Model): ('A', 'ASSET'), ) name = models.CharField(max_length=80, unique=True) + dept = models.ForeignKey(DEPT) comment = models.CharField(max_length=160, blank=True, null=True) type = models.CharField(max_length=1, choices=GROUP_TYPE, default='P') @@ -27,14 +28,12 @@ class BisGroup(models.Model): class Asset(models.Model): LOGIN_TYPE_CHOICES = ( ('L', 'LDAP'), - ('S', 'SSH_KEY'), - ('P', 'PASSWORD'), ('M', 'MAP'), ) ip = models.IPAddressField(unique=True) port = models.SmallIntegerField(max_length=5) idc = models.ForeignKey(IDC) - user_group = models.ManyToManyField(UserGroup) + dept = models.ManyToManyField(DEPT) bis_group = models.ManyToManyField(BisGroup) login_type = models.CharField(max_length=1, choices=LOGIN_TYPE_CHOICES, default='L') username = models.CharField(max_length=20, blank=True, null=True) diff --git a/jasset/views.py b/jasset/views.py index 3d16b588c..e399d8067 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -6,7 +6,7 @@ from django.template import RequestContext from django.shortcuts import render_to_response from models import IDC, Asset, BisGroup -from juser.models import UserGroup +from juser.models import UserGroup, DEPT from connect import PyCrypt, KEY from jlog.models import Log from jumpserver.views import jasset_group_add, jasset_host_edit, pages @@ -19,8 +19,8 @@ def index(request): return render_to_response('jasset/jasset.html', ) -def f_add_host(ip, port, idc, jtype, group, active, comment, username='', password=''): - groups = [] +def f_add_host(ip, port, idc, jtype, group, dept, active, comment, username='', password=''): + groups, depts = [], [] idc = IDC.objects.get(name=idc) if jtype == 'M': print username, password @@ -45,7 +45,14 @@ def f_add_host(ip, port, idc, jtype, group, active, comment, username='', passwo groups.append(c) groups.extend([all_group, private_group]) + print dept + for d in dept: + print d + p = DEPT.objects.get(name=d) + depts.append(p) + a.bis_group = groups + a.dept = depts a.save() @@ -53,6 +60,7 @@ def add_host(request): login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} header_title, path1, path2 = u'添加主机', u'资产管理', u'添加主机' eidc = IDC.objects.all() + edept = DEPT.objects.all() egroup = BisGroup.objects.filter(type='A') eusergroup = UserGroup.objects.all() @@ -64,6 +72,7 @@ def add_host(request): j_group = request.POST.getlist('j_group') j_active = request.POST.get('j_active') j_comment = request.POST.get('j_comment') + j_dept = request.POST.getlist('j_dept') if Asset.objects.filter(ip=str(j_ip)): emg = u'该IP %s 已存在!' % j_ip @@ -72,9 +81,9 @@ def add_host(request): if j_type == 'M': j_user = request.POST.get('j_user') j_password = cryptor.encrypt(request.POST.get('j_password')) - f_add_host(j_ip, j_port, j_idc, j_type, j_group, j_active, j_comment, j_user, j_password) + f_add_host(j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment, j_user, j_password) else: - f_add_host(j_ip, j_port, j_idc, j_type, j_group, j_active, j_comment) + f_add_host(j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment) smg = u'主机 %s 添加成功' % j_ip return render_to_response('jasset/host_add.html', locals(), context_instance=RequestContext(request)) @@ -184,28 +193,37 @@ def host_del(request, offset): def host_edit(request): actives = {1: u'激活', 0: u'禁用'} - login_types = {'L': 'LDAP', 'S': 'SSH_KEY', 'P': 'PASSWORD', 'M': 'MAP'} + login_types = {'L': 'LDAP', 'M': 'MAP'} header_title, path1, path2 = u'修改主机', u'资产管理', u'修改主机' - groups, e_group = [], [] + groups, e_group, e_dept, depts = [], [], [], [] eidc = IDC.objects.all() egroup = BisGroup.objects.filter(type='A') + edept = DEPT.objects.all() offset = request.GET.get('id') for g in Asset.objects.get(id=int(offset)).bis_group.all(): e_group.append(g) + for d in Asset.objects.get(id=int(offset)).dept.all(): + e_dept.append(d) post = Asset.objects.get(id=int(offset)) if request.method == 'POST': j_ip = request.POST.get('j_ip') j_idc = request.POST.get('j_idc') j_port = request.POST.get('j_port') j_type = request.POST.get('j_type') + j_dept = request.POST.getlist('j_dept') j_group = request.POST.getlist('j_group') j_active = request.POST.get('j_active') j_comment = request.POST.get('j_comment') j_idc = IDC.objects.get(name=j_idc) for group in j_group: + print group c = BisGroup.objects.get(name=group) groups.append(c) + for dept in j_dept: + d = DEPT.objects.get(name=dept) + depts.append(d) + a = Asset.objects.get(id=int(offset)) if j_type == 'M': if post.password == request.POST.get('j_password'): @@ -231,6 +249,7 @@ def host_edit(request): a.save() a.bis_group = groups + a.dept = depts a.save() smg = u'主机 %s 修改成功' % j_ip return HttpResponseRedirect('/jasset/host_list') @@ -317,10 +336,13 @@ def del_idc(request, offset): def add_group(request): header_title, path1, path2 = u'添加主机组', u'资产管理', u'添加主机组' posts = Asset.objects.all() + edept = DEPT.objects.all() if request.method == 'POST': j_group = request.POST.get('j_group') + j_dept = request.POST.get('j_dept') j_hosts = request.POST.getlist('j_hosts') j_comment = request.POST.get('j_comment') + j_dept = DEPT.objects.get(name=j_dept) if BisGroup.objects.filter(name=j_group): emg = u'该主机组已存在!' @@ -328,6 +350,7 @@ def add_group(request): else: BisGroup.objects.create(name=j_group, comment=j_comment, type='A') group = BisGroup.objects.get(name=j_group) + group.dept = j_dept for host in j_hosts: g = Asset.objects.get(id=host) group.asset_set.add(g) @@ -342,7 +365,7 @@ def list_group(request): if keyword: posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)) else: - posts = BisGroup.objects.filter(type='A').order_by('id') + posts = BisGroup.objects.all().order_by('id') contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) return render_to_response('jasset/group_list.html', locals(), context_instance=RequestContext(request)) diff --git a/jumpserver.conf b/jumpserver.conf index a24c2190a..9540fa014 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -8,7 +8,7 @@ password = mysql234 database = jumpserver [ldap] -ldap_enable = 1 +ldap_enable = 0 host_url = ldap://127.0.0.1:389 base_dn = dc=jumpserver, dc=org root_dn = cn=admin,dc=jumpserver,dc=org diff --git a/templates/foot_script.html b/templates/foot_script.html index 0e3fdb657..10074eef4 100644 --- a/templates/foot_script.html +++ b/templates/foot_script.html @@ -27,7 +27,7 @@ var str = document.location.pathname.split("/")[1]; var str1 = document.location.pathname.split("/")[2]; $("#"+str).addClass('active'); - if($("#"+str1).length>0) { - $("#"+str1).addClass('active'); + if($("."+str1).length>0) { + $("."+str1).addClass('active'); } diff --git a/templates/jasset/group_add.html b/templates/jasset/group_add.html index e926af296..bc39764ea 100644 --- a/templates/jasset/group_add.html +++ b/templates/jasset/group_add.html @@ -16,13 +16,25 @@