diff --git a/jperm/views.py b/jperm/views.py index e577af3fc..3ca221155 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -1,7 +1,8 @@ # coding: utf-8 from django.shortcuts import render_to_response -from django.http import HttpResponseRedirect, HttpResponse +from django.http import HttpResponseRedirect +from django.template import RequestContext from juser.models import User, UserGroup from jasset.models import Asset, BisGroup from jperm.models import Perm, SudoPerm, CmdGroup @@ -51,7 +52,7 @@ def perm_list(request): except (EmptyPage, InvalidPage): contacts = paginator.page(paginator.num_pages) contacts2 = paginator2.page(paginator2.num_pages) - return render_to_response('jperm/perm_list.html', locals()) + return render_to_response('jperm/perm_list.html', locals(), context_instance=RequestContext(request)) def user_asset_cmd_groups_get(user_groups_select='', asset_groups_select='', cmd_groups_select=''): @@ -92,7 +93,7 @@ def perm_add(request): perm.user_group = user_groups perm.asset_group = asset_groups msg = '添加成功' - return render_to_response('jperm/perm_add.html', locals()) + return render_to_response('jperm/perm_add.html', locals(), context_instance=RequestContext(request)) def perm_list_ajax(request): @@ -151,8 +152,8 @@ def perm_edit(request): 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()) - return render_to_response('jperm/perm_edit.html', locals()) + return HttpResponseRedirect('/jperm/perm_list/', locals(), context_instance=RequestContext(request)) + return render_to_response('jperm/perm_edit.html', locals(), context_instance=RequestContext(request)) def perm_detail(request): @@ -172,7 +173,7 @@ def perm_detail(request): for asset_group in asset_groups: assets_list.extend(asset_group.asset_set.all()) - return render_to_response('jperm/perm_detail.html', locals()) + return render_to_response('jperm/perm_detail.html', locals(), context_instance=RequestContext(request)) def perm_del(request): @@ -191,7 +192,7 @@ def perm_asset_detail(request): if user: user = user[0] assets_list = perm_user_asset(user_id) - return render_to_response('jperm/perm_asset_detail.html', locals()) + return render_to_response('jperm/perm_asset_detail.html', locals(), context_instance=RequestContext(request)) def sudo_db_add(name, user_runas, user_groups_select, asset_groups_select, cmd_groups_select, comment): @@ -286,7 +287,7 @@ def sudo_add(request): msg = '添加成功' return HttpResponseRedirect('/jperm/sudo_list/') - return render_to_response('jperm/sudo_add.html', locals()) + return render_to_response('jperm/sudo_add.html', locals(), context_instance=RequestContext(request)) def sudo_list(request): @@ -306,7 +307,7 @@ def sudo_list(request): contacts1 = paginator1.page(page1) except (EmptyPage, InvalidPage): contacts1 = paginator1.page(paginator1.num_pages) - return render_to_response('jperm/sudo_list.html', locals()) + return render_to_response('jperm/sudo_list.html', locals(), context_instance=RequestContext(request)) def sudo_edit(request): @@ -352,7 +353,7 @@ def sudo_edit(request): return HttpResponseRedirect('/jperm/sudo_list/') - return render_to_response('jperm/sudo_edit.html', locals()) + return render_to_response('jperm/sudo_edit.html', locals(), context_instance=RequestContext(request)) def sudo_detail(request): @@ -376,7 +377,7 @@ def sudo_detail(request): for cmd_group in cmd_groups: cmds_list.append({cmd_group.name: cmd_group.cmd.split(',')}) - return render_to_response('jperm/sudo_detail.html', locals()) + return render_to_response('jperm/sudo_detail.html', locals(), context_instance=RequestContext(request)) def sudo_del(request): @@ -403,7 +404,7 @@ def cmd_add(request): return HttpResponseRedirect('/jperm/cmd_list/') - return render_to_response('jperm/sudo_cmd_add.html', locals()) + return render_to_response('jperm/sudo_cmd_add.html', locals(), context_instance=RequestContext(request)) def cmd_edit(request): @@ -429,7 +430,7 @@ def cmd_edit(request): if cmd_group: cmd_group.update(name=name, cmd=cmd, comment=comment) return HttpResponseRedirect('/jperm/cmd_list/') - return render_to_response('jperm/sudo_cmd_add.html', locals()) + return render_to_response('jperm/sudo_cmd_add.html', locals(), context_instance=RequestContext(request)) def cmd_list(request): @@ -447,7 +448,7 @@ def cmd_list(request): contacts = paginator.page(page) except (EmptyPage, InvalidPage): contacts = paginator.page(paginator.num_pages) - return render_to_response('jperm/sudo_cmd_list.html', locals()) + return render_to_response('jperm/sudo_cmd_list.html', locals(), context_instance=RequestContext(request)) def cmd_del(request): diff --git a/jumpserver/context_processors.py b/jumpserver/context_processors.py new file mode 100644 index 000000000..3cae562ae --- /dev/null +++ b/jumpserver/context_processors.py @@ -0,0 +1,5 @@ + +def name_proc(request): + user_id = request.session.get('user_id') + role = request.session.get('role') + return {'user_id': user_id, 'role': role} \ No newline at end of file diff --git a/jumpserver/settings.py b/jumpserver/settings.py index f935e347b..7432d56a5 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -90,6 +90,7 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.static', 'django.core.context_processors.tz', 'django.contrib.messages.context_processors.messages', + 'jumpserver.context_processors.name_proc' ) TEMPLATE_DIRS = ( diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 11f8c90cf..07f546db4 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -97,3 +97,18 @@ def filter_private(group): p = BisGroup.objects.get(name='ALL') [agroup.append(g) for g in group if g != p] return agroup + + +@register.filter(name='to_name') +def to_name(user_id): + user = User.objects.filter(id=user_id) + if user: + user = user[0] + return user.name + else: + return '非法用户' + +@register.filter(name='to_role_name') +def to_role_name(role_id): + role_dict = {'0': '普通用户', '1': '组管理员', '2': '超级管理员'} + return role_dict.get(role_id, '未知') \ No newline at end of file diff --git a/jumpserver/views.py b/jumpserver/views.py index dd2510ccc..f20dc01e9 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -11,6 +11,7 @@ import os from django.http import HttpResponse from django.shortcuts import render_to_response from django.http import HttpResponseRedirect +from django.template import RequestContext from juser.models import User from jasset.models import Asset, BisGroup, IDC @@ -32,7 +33,7 @@ def md5_crypt(string): def base(request): - return render_to_response('base.html') + return render_to_response('base.html', context_instance=RequestContext(request)) def skin_config(request): @@ -50,6 +51,7 @@ def jasset_group_add(name, comment, type): class ServerError(Exception): pass + def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment): groups = [] is_active = {u'是': '1', u'否': '2'} @@ -99,7 +101,7 @@ def login(request): if user: user = user[0] if md5_crypt(password) == user.password: - request.session['username'] = username + request.session['user_id'] = user.id if user.role == 'SU': request.session['role'] = 2 elif user.role == 'GA': diff --git a/juser/views.py b/juser/views.py index d2073d553..2ab3733d6 100644 --- a/juser/views.py +++ b/juser/views.py @@ -13,6 +13,7 @@ from django.http import HttpResponseRedirect from django.shortcuts import render_to_response from django.core.exceptions import ObjectDoesNotExist from django.db.models import Q +from django.template import RequestContext from django.http import HttpResponse from django.core.paginator import Paginator, EmptyPage, InvalidPage @@ -212,7 +213,7 @@ def ldap_del_user(username): def group_add(request, group_type_select='A'): error = '' msg = '' - header_title, path1, path2 = '添加属组 | Add Group', 'juser', 'group_add' + header_title, path1, path2 = '添加属组 | Group Add', '用户管理', '添加用户组' group_types = { 'M': '部门', 'A': '用户组', @@ -245,7 +246,7 @@ def group_add(request, group_type_select='A'): else: msg = u'添加组 %s 成功' % group_name - return render_to_response('juser/group_add.html', locals()) + return render_to_response('juser/group_add.html', locals(), context_instance=RequestContext(request)) def group_add_ajax(request): @@ -256,11 +257,11 @@ def group_add_ajax(request): else: users = [user for user in users_all if not user.user_group.filter(type='M')] - return render_to_response('juser/group_add_ajax.html', locals()) + return render_to_response('juser/group_add_ajax.html', locals(), context_instance=RequestContext(request)) def group_list(request): - header_title, path1, path2 = '查看属组 | Show Group', 'juser', 'group_list' + header_title, path1, path2 = '查看属组 | Show Group', '用户管理', '查看用户组' groups = contact_list = UserGroup.objects.filter(Q(type='M') | Q(type='A')).order_by('type') p = paginator = Paginator(contact_list, 10) @@ -273,7 +274,7 @@ def group_list(request): contacts = paginator.page(page) except (EmptyPage, InvalidPage): contacts = paginator.page(paginator.num_pages) - return render_to_response('juser/group_list.html', locals()) + return render_to_response('juser/group_list.html', locals(), context_instance=RequestContext(request)) def group_detail(request): @@ -282,7 +283,7 @@ def group_detail(request): return HttpResponseRedirect('/') group = UserGroup.objects.get(id=group_id) users = group.user_set.all() - return render_to_response('juser/group_detail.html', locals()) + return render_to_response('juser/group_detail.html', locals(), context_instance=RequestContext(request)) def group_del(request): @@ -291,7 +292,7 @@ def group_del(request): return HttpResponseRedirect('/') group = UserGroup.objects.get(id=group_id) group.delete() - return HttpResponseRedirect('/juser/group_list/', locals()) + return HttpResponseRedirect('/juser/group_list/', locals(), context_instance=RequestContext(request)) def group_edit(request): @@ -312,7 +313,7 @@ def group_edit(request): users_selected = group.user_set.all() users = [user for user in users_all if user not in users_selected] - return render_to_response('juser/group_edit.html', locals()) + return render_to_response('juser/group_edit.html', locals(), context_instance=RequestContext(request)) else: group_id = request.POST.get('group_id', None) group_name = request.POST.get('group_name', None) @@ -341,7 +342,7 @@ def user_list(request): contacts = paginator.page(page) except (EmptyPage, InvalidPage): contacts = paginator.page(paginator.num_pages) - return render_to_response('juser/user_list.html', locals()) + return render_to_response('juser/user_list.html', locals(), context_instance=RequestContext(request)) def user_detail(request): @@ -349,7 +350,7 @@ def user_detail(request): if not user_id: return HttpResponseRedirect('/') user = User.objects.get(id=user_id) - return render_to_response('juser/user_detail.html', locals()) + return render_to_response('juser/user_detail.html', locals(), context_instance=RequestContext(request)) def user_del(request): @@ -362,7 +363,7 @@ def user_del(request): group.delete() server_del_user(user.username) ldap_del_user(user.username) - return HttpResponseRedirect('/juser/user_list/', locals()) + return HttpResponseRedirect('/juser/user_list/', locals(), context_instance=RequestContext(request)) def user_edit(request): @@ -430,13 +431,13 @@ def user_edit(request): return HttpResponseRedirect('/juser/user_list/') - return render_to_response('juser/user_add.html', locals()) + return render_to_response('juser/user_add.html', locals(), context_instance=RequestContext(request)) def user_add(request): error = '' msg = '' - header_title, path1, path2 = '添加用户 | Add User', 'juser', 'user_add' + header_title, path1, path2 = '添加用户 | User Add', '用户管理', '添加用户' user_role = {'SU': u'超级管理员', 'GA': u'组管理员', 'CU': u'普通用户'} manage_groups = UserGroup.objects.filter(type='M') auth_groups = UserGroup.objects.filter(type='A') @@ -497,7 +498,7 @@ def user_add(request): except Exception: pass - return render_to_response('juser/user_add.html', locals()) + return render_to_response('juser/user_add.html', locals(), context_instance=RequestContext(request)) diff --git a/templates/nav_li_profile.html b/templates/nav_li_profile.html index 09811e0c8..b9e9384c1 100644 --- a/templates/nav_li_profile.html +++ b/templates/nav_li_profile.html @@ -1,10 +1,11 @@ +{% load mytags %}