From a94c050fd944c15d663fec65909ad2ebdb9bfd08 Mon Sep 17 00:00:00 2001 From: guanghongwei Date: Sat, 7 Mar 2015 00:12:38 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=B7=A5=EF=BC=8C=E7=9D=A1=E8=A7=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/views.py | 118 ++++++++++++------------ jumpserver/api.py | 36 ++++++++ jumpserver/templatetags/mytags.py | 8 ++ juser/views.py | 2 + templates/jasset/jlist_ip.html | 6 ++ templates/jperm/perm_detail.html | 6 +- templates/jperm/perm_edit.html | 24 ++--- templates/jperm/perm_list.html | 143 ++++++++++++++---------------- templates/juser/user_detail.html | 8 ++ templates/juser/user_list.html | 2 +- 10 files changed, 203 insertions(+), 150 deletions(-) create mode 100644 jumpserver/api.py diff --git a/jperm/views.py b/jperm/views.py index 959b9e318..b1b14b737 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -1,7 +1,7 @@ # coding: utf-8 from django.shortcuts import render_to_response -from django.http import HttpResponseRedirect +from django.http import HttpResponseRedirect, HttpResponse from django.template import RequestContext from juser.models import User, UserGroup from jasset.models import Asset, BisGroup @@ -18,23 +18,6 @@ if LDAP_ENABLE: LDAP_ROOT_PW = CONF.get('ldap', 'root_pw') -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 user_asset_cmd_groups_get(user_groups_select='', asset_groups_select='', cmd_groups_select=''): user_groups_select_list = [] asset_groups_select_list = [] @@ -78,8 +61,11 @@ def perm_add(request): def perm_list(request): header_title, path1, path2 = u'主机授权', u'授权管理', u'授权详情' - contact_list = Perm.objects.all() - contact_list2 = User.objects.all().order_by('id') + keyword = request.GET.get('search', '') + if keyword: + contact_list = Perm.objects.filter(name__icontains=keyword) + else: + contact_list = Perm.objects.all() p = paginator = Paginator(contact_list, 10) try: current_page = int(request.GET.get('page', '1')) @@ -95,45 +81,63 @@ def perm_list(request): return render_to_response('jperm/perm_list.html', locals(), context_instance=RequestContext(request)) -def perm_list_ajax(request): - tab = request.POST.get('tab', 'tab1') - search = request.POST.get('search', '') +# def perm_list_ajax(request): +# tab = request.POST.get('tab', 'tab1') +# search = request.POST.get('search', '') +# +# if tab == 'tab1': +# groups = contact_list = UserGroup.objects.filter(name__icontains=search).order_by('type') +# p = paginator = Paginator(contact_list, 10) +# +# try: +# page = int(request.GET.get('page', '1')) +# except ValueError: +# page = 1 +# +# try: +# contacts = paginator.page(page) +# except (EmptyPage, InvalidPage): +# contacts = paginator.page(paginator.num_pages) +# +# else: +# users = contact_list2 = User.objects.filter(name__icontains=search).order_by('id') +# p2 = paginator2 = Paginator(contact_list2, 10) +# +# try: +# page = int(request.GET.get('page', '1')) +# except ValueError: +# page = 1 +# +# try: +# contacts2 = paginator2.page(page) +# except (EmptyPage, InvalidPage): +# contacts2 = paginator2.page(paginator2.num_pages) +# +# return render_to_response('jperm/perm_list_ajax.html', locals()) - if tab == 'tab1': - groups = contact_list = UserGroup.objects.filter(name__icontains=search).order_by('type') - p = paginator = Paginator(contact_list, 10) +def perm_group_update(perm_id, user_group_id_list, asset_groups_id_list): + perm = Perm.objects.filter(id=perm_id) + if perm: + perm = perm[0] + user_group_list = [] + asset_group_list = [] - try: - page = int(request.GET.get('page', '1')) - except ValueError: - page = 1 + for user_group_id in user_group_id_list: + user_group_list.extend(UserGroup.objects.filter(id=user_group_id)) - try: - contacts = paginator.page(page) - except (EmptyPage, InvalidPage): - contacts = paginator.page(paginator.num_pages) + for asset_group_id in asset_groups_id_list: + asset_group_list.extend(BisGroup.objects.filter(id=asset_group_id)) - else: - users = contact_list2 = User.objects.filter(name__icontains=search).order_by('id') - p2 = paginator2 = Paginator(contact_list2, 10) - - try: - page = int(request.GET.get('page', '1')) - except ValueError: - page = 1 - - try: - contacts2 = paginator2.page(page) - except (EmptyPage, InvalidPage): - contacts2 = paginator2.page(paginator2.num_pages) - - return render_to_response('jperm/perm_list_ajax.html', locals()) + perm.user_group.clear() + perm.asset_group.clear() + perm.user_group = user_group_list + perm.asset_group = asset_group_list def perm_edit(request): if request.method == 'GET': header_title, path1, path2 = u'编辑授权', u'授权管理', u'授权编辑' - perm_id = request.GET.get('id') + perm_id = request.GET.get('id', '') perm = Perm.objects.filter(id=perm_id) if perm: perm = perm[0] @@ -142,16 +146,20 @@ def perm_edit(request): user_groups_select = perm.user_group.all() asset_groups_select = perm.asset_group.all() - user_groups_all = UserGroup.objects.filter(Q(type='A') | Q(type='M')) + user_groups_all = UserGroup.objects.all() asset_groups_all = BisGroup.objects.all() user_groups = [user_group for user_group in user_groups_all if user_group not in user_groups_select] asset_groups = [asset_group for asset_group in asset_groups_all if asset_group not in asset_groups_select] 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/', locals(), context_instance=RequestContext(request)) + perm_id = request.POST.get('perm_id', '') + user_group_id_list = request.POST.getlist('user_groups_select') + asset_group_id_list = request.POST.getlist('asset_groups_select') + # return HttpResponse("perm_id: %s user_group: %s asset_group: %s" % (perm_id, repr(user_group_id_list), repr(asset_group_id_list) )) + # return HttpResponse(perm_group_update(perm_id, user_group_id_list, asset_group_id_list)) + perm_group_update(perm_id, user_group_id_list, asset_group_id_list) + + return HttpResponseRedirect('/jperm/perm_list/') return render_to_response('jperm/perm_edit.html', locals(), context_instance=RequestContext(request)) diff --git a/jumpserver/api.py b/jumpserver/api.py new file mode 100644 index 000000000..2a0aaa932 --- /dev/null +++ b/jumpserver/api.py @@ -0,0 +1,36 @@ +__author__ = 'guanghongwei' + +from juser.models import User, UserGroup +from jasset.models import Asset, BisGroup + + +def user_perm_group_api(user): + if user: + perm_list = [] + user_group_all = user.group.all() + for user_group in user_group_all: + perm_list.extend(user_group.perm_set.all()) + + asset_group_list = [] + for perm in perm_list: + asset_group_list.extend(perm.asset_group.all()) + + return asset_group_list + + +def asset_perm_api(asset): + if asset: + perm_list = [] + asset_group_all = asset.bis_group.all() + for asset_group in asset_group_all: + perm_list.extend(asset_group.perm_set.all()) + + user_group_list = [] + for perm in perm_list: + user_group_list.extend(perm.user_group.all()) + + user_permed_list = [] + for user_group in user_group_list: + user_permed_list.extend(user_group.user_set.all()) + return user_permed_list + diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 41d9339df..41f338e24 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -47,6 +47,14 @@ def groups_str(user_id): return "%s ..." % ' '.join(groups[0:2]) +@register.filter(name='group_str2') +def groups_str2(group_list): + if len(group_list) < 3: + return ' '.join([group.name for group in group_list]) + else: + return '%s ...' % ' '.join([group.name for group in group_list[0:2]]) + + @register.filter(name='group_manage_str') def group_manage_str(username): user = User.objects.get(username=username) diff --git a/juser/views.py b/juser/views.py index 7c9bd06ee..1a4753d6f 100644 --- a/juser/views.py +++ b/juser/views.py @@ -23,6 +23,7 @@ from connect import PyCrypt, KEY from connect import BASE_DIR from connect import CONF from jumpserver.views import md5_crypt, LDAPMgmt, LDAP_ENABLE, ldap_conn, page_list_return +from jumpserver.api import user_perm_group_api if LDAP_ENABLE: LDAP_HOST_URL = CONF.get('ldap', 'host_url') @@ -553,6 +554,7 @@ def user_detail(request): user = User.objects.filter(id=user_id) if user: user = user[0] + asset_group_permed = user_perm_group_api(user) return render_to_response('juser/user_detail.html', locals(), context_instance=RequestContext(request)) diff --git a/templates/jasset/jlist_ip.html b/templates/jasset/jlist_ip.html index fff6723d2..7ebc7db9d 100644 --- a/templates/jasset/jlist_ip.html +++ b/templates/jasset/jlist_ip.html @@ -49,6 +49,7 @@ 登录时间 退出时间 详情 + 有权限用户 @@ -60,6 +61,11 @@ 命令统计 + + {% for user in user_permed_list %} + {{ user.name }} + {% endfor %} + {% endfor %} diff --git a/templates/jperm/perm_detail.html b/templates/jperm/perm_detail.html index 4cf28bfe7..dac9b254d 100644 --- a/templates/jperm/perm_detail.html +++ b/templates/jperm/perm_detail.html @@ -34,7 +34,7 @@ 用户名 姓名 部门 - 属组 + 小组 @@ -42,8 +42,8 @@ {{ user.username }} {{ user.name }} - {{ user.username|group_manage_str }} - {{ user.username|groups_str }} + {{ user.dept.name }} + {{ user.group.all | group_str2 }} {% endfor %} diff --git a/templates/jperm/perm_edit.html b/templates/jperm/perm_edit.html index 5f98313c4..e4e001da1 100644 --- a/templates/jperm/perm_edit.html +++ b/templates/jperm/perm_edit.html @@ -41,7 +41,7 @@
- + 取个名字方便辨识
@@ -53,7 +53,7 @@
@@ -71,7 +71,7 @@
@@ -147,18 +147,6 @@ $('#sudoPerm').validator({ tip: "输入授权名", ok: "", msg: {required: "必须填写!"} - }, - "user_groups_select": { - rule: "required", - tip: "选择用户组", - ok: "", - msg: {checked: "至少选择一个用户组"} - }, - "asset_groups_select": { - rule: "required", - tip: "选择主机组", - ok: "", - msg: {checked: "至少选择一个主机组"} } }, @@ -170,7 +158,11 @@ $('#sudoPerm').validator({ $(document).ready(function(){ $("#submit_button").click(function(){ - $('#users_selected option').each(function(){ + $('#user_groups_select option').each(function(){ + $(this).prop('selected', true) + }) + + $('#asset_groups_select option').each(function(){ $(this).prop('selected', true) }) }) diff --git a/templates/jperm/perm_list.html b/templates/jperm/perm_list.html index de918b4ba..2b1288f85 100644 --- a/templates/jperm/perm_list.html +++ b/templates/jperm/perm_list.html @@ -33,8 +33,7 @@
-
- - - - - - - - - - - - {% for user in contacts2.object_list %} - - - - - - - - {% endfor %} - -
用户角色属组主机数量操作
{{ user.name }} {{ user.id | get_role }} {{ user.username | groups_str }} {{ user.id | perm_asset_count }} - 详情 -
-
-
-
- Showing {{ contacts2.start_index }} to {{ contacts2.end_index }} of {{ p2.count }} entries -
-
-
-
-
    - {% if contacts2.has_previous %} - - {% else %} - - {% endif %} - {% for page in page_range2 %} - {% ifequal current_page page %} -
  • {{ page }}
  • - {% else %} -
  • {{ page }}
  • - {% endifequal %} - {% endfor %} - {% if contacts2.has_next %} - - {% else %} - - {% endif %} -
-
-
-
- -
+{#
#} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# {% for user in contacts2.object_list %}#} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# #} +{# {% endfor %}#} +{# #} +{#
用户角色属组主机数量操作
{{ user.name }} {{ user.id | get_role }} {{ user.username | groups_str }} {{ user.id | perm_asset_count }} #} +{# 详情#} +{#
#} +{#
#} +{#
#} +{#
#} +{# Showing {{ contacts2.start_index }} to {{ contacts2.end_index }} of {{ p2.count }} entries#} +{#
#} +{#
#} +{#
#} +{#
#} +{#
    #} +{# {% if contacts2.has_previous %}#} +{# #} +{# {% else %}#} +{# #} +{# {% endif %}#} +{# {% for page in page_range2 %}#} +{# {% ifequal current_page page %}#} +{#
  • {{ page }}
  • #} +{# {% else %}#} +{#
  • {{ page }}
  • #} +{# {% endifequal %}#} +{# {% endfor %}#} +{# {% if contacts2.has_next %}#} +{# #} +{# {% else %}#} +{# #} +{# {% endif %}#} +{#
#} +{#
#} +{#
#} +{#
#} +{##} +{#
#}
- - diff --git a/templates/juser/user_detail.html b/templates/juser/user_detail.html index e6a78765b..dde718931 100644 --- a/templates/juser/user_detail.html +++ b/templates/juser/user_detail.html @@ -71,6 +71,14 @@ 最后登录 {{ user.last_login }} + + 授权主机组 + + {% for asset_group in asset_group_permed %} + {{ asset_group.name }} + {% endfor %} + + diff --git a/templates/juser/user_list.html b/templates/juser/user_list.html index 5785d3f89..a9bd6b3ce 100644 --- a/templates/juser/user_list.html +++ b/templates/juser/user_list.html @@ -68,7 +68,7 @@ {{ user.username }} {{ user.name }} {{ user.dept.name }} - {{ user.id | groups_str }} + {{ user.group.all | group_str2 }} {{ user.id | get_role }} {{ user.is_active|bool2str }}