完工,睡觉

pull/6/head
guanghongwei 2015-03-07 00:12:38 +08:00
parent 07b0e2980e
commit a94c050fd9
10 changed files with 203 additions and 150 deletions

View File

@ -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))

36
jumpserver/api.py Normal file
View File

@ -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

View File

@ -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)

View File

@ -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))

View File

@ -49,6 +49,7 @@
<th class="text-center"> 登录时间 </th>
<th class="text-center"> 退出时间 </th>
<th class="text-center"> 详情 </th>
<th class="text-center"> 有权限用户</th>
</tr>
</thead>
<tbody>
@ -60,6 +61,11 @@
<td class="text-center">
<a class="log_command text-success" href="/jlog/history/?id={{ l.id }}">命令统计</a>
</td>
<td>
{% for user in user_permed_list %}
{{ user.name }}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>

View File

@ -34,7 +34,7 @@
<th>用户名</th>
<th>姓名</th>
<th>部门</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
@ -42,8 +42,8 @@
<tr>
<td>{{ user.username }}</td>
<td>{{ user.name }}</td>
<td>{{ user.username|group_manage_str }}</td>
<td>{{ user.username|groups_str }}</td>
<td>{{ user.dept.name }}</td>
<td>{{ user.group.all | group_str2 }}</td>
</tr>
{% endfor %}
</tbody>

View File

@ -41,7 +41,7 @@
<label for="name" class="col-sm-2 control-label">授权名<span class="red-fonts">*</span></label>
<div class="col-sm-8">
<input id="name" name="name" placeholder="授权名称" type="text" class="form-control" value="{{ name }}">
<input id="perm_id" name="perm_id" placeholder="授权名称" type="text" class="form-control" style="display: none" value="{{ perm_id }}">
<input id="perm_id" name="perm_id" style="display: none" value="{{ perm.id }}">
<span class="help-block m-b-none">取个名字方便辨识</span>
</div>
</div>
@ -53,7 +53,7 @@
<div>
<select id="user_groups" name="user_groups" class="form-control" size="5" multiple>
{% for user_group in user_groups %}
<option value="{{ user_group.id }}">{{ user_group.name }} --- {{ user_group.type|group_type_to_str }}</option>
<option value="{{ user_group.id }}">{{ user_group.name }}</option>
{% endfor %}
</select>
</div>
@ -71,7 +71,7 @@
<div>
<select id="user_groups_select" name="user_groups_select" class="form-control m-b" size="5" multiple>
{% for user_group in user_groups_select %}
<option value="{{ user_group.id }}">{{ user_group.name }} --- {{ user_group.type|group_type_to_str }}</option>
<option value="{{ user_group.id }}">{{ user_group.name }}</option>
{% endfor %}
</select>
</div>
@ -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)
})
})

View File

@ -33,8 +33,7 @@
<div class="panel-heading">
<div class="panel-options">
<ul class="nav nav-tabs">
<li id="tab1" class="active"><a data-toggle="tab" href="#tab-1">授权查看</a></li>
<li id="tab2" class=""><a data-toggle="tab" href="#tab-2">用户授权详情</a></li>
<li id="tab1" class="active"><a href="/jperm/perm_list/">授权查看</a></li>
<li style="float: right">
<form method="get" action="" class="pull-right mail-search">
<div class="input-group">
@ -70,14 +69,10 @@
<tr class="gradeX">
<td class="text-center"> {{ perm.name }} </td>
<td class="text-center">
{% for user_group in perm.user_group.all %}
{{ user_group.name }}
{% endfor %}
{{ perm.user_group.all | group_str2 }}
</td>
<td class="text-center">
{% for asset_group in perm.asset_group.all %}
{{ asset_group.name }}
{% endfor %}
{{ perm.asset_group.all | group_str2 }}
</td>
<td class="text-center"> {{ perm.comment }} </td>
<td class="text-center">
@ -129,79 +124,77 @@
</div>
</div>
<div id="tab-2" class="tab-pane">
<table class="table table-striped table-bordered table-hover " id="editable" >
<thead>
<tr>
<th class="text-center">用户</th>
<th class="text-center">角色</th>
<th class="text-center">属组</th>
<th class="text-center">主机数量</th>
<th class="text-center">操作</th>
</tr>
</thead>
<tbody id="perm_list">
{% for user in contacts2.object_list %}
<tr class="gradeX">
<td class="text-center"> {{ user.name }} </td>
<td class="text-center"> {{ user.id | get_role }} </td>
<td class="text-center"> {{ user.username | groups_str }} </td>
<td class="text-center"> {{ user.id | perm_asset_count }} </td>
<td class="text-center">
<a title="[ {{ user.name }} ] 授权详情" href="#" class="btn btn-xs btn-primary">详情</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="row">
<div class="col-sm-6">
<div class="dataTables_info" id="editable_info" role="status" aria-live="polite">
Showing {{ contacts2.start_index }} to {{ contacts2.end_index }} of {{ p2.count }} entries
</div>
</div>
<div class="col-sm-6">
<div class="dataTables_paginate paging_simple_numbers" id="editable_paginate">
<ul class="pagination" style="margin-top: 0; float: right">
{% if contacts2.has_previous %}
<li class="paginate_button previous" aria-controls="editable" tabindex="0" id="editable_previous">
<a href="?page={{ contacts2.previous_page_number }}">Previous</a>
</li>
{% else %}
<li class="paginate_button previous disabled" aria-controls="editable" tabindex="0" id="editable_previous">
<a href="#">Previous</a>
</li>
{% endif %}
{% for page in page_range2 %}
{% ifequal current_page page %}
<li class="paginate_button active" aria-controls="editable" tabindex="0"><a href="?page={{ page }}" title="第{{ page }}页">{{ page }}</a></li>
{% else %}
<li class="paginate_button" aria-controls="editable" tabindex="0"><a href="?page={{ page }}" title="第{{ page }}页">{{ page }}</a></li>
{% endifequal %}
{% endfor %}
{% if contacts2.has_next %}
<li class="paginate_button next" aria-controls="editable" tabindex="0" id="editable_next">
<a href="?page={{ contacts2.next_page_number }}">Next</a>
</li>
{% else %}
<li class="paginate_button next disabled" aria-controls="editable" tabindex="0" id="editable_next">
<a href="#">Next</a>
</li>
{% endif %}
</ul>
</div>
</div>
</div>
</div>
{# <div id="tab-2" class="tab-pane">#}
{# <table class="table table-striped table-bordered table-hover " id="editable" >#}
{# <thead>#}
{# <tr>#}
{# <th class="text-center">用户</th>#}
{# <th class="text-center">角色</th>#}
{# <th class="text-center">属组</th>#}
{# <th class="text-center">主机数量</th>#}
{# <th class="text-center">操作</th>#}
{# </tr>#}
{# </thead>#}
{# <tbody id="perm_list">#}
{# {% for user in contacts2.object_list %}#}
{# <tr class="gradeX">#}
{# <td class="text-center"> {{ user.name }} </td>#}
{# <td class="text-center"> {{ user.id | get_role }} </td>#}
{# <td class="text-center"> {{ user.username | groups_str }} </td>#}
{# <td class="text-center"> {{ user.id | perm_asset_count }} </td>#}
{# <td class="text-center">#}
{# <a title="[ {{ user.name }} ] 授权详情" href="#" class="btn btn-xs btn-primary">详情</a>#}
{# </td>#}
{# </tr>#}
{# {% endfor %}#}
{# </tbody>#}
{# </table>#}
{# <div class="row">#}
{# <div class="col-sm-6">#}
{# <div class="dataTables_info" id="editable_info" role="status" aria-live="polite">#}
{# Showing {{ contacts2.start_index }} to {{ contacts2.end_index }} of {{ p2.count }} entries#}
{# </div>#}
{# </div>#}
{# <div class="col-sm-6">#}
{# <div class="dataTables_paginate paging_simple_numbers" id="editable_paginate">#}
{# <ul class="pagination" style="margin-top: 0; float: right">#}
{# {% if contacts2.has_previous %}#}
{# <li class="paginate_button previous" aria-controls="editable" tabindex="0" id="editable_previous">#}
{# <a href="?page={{ contacts2.previous_page_number }}">Previous</a>#}
{# </li>#}
{# {% else %}#}
{# <li class="paginate_button previous disabled" aria-controls="editable" tabindex="0" id="editable_previous">#}
{# <a href="#">Previous</a>#}
{# </li>#}
{# {% endif %}#}
{# {% for page in page_range2 %}#}
{# {% ifequal current_page page %}#}
{# <li class="paginate_button active" aria-controls="editable" tabindex="0"><a href="?page={{ page }}" title="第{{ page }}页">{{ page }}</a></li>#}
{# {% else %}#}
{# <li class="paginate_button" aria-controls="editable" tabindex="0"><a href="?page={{ page }}" title="第{{ page }}页">{{ page }}</a></li>#}
{# {% endifequal %}#}
{# {% endfor %}#}
{# {% if contacts2.has_next %}#}
{# <li class="paginate_button next" aria-controls="editable" tabindex="0" id="editable_next">#}
{# <a href="?page={{ contacts2.next_page_number }}">Next</a>#}
{# </li>#}
{# {% else %}#}
{# <li class="paginate_button next disabled" aria-controls="editable" tabindex="0" id="editable_next">#}
{# <a href="#">Next</a>#}
{# </li>#}
{# {% endif %}#}
{# </ul>#}
{# </div>#}
{# </div>#}
{# </div>#}
{##}
{# </div>#}
</div>
</div>
</div>
</div>
</div>
</div>

View File

@ -71,6 +71,14 @@
<td class="text-center">最后登录</td>
<td class="text-center">{{ user.last_login }}</td>
</tr>
<tr class="gradeX">
<td class="text-center">授权主机组</td>
<td class="text-center">
{% for asset_group in asset_group_permed %}
{{ asset_group.name }}
{% endfor %}
</td>
</tr>
</table>
</div>
</div>

View File

@ -68,7 +68,7 @@
<td class="text-center"> {{ user.username }} </td>
<td class="text-center"> {{ user.name }} </td>
<td class="text-center"> {{ user.dept.name }} </td>
<td class="text-center"> {{ user.id | groups_str }} </td>
<td class="text-center"> {{ user.group.all | group_str2 }} </td>
<td class="text-center"> {{ user.id | get_role }}</td>
<td class="text-center">{{ user.is_active|bool2str }}</td>
<td class="text-center">