From b4006d3d36355c8f7f1c3f4faffb04e0fc67626e Mon Sep 17 00:00:00 2001 From: guanghongwei Date: Thu, 29 Jan 2015 00:53:15 +0800 Subject: [PATCH] =?UTF-8?q?perm=20edit=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/urls.py | 9 +- jperm/views.py | 221 +++++++++-------- jumpserver/templatetags/mytags.py | 12 +- jumpserver/urls.py | 2 +- templates/jperm/perm_edit.html | 138 +++++++++++ templates/jperm/perm_list.html | 231 ++++++++++++++---- .../{perm_host.html => perm_list2.htl.html} | 0 templates/nav.html | 2 +- 8 files changed, 452 insertions(+), 163 deletions(-) create mode 100644 templates/jperm/perm_edit.html rename templates/jperm/{perm_host.html => perm_list2.htl.html} (100%) diff --git a/jperm/urls.py b/jperm/urls.py index 380a92a86..53ebbc758 100644 --- a/jperm/urls.py +++ b/jperm/urls.py @@ -6,12 +6,7 @@ urlpatterns = patterns('jperm.views', # url(r'^$', 'jumpserver.views.home', name='home'), # url(r'^blog/', include('blog.urls')), - (r'^perm_host/$', 'perm_host'), - (r'^perm_add/$', 'perm_add'), + (r'^perm_edit/$', 'perm_edit'), (r'^perm_user_show/$', 'perm_user_show'), - (r'^perm_host/$', 'perm_list'), - (r'^perm_user_edit/$', 'perm_user_edit'), - (r'^perm_user_detail/$', 'perm_user_detail'), - (r'^perm_group_edit/$', 'perm_group_edit'), - (r'^perm_group_detail/$', 'perm_group_detail'), + (r'^perm_list/$', 'perm_list'), ) diff --git a/jperm/views.py b/jperm/views.py index 8a6a843f6..0f0226a08 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -3,15 +3,32 @@ from django.shortcuts import render_to_response from django.http import HttpResponseRedirect, HttpResponse from juser.models import User, UserGroup -from jasset.models import Asset +from jasset.models import Asset, BisGroup from jperm.models import Perm from django.core.paginator import Paginator, EmptyPage, InvalidPage -def perm_host(request): - header_title, path1, path2 = u'主机授权 | Perm Host Detail.', u'jperm', u'perm_host' - users = contact_list = User.objects.all().order_by('id') - groups = contact_list2 = UserGroup.objects.all().order_by('id') +def perm_group_update(user_group_name='', user_group_id='', asset_groups_name='', asset_groups_id=''): + if user_group_name: + user_group = UserGroup.objects.get(name=user_group_name) + else: + user_group = UserGroup.objects.get(id=user_group_id) + + Perm.objects.filter(user_group=user_group).delete() + if asset_groups_name: + for asset_group_name in asset_groups_name: + asset_group = BisGroup.objects.get(name=asset_group_name) + Perm(user_group=user_group, asset_group=asset_group).save() + else: + for asset_group_id in asset_groups_id: + asset_group = BisGroup.objects.get(id=asset_group_id) + Perm(user_group=user_group, asset_group=asset_group).save() + + +def perm_list(request): + header_title, path1, path2 = u'主机授权 | Perm Host Detail.', u'jperm', u'perm_list' + groups = contact_list = UserGroup.objects.all().order_by('type') + users = contact_list2 = User.objects.all().order_by('id') p = paginator = Paginator(contact_list, 10) p2 = paginator2 = Paginator(contact_list2, 10) try: @@ -25,99 +42,103 @@ def perm_host(request): except (EmptyPage, InvalidPage): contacts = paginator.page(paginator.num_pages) contacts2 = paginator2.page(paginator2.num_pages) - return render_to_response('jperm/perm_host.html', locals()) - - -def perm_user_host(username, ips): - user = User.objects.get(username=username) - user.perm_set.all().delete() - for ip in ips: - asset = Asset.objects.get(ip=ip) - Perm.objects.create(user=user, asset=asset) - - -def perm_user_edit(request): - header_title, path1, path2 = u'授权编辑 | Perm Edit.', u'jperm', u'perm_edit' - if request.method == 'GET': - if request.GET.get('id', None): - user_id = request.GET.get('id') - user = User.objects.get(id=user_id) - assets = Asset.objects.all() - assets_permed = [] - for perm in user.perm_set.all(): - assets_permed.append(perm.asset) - assets_unperm = list(set(assets)-set(assets_permed)) - return render_to_response('jperm/perm_user_edit.html', locals()) - else: - host_ips = request.POST.getlist('host_permed', '') - username = request.POST.get('username') - perm_user_host(username, host_ips) - - return HttpResponseRedirect('/jperm/perm_host/') - - -def perm_user_detail(request): - user_id = request.GET.get('id', '') - user = User.objects.get(id=user_id) - host_permed = [] - for perm in user.perm_set.all(): - host_permed.append(perm.asset) - - return render_to_response('jperm/perm_user_detail.html', locals()) - - -def perm_group_edit(request): - if request.method == 'GET': - group_id = request.GET.get('id', '') - group = UserGroup.objects.get(id=group_id) - - return render_to_response('jperm/perm_group_edit.html') - - -def perm_add(request): - header_title, path1, path2 = u'添加授权 | Add User perm.', u'授权管理', u'添加授权' - if request.method == 'GET': - username = request.GET.get('username', None) - if not username: - return HttpResponseRedirect('/') - - user = User.objects.get(username=username) - permed_hosts = [] - for perm in user.perm_set.all(): - permed_hosts.append(perm.asset) - - hosts_all = Asset.objects.all() - hosts = list(set(hosts_all) - set(permed_hosts)) - - else: - username = request.POST.get('username', None) - host_ids = request.POST.getlist('host_ids', None) - - user = User.objects.get(username=username) - for host_id in host_ids: - asset = Asset.objects.get(id=host_id) - perm = Perm(user=user, asset=asset) - perm.save() - msg = u'添加成功' - - return render_to_response('jperm/perm_add.html', locals()) - - -def perm_user_show(request): - header_title, path1, path2 = u'查看授权用户 | Perm User Detail.', u'授权管理', u'用户详情' - users = User.objects.all() - return render_to_response('jperm/perm_user_show.html', locals(),) - - -def perm_list(request): - header_title, path1, path2 = u'查看用户授权 | Perm User Detail.', u'授权管理', u'用户详情' - username = request.GET.get('username', None) - if not username: - return HttpResponseRedirect('/') - - user = User.objects.get(username=username) - hosts = [] - for perm in user.perm_set.all(): - hosts.append(perm.asset) - return render_to_response('jperm/perm_list.html', locals()) + + +def perm_edit(request): + if request.method == 'GET': + header_title, path1, path2 = u'编辑授权 | Perm Host Edit.', u'jperm', u'perm_edit' + user_group_id = request.GET.get('id') + user_group = UserGroup.objects.get(id=user_group_id) + asset_groups = BisGroup.objects.all() + asset_groups_permed = [perm.asset_group for perm in user_group.perm_set.all()] + asset_groups_unperm = [asset_group for asset_group in asset_groups if asset_group not in asset_groups_permed] + return render_to_response('jperm/perm_edit.html', locals()) + else: + user_group_name = request.POST.get('user_group_name') + asset_groups_selected = request.POST.getlist('asset_group_permed') + perm_group_update(user_group_name=user_group_name, asset_groups_id=asset_groups_selected) + return HttpResponseRedirect('/jperm/perm_list/') + + +# def perm_user_host(username, ips): +# user = User.objects.get(username=username) +# user.perm_set.all().delete() +# for ip in ips: +# asset = Asset.objects.get(ip=ip) +# Perm.objects.create(user=user, asset=asset) +# +# +# def perm_user_edit(request): +# header_title, path1, path2 = u'授权编辑 | Perm Edit.', u'jperm', u'perm_edit' +# if request.method == 'GET': +# if request.GET.get('id', None): +# user_id = request.GET.get('id') +# user = User.objects.get(id=user_id) +# assets = Asset.objects.all() +# assets_permed = [] +# for perm in user.perm_set.all(): +# assets_permed.append(perm.asset) +# assets_unperm = list(set(assets)-set(assets_permed)) +# return render_to_response('jperm/perm_user_edit.html', locals()) +# else: +# host_ips = request.POST.getlist('host_permed', '') +# username = request.POST.get('username') +# perm_user_host(username, host_ips) +# +# return HttpResponseRedirect('/jperm/perm_host/') +# +# +# def perm_user_detail(request): +# user_id = request.GET.get('id', '') +# user = User.objects.get(id=user_id) +# host_permed = [] +# for perm in user.perm_set.all(): +# host_permed.append(perm.asset) +# +# return render_to_response('jperm/perm_user_detail.html', locals()) +# +# +# def perm_group_edit(request): +# if request.method == 'GET': +# group_id = request.GET.get('id', '') +# group = UserGroup.objects.get(id=group_id) +# +# return render_to_response('jperm/perm_group_edit.html') +# +# +# def perm_add(request): +# header_title, path1, path2 = u'添加授权 | Add User perm.', u'授权管理', u'添加授权' +# if request.method == 'GET': +# username = request.GET.get('username', None) +# if not username: +# return HttpResponseRedirect('/') +# +# user = User.objects.get(username=username) +# permed_hosts = [] +# for perm in user.perm_set.all(): +# permed_hosts.append(perm.asset) +# +# hosts_all = Asset.objects.all() +# hosts = list(set(hosts_all) - set(permed_hosts)) +# +# else: +# username = request.POST.get('username', None) +# host_ids = request.POST.getlist('host_ids', None) +# +# user = User.objects.get(username=username) +# for host_id in host_ids: +# asset = Asset.objects.get(id=host_id) +# perm = Perm(user=user, asset=asset) +# perm.save() +# msg = u'添加成功' +# +# return render_to_response('jperm/perm_add.html', locals()) +# +# +# def perm_user_show(request): +# header_title, path1, path2 = u'查看授权用户 | Perm User Detail.', u'授权管理', u'用户详情' +# users = User.objects.all() +# return render_to_response('jperm/perm_user_show.html', locals(),) + + diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 88e709546..7ebb5cb24 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -50,18 +50,18 @@ def bool2str(value): return u'否' -@register.filter(name='perm_count') -def perm_count(user_id): - user = User.objects.get(id=int(user_id)) - return user.perm_set.all().count() - - @register.filter(name='member_count') def member_count(group_id): group = UserGroup.objects.get(id=group_id) return group.user_set.count() +@register.filter(name='perm_count') +def perm_count(group_id): + group = UserGroup.objects.get(id=group_id) + return group.perm_set.count() + + @register.filter(name='group_type_to_str') def group_type_to_str(type_name): group_types = { diff --git a/jumpserver/urls.py b/jumpserver/urls.py index 13e669b86..187362658 100644 --- a/jumpserver/urls.py +++ b/jumpserver/urls.py @@ -5,7 +5,7 @@ urlpatterns = patterns('', # Examples: # url(r'^$', 'jumpserver.views.home', name='home'), # url(r'^blog/', include('blog.urls')), - + (r'^$', 'jumpserver.views.base'), (r'^skin_config/$', 'jumpserver.views.skin_config'), (r'^base/$', 'jumpserver.views.base'), (r'^juser/', include('juser.urls')), diff --git a/templates/jperm/perm_edit.html b/templates/jperm/perm_edit.html new file mode 100644 index 000000000..cba2d4155 --- /dev/null +++ b/templates/jperm/perm_edit.html @@ -0,0 +1,138 @@ +{% extends 'base.html' %} +{% load mytags %} + +{% block content %} + + + {% include 'nav_cat_bar.html' %} +
+
+
+
+ +
+
授权编辑表单 Edit perm of Group
+ +
+ +
+
+
+
+ + +
+
+
+
+ +
+
+
+
+ + +
+
+ +
+
+ +
+

未授权主机组

+
+ + +
+
+ +
+
+ + +
+
+ +

授权主机

+
+ +
+
+
+
+
+
+ + +
+
+
+
+ +
+
+
+
+
+ +{# #} + +{% endblock %} \ No newline at end of file diff --git a/templates/jperm/perm_list.html b/templates/jperm/perm_list.html index 6f1742bb3..fa5bc90e4 100644 --- a/templates/jperm/perm_list.html +++ b/templates/jperm/perm_list.html @@ -1,59 +1,194 @@ {% extends 'base.html' %} - +{% load mytags %} {% block content %} - {% include 'nav_cat_bar.html' %} -
-
-
-
-
-
权限主机 {{ user.username }} show perm host info.
- +{% include 'nav_cat_bar.html' %} + +
+
+
+
+
+
用户组授权 show host perm info.
+ -
-
- - - - - - - - - {% for host in hosts %} - - - - - {% endfor %} - -
IDIP
{{ host.id }}{{ host.ip }}
-
-
- - +
+ +
+
+
+ +
+ +
+ +
+
+ + + + + + + + + + + + {% for group in contacts.object_list %} + + + + + + + + {% endfor %} + +
组名类型成员数量授权数量操作
{{ group.name }} {{ group.type|group_type_to_str }} {{ group.id|member_count }} {{ group.id|perm_count }} + 详情 + 编辑 + 删除 +
+
+
+
+ Showing {{ contacts.start_index }} to {{ contacts.end_index }} of {{ p.count }} entries +
+
+
+
+
    + {% if contacts.has_previous %} + + {% else %} + + {% endif %} + {% for page in p.page_range %} + {% ifequal offset1 page %} +
  • {{ page }}
  • + {% else %} +
  • {{ page }}
  • + {% endifequal %} + {% endfor %} + {% if contacts.has_next %} + + {% else %} + + {% endif %} +
+
+
+
+
+ +
+ + + + + + + + + + {% for group in contacts2.object_list %} + + + + + + {% endfor %} + +
组名备注操作
{{ group.name }} {{ group.comment }} + 详情 + 编辑 +
+
+
+
+ Showing {{ contacts2.start_index }} to {{ contacts2.end_index }} of {{ p2.count }} entries +
+
+
+
+
    + {% if contacts2.has_previous %} + + {% else %} + + {% endif %} + {% for page in p2.page_range %} + {% ifequal offset1 page %} +
  • {{ page }}
  • + {% else %} +
  • {{ page }}
  • + {% endifequal %} + {% endfor %} + {% if contacts2.has_next %} + + {% else %} + + {% endif %} +
+
+
+
+
- + +
+
+ + +
+
+ + + {% endblock %} \ No newline at end of file diff --git a/templates/jperm/perm_host.html b/templates/jperm/perm_list2.htl.html similarity index 100% rename from templates/jperm/perm_host.html rename to templates/jperm/perm_list2.htl.html diff --git a/templates/nav.html b/templates/nav.html index 0148484e9..9203ea643 100644 --- a/templates/nav.html +++ b/templates/nav.html @@ -34,7 +34,7 @@
  • 授权管理