diff --git a/docs/AddUserAsset.py b/docs/AddUserAsset.py index 6c9a00e70..f8faea53a 100644 --- a/docs/AddUserAsset.py +++ b/docs/AddUserAsset.py @@ -127,13 +127,13 @@ def test_add_log(): if __name__ == '__main__': - install() - test_add_dept() - test_add_group() - test_add_user() - test_add_idc() - test_add_asset_group() - test_add_asset() + #install() + #test_add_dept() + #test_add_group() + #test_add_user() + #test_add_idc() + #test_add_asset_group() + #test_add_asset() test_add_log() diff --git a/jumpserver/api.py b/jumpserver/api.py index 537843aed..08d131476 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -10,6 +10,7 @@ from binascii import b2a_hex, a2b_hex import ldap from ldap import modlist import hashlib +import datetime from django.core.paginator import Paginator, EmptyPage, InvalidPage from django.http import HttpResponse, Http404 from juser.models import User, UserGroup diff --git a/jumpserver/context_processors.py b/jumpserver/context_processors.py index 6fdd4d07f..baa3f091b 100644 --- a/jumpserver/context_processors.py +++ b/jumpserver/context_processors.py @@ -2,6 +2,7 @@ from juser.models import User from jasset.models import Asset from jumpserver.api import * + def name_proc(request): user_id = request.session.get('user_id') role_id = request.session.get('role_id') diff --git a/jumpserver/settings.py b/jumpserver/settings.py index 50b6ae0db..c3c4ad509 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -108,6 +108,7 @@ TEMPLATE_DIRS = ( STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), + os.path.join(BASE_DIR, "keys"), ) # Internationalization # https://docs.djangoproject.com/en/1.7/topics/i18n/ diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 52ca04b76..42cc1ec28 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -56,7 +56,7 @@ def groups_str2(group_list): @register.filter(name='group_str2_all') -def groups_str2(group_list): +def group_str2_all(group_list): group_lis = [] for i in group_list: if str(i) != 'ALL': @@ -106,6 +106,12 @@ def member_count(group_id): return group.user_set.count() +@register.filter(name='group_user_count') +def group_user_count(group_id): + group = UserGroup.objects.get(id=group_id) + return group.user_set.count() + + @register.filter(name='dept_user_num') def dept_user_num(dept_id): dept = DEPT.objects.filter(id=dept_id) @@ -217,3 +223,31 @@ def to_avatar(role_id='0'): @register.filter(name='get_user_asset_group') def get_user_asset_group(user): return user_perm_group_api(user) + + +@register.filter(name='group_asset_list') +def group_asset_list(group): + return group.asset_set.all() + + +@register.filter(name='group_asset_list_count') +def group_asset_list_count(group): + return group.asset_set.all().count() + + +@register.filter(name='time_delta') +def time_delta(time_before): + delta = datetime.datetime.now() - time_before + days = delta.days + if days: + return "%s 天前" % days + else: + hours = delta.seconds/3600 + if hours: + return "%s 小时前" % hours + else: + mins = delta.seconds/60 + if mins: + return '%s 分钟前' % mins + else: + return '%s 秒前' % delta.seconds diff --git a/jumpserver/views.py b/jumpserver/views.py index 5f4f82226..2a6fdd037 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -42,8 +42,15 @@ def get_data(data, items, option): return dic +@require_login +def index_cu(request): + return render_to_response('index_cu.html', locals(), context_instance=RequestContext(request)) + + @require_login def index(request): + if request.session.get('role_id') == 0: + return index_cu(request) users = User.objects.all() hosts = Asset.objects.all() online_host = Log.objects.filter(is_finished=0) diff --git a/juser/views.py b/juser/views.py index eb0a361ec..987ecdd4a 100644 --- a/juser/views.py +++ b/juser/views.py @@ -109,7 +109,7 @@ def db_del_user(username): def gen_ssh_key(username, password=None, length=2048): private_key_dir = os.path.join(BASE_DIR, 'keys/jumpserver/') - private_key_file = os.path.join(private_key_dir, username) + private_key_file = os.path.join(private_key_dir, username+".pem") public_key_dir = '/home/%s/.ssh/' % username public_key_file = os.path.join(public_key_dir, 'authorized_keys') is_dir(private_key_dir) @@ -775,18 +775,26 @@ def user_list_adm(request): return render_to_response('juser/user_list.html', locals(), context_instance=RequestContext(request)) -@require_admin +@require_login def user_detail(request): - user_id = request.GET.get('id', '') + header_title, path1, path2 = '查看用户', '用户管理', '用户详情' + if request.session.get('role_id') == 0: + user_id = request.session.get('user_id') + else: + user_id = request.GET.get('id', '') + if request.session.get('role_id') == 1: + user, dept = get_session_user_dept(request) + if not validate(request, user=[user_id]): + return HttpResponseRedirect('/') if not user_id: return HttpResponseRedirect('/juser/user_list/') - if request.session.get('role_id', '') == '1': - if not validate(request, user=[user_id]): - return HttpResponseRedirect('/juser/user_list/') + user = User.objects.filter(id=user_id) if user: user = user[0] asset_group_permed = user_perm_group_api(user) + logs_last = Log.objects.filter(user=user.name).order_by('-start_time')[0:10] + logs_all = Log.objects.filter(user=user.name).order_by('-start_time') return render_to_response('juser/user_detail.html', locals(), context_instance=RequestContext(request)) @@ -953,11 +961,11 @@ def profile(request): if not user_id: return HttpResponseRedirect('/') user = User.objects.get(id=user_id) - return render_to_response('juser/user_detail.html', locals(), context_instance=RequestContext(request)) + return render_to_response('juser/profile.html', locals(), context_instance=RequestContext(request)) def chg_info(request): - header_title, path1, path2 = '修改信息 | Edit Info', '用户管理', '修改个人信息' + header_title, path1, path2 = '修改信息', '用户管理', '修改个人信息' user_id = request.session.get('user_id') user_set = User.objects.filter(id=user_id) error = '' diff --git a/templates/index_cu.html b/templates/index_cu.html new file mode 100644 index 000000000..b1a602230 --- /dev/null +++ b/templates/index_cu.html @@ -0,0 +1,38 @@ +{% extends 'base.html' %} +{% block content %} +{% include 'nav_cat_bar.html' %} + +
ID | +用户名 | +姓名 | +角色 | +部门 | +激活 | +{#添加时间 | #} +{#最后登录 | #} +|
{{ user.id }} | +{{ user.username }} | +{{ user.name }} | +{{ user.id | get_role }} | +{{ user.dept.name }} | +{{ user.email }} | +{{ user.is_active|bool2str }} | +{#{{ user.date_joined }} | #} +{#{{ user.last_login }} | #} +
添加日期: | +{{ user.date_joined }} | +最后登录: | +{{ user.last_login }} | +|||||
用户组: | ++ {% for group in user.group.all %} + {{ group.name }} + {% endfor %} + | +|||||||
授权主机组: | #} +{##} +{# {% for group in user|get_user_asset_group %}#} +{# {{ group.name }}#} +{# {% endfor %}#} +{# | #} +{#|||||||
授权主机组 | #} +{##} +{# {% for asset_group in asset_group_permed %}#} +{# {{ asset_group.name }}#} +{# {% endfor %}#} +{# | #} +{#
ID | -用户名 | -姓名 | -角色 | -部门 | -激活 | -{#添加时间 | #} -{#最后登录 | #} -|
{{ user.id }} | -{{ user.username }} | -{{ user.name }} | -{{ user.id | get_role }} | -{{ user.dept.name }} | -{{ user.email }} | -{{ user.is_active|bool2str }} | -{#{{ user.date_joined }} | #} -{#{{ user.last_login }} | #} -
添加日期: | -{{ user.date_joined }} | -最后登录: | -{{ user.last_login }} | -|||||
用户组: | -- {% for group in user.group.all %} - {{ group.name }} - {% endfor %} - | -|||||||
授权主机组: | -- {% for group in user|get_user_asset_group %} - {{ group.name }} - {% endfor %} - | -|||||||
授权主机组 | #} -{##} -{# {% for asset_group in asset_group_permed %}#} -{# {{ asset_group.name }}#} -{# {% endfor %}#} -{# | #} -{#
ID | +{{ user.id }} | +|
用户名 | +{{ user.username }} | +|
姓名 | +{{ user.name }} | +|
角色 | +{{ user.id | get_role }} | +|
部门 | +{{ user.dept.name }} | +|
{{ user.email }} | +||
激活 | +{{ user.is_active|bool2str }} | +|
添加日期 | +{{ user.date_joined|date:"Y-m-d H:i:s" }} | +|
最后登录 | +{{ user.last_login|date:"Y-m-d H:i:s" }} | +|
所在用户组 | +
+
|
+
{{ group.comment }}
+ +
+ {% for asset in group|group_asset_list %}
+ {{ asset.ip }}
+ {% endfor %}
+