From b9e2c9aa951d0c8f41cd9a6b40d2eb01751c1aed Mon Sep 17 00:00:00 2001 From: guanghongwei Date: Mon, 9 Feb 2015 08:40:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9sudo=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jperm/urls.py | 3 + jperm/views.py | 101 +++++++++++++++-- static/css/style.css | 2 +- templates/jperm/sudo_add.html | 149 +++++++++++++++++++++++++ templates/jperm/sudo_cmd_add.html | 117 ++++++++++++++++++++ templates/jperm/sudo_cmd_list.html | 110 ++++++++++++++++++ templates/jperm/sudo_list.html | 172 +++++++++++++++++------------ templates/nav.html | 3 +- 8 files changed, 577 insertions(+), 80 deletions(-) create mode 100644 templates/jperm/sudo_add.html create mode 100644 templates/jperm/sudo_cmd_add.html create mode 100644 templates/jperm/sudo_cmd_list.html diff --git a/jperm/urls.py b/jperm/urls.py index 6d34810ea..484818473 100644 --- a/jperm/urls.py +++ b/jperm/urls.py @@ -13,4 +13,7 @@ urlpatterns = patterns('jperm.views', (r'^perm_del/$', 'perm_del'), (r'^perm_asset_detail/$', 'perm_asset_detail'), (r'^sudo_list/$', 'sudo_list'), + (r'^sudo_add/$', 'sudo_add'), + (r'^cmd_add/$', 'cmd_add'), + (r'^cmd_list/$', 'cmd_list'), ) diff --git a/jperm/views.py b/jperm/views.py index ad30f23e5..68518fc02 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -4,8 +4,9 @@ from django.shortcuts import render_to_response from django.http import HttpResponseRedirect, HttpResponse from juser.models import User, UserGroup from jasset.models import Asset, BisGroup -from jperm.models import Perm, SudoPerm +from jperm.models import Perm, SudoPerm, CmdGroup from django.core.paginator import Paginator, EmptyPage, InvalidPage +from django.db.models import Q def perm_group_update(user_group_name='', user_group_id='', asset_groups_name='', asset_groups_id=''): @@ -137,12 +138,99 @@ def perm_asset_detail(request): return render_to_response('jperm/perm_asset_detail.html', locals()) + +def user_asset_cmd_groups_get(user_groups_select, asset_groups_select, cmd_groups_select): + user_groups_select_list = [] + asset_groups_select_list = [] + cmd_groups_select_list = [] + + for user_group_id in user_groups_select: + user_groups_select_list.append(UserGroup.objects.get(id=user_group_id)) + + for asset_group_id in asset_groups_select: + asset_groups_select_list.append(BisGroup.objects.get(id=asset_group_id)) + + for cmd_group_id in cmd_groups_select: + cmd_groups_select_list.append(CmdGroup.objects.get(id=cmd_group_id)) + + return user_groups_select_list, asset_groups_select_list, cmd_groups_select_list + + +def sudo_db_add(user_groups_select, asset_groups_select, cmd_groups_select, comment): + user_groups_select_list, asset_groups_select_list, cmd_groups_select_list = \ + user_asset_cmd_groups_get(user_groups_select, asset_groups_select, cmd_groups_select) + + sudo_perm = SudoPerm(comment=comment) + sudo_perm.save() + sudo_perm.user_group = user_groups_select_list + sudo_perm.asset_group = asset_groups_select_list + sudo_perm.cmd_group = cmd_groups_select_list + + +def sudo_ldap_add(user_groups_select, asset_groups_select, cmd_groups_select): + user_groups_select_list, asset_groups_select_list, cmd_groups_select_list = \ + user_asset_cmd_groups_get(user_groups_select, asset_groups_select, cmd_groups_select) + + +def sudo_add(request): + header_title, path1, path2 = u'Sudo授权 | Perm Sudo Add.', u'jperm', u'sudo_add' + user_groups = UserGroup.objects.filter(Q(type='A') | Q(type='P')).order_by('type') + asset_groups = BisGroup.objects.all().order_by('type') + cmd_groups = CmdGroup.objects.all() + + if request.method == 'POST': + user_groups_select = request.POST.getlist('user_groups_select') + asset_groups_select = request.POST.getlist('asset_groups_select') + cmd_groups_select = request.POST.getlist('cmd_groups_select') + comment = request.POST.get('comment', '') + + sudo_db_add(user_groups_select, asset_groups_select, cmd_groups_select, comment) + + + msg = '添加成功' + + return render_to_response('jperm/sudo_add.html', locals()) + + def sudo_list(request): header_title, path1, path2 = u'Sudo授权 | Perm Sudo Detail.', u'jperm', u'sudo_list' - sudo_perms = contact_list = SudoPerm.objects.all() - users = contact_list2 = User.objects.all().order_by('id') - p = paginator = Paginator(contact_list, 10) + sudo_perms = contact_list2 = SudoPerm.objects.all() p2 = paginator2 = Paginator(contact_list2, 10) + user_groups = UserGroup.objects.filter(Q(type='A') | Q(type='P')) + asset_groups = BisGroup.objects.all() + cmd_groups = CmdGroup.objects.all() + 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/sudo_list.html', locals()) + + +def cmd_add(request): + header_title, path1, path2 = u'sudo命令添加 | Sudo Cmd Add.', u'jperm', u'sudo_cmd_add' + + if request.method == 'POST': + name = request.POST.get('name') + cmd = ','.join(request.POST.get('cmd').split()) + comment = request.POST.get('comment') + + CmdGroup.objects.create(name=name, cmd=cmd, comment=comment) + msg = u'命令组添加成功' + + return render_to_response('jperm/sudo_cmd_add.html', locals()) + + +def cmd_list(request): + header_title, path1, path2 = u'sudo命令查看 | Sudo Cmd List.', u'jperm', u'sudo_cmd_list' + + cmd_groups = contact_list = CmdGroup.objects.all() + p = paginator = Paginator(contact_list, 10) + try: page = int(request.GET.get('page', '1')) except ValueError: @@ -150,10 +238,7 @@ def sudo_list(request): try: contacts = paginator.page(page) - contacts2 = paginator2.page(page) except (EmptyPage, InvalidPage): contacts = paginator.page(paginator.num_pages) - contacts2 = paginator2.page(paginator2.num_pages) - return render_to_response('jperm/sudo_list.html', locals()) - + return render_to_response('jperm/sudo_cmd_list.html', locals()) diff --git a/static/css/style.css b/static/css/style.css index b80d0bb47..5b0e5745d 100644 --- a/static/css/style.css +++ b/static/css/style.css @@ -1,4 +1,4 @@ -@import url("//fonts.useso.com/css?family=Open+Sans:300,400,600,700&lang=en"); +/*@import url("//fonts.useso.com/css?family=Open+Sans:300,400,600,700&lang=en");*/ h1, h2, diff --git a/templates/jperm/sudo_add.html b/templates/jperm/sudo_add.html new file mode 100644 index 000000000..3ecbe8174 --- /dev/null +++ b/templates/jperm/sudo_add.html @@ -0,0 +1,149 @@ +{% extends 'base.html' %} +{% load mytags %} +{% block content %} +{% include 'nav_cat_bar.html' %} + +
+
+
+
+
+
Sudo授权 show sudo perm info.
+ +
+ +
+
+ {% if error %} +
{{ error }}
+ {% endif %} + {% if msg %} +
{{ msg }}
+ {% endif %} +
+

用户组

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

授权用户组

+
+ +
+
+
+ +
+ +
+

主机组

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

授权主机组

+
+ +
+
+
+ +
+ +
+

命令组

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

命令组

+
+ +
+
+
+ + +
+
+ +
+ +
+
+
+ + +
+
+
+ + +
+
+
+
+ +
+
+
+
+
+ + +{% endblock %} \ No newline at end of file diff --git a/templates/jperm/sudo_cmd_add.html b/templates/jperm/sudo_cmd_add.html new file mode 100644 index 000000000..3cd6fe7c5 --- /dev/null +++ b/templates/jperm/sudo_cmd_add.html @@ -0,0 +1,117 @@ +{% extends 'base.html' %} + +{% block content %} + {% include 'nav_cat_bar.html' %} +
+
+
+
+
+
填写基本信息 Fill Cmd info.
+ +
+
+
+ {% if error %} +
{{ error }}
+ {% endif %} + {% if msg %} +
{{ msg }}
+ {% endif %} +
+ +
+ +
+
+
+
+ +
+ + + 输入命令一行一个 + +
+
+
+
+ +
+ +
+
+ +
+
+
+ + +
+
+ +
+
+
+
+
+
+ + +{% endblock %} \ No newline at end of file diff --git a/templates/jperm/sudo_cmd_list.html b/templates/jperm/sudo_cmd_list.html new file mode 100644 index 000000000..02545d838 --- /dev/null +++ b/templates/jperm/sudo_cmd_list.html @@ -0,0 +1,110 @@ +{% extends 'base.html' %} +{% load mytags %} +{% block content %} +{% include 'nav_cat_bar.html' %} + +
+
+
+
+
+
查看分组 show group info.
+ +
+ +
+ + + + + + + + + + + + + {% for group in contacts.object_list %} + + + + + + + {% endfor %} + +
组名命令备注操作
{{ group.name }} {{ group.cmd }} {{ group.comment }} + 成员 + 编辑 + 删除 +
+
+
+
+ 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 %} +
+
+
+
+
+
+
+
+
+ + + +{% endblock %} \ No newline at end of file diff --git a/templates/jperm/sudo_list.html b/templates/jperm/sudo_list.html index 215abbe26..22cd04607 100644 --- a/templates/jperm/sudo_list.html +++ b/templates/jperm/sudo_list.html @@ -29,11 +29,16 @@
+
+ 添加命令组 + 查看命令组 +
+