diff --git a/jasset/urls.py b/jasset/urls.py index 7e8912a67..cf78be50e 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -13,9 +13,9 @@ urlpatterns = patterns('', url(r'^idc_edit/$', edit_idc), url(r'^idc_detail/$', detail_idc), url(r'^idc_del/(\w+)/$', del_idc), - url(r'^jgroup_add/$', add_group), + url(r'^group_add/$', add_group), url(r'^group_edit/$', edit_group), - url(r'^jgroup_list/$', list_group), + url(r'^group_list/$', list_group), url(r'^group_detail/$', detail_group), url(r'^group_del_host/(\w+)/$', group_del_host), url(r'^group_del/(\w+)/$', group_del), diff --git a/jasset/views.py b/jasset/views.py index a33c6cfa9..ea5e32fec 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -3,16 +3,13 @@ import ast from django.db.models import Q -from django.http import Http404 -from django.http import HttpResponseRedirect from django.template import RequestContext from django.shortcuts import render_to_response -from models import IDC, Asset, BisGroup +from jasset.models import IDC, Asset, BisGroup from juser.models import UserGroup, DEPT -from connect import PyCrypt, KEY -from jlog.models import Log -from jumpserver.views import jasset_host_edit, pages +from jperm.models import Perm +from jumpserver.views import pages from jumpserver.api import * cryptor = PyCrypt(KEY) @@ -55,6 +52,42 @@ def f_add_host(ip, port, idc, jtype, group, dept, active, comment, username='', a.save() +def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment, j_user='', j_password=''): + groups, depts = [], [] + is_active = {u'是': '1', u'否': '2'} + login_types = {'LDAP': 'L', 'MAP': 'M'} + for group in j_group[0].split(): + c = BisGroup.objects.get(name=group.strip()) + groups.append(c) + print j_dept + for d in j_dept[0].split(): + p = DEPT.objects.get(name=d.strip()) + depts.append(p) + + j_type = login_types[j_type] + j_idc = IDC.objects.get(name=j_idc) + a = Asset.objects.get(id=j_id) + if j_type == 'M': + a.ip = j_ip + a.port = j_port + a.login_type = j_type + a.idc = j_idc + a.is_active = j_active + a.comment = j_comment + a.username = j_user + a.password = j_password + else: + a.ip = j_ip + a.port = j_port + a.idc = j_idc + a.login_type = j_type + a.is_active = is_active[j_active] + a.comment = j_comment + a.save() + a.bis_group = groups + a.dept = depts + a.save() + @require_admin def add_host(request): login_types = {'L': 'LDAP', 'M': 'MAP'} @@ -183,27 +216,45 @@ def list_host(request): keyword = request.GET.get('keyword', '') dept_id = get_user_dept(request) dept = DEPT.objects.get(id=dept_id) - if is_super_user(request): - if keyword: - posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | - Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).distinct().order_by('ip') - else: - posts = Asset.objects.all().order_by('ip') - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - elif is_group_admin(request): - if keyword: - posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | - Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip') - else: - posts = Asset.objects.all().filter(dept=dept).order_by('ip') - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + did = request.GET.get('did') + gid = request.GET.get('gid') + sid = request.GET.get('sid') + if did: + dept = DEPT.objects.get(id=did) + posts = dept.asset_set.all() + elif gid: + posts = [] + user_group = UserGroup.objects.get(id=gid) + perms = Perm.objects.filter(user_group=user_group) + for perm in perms: + for post in perm.asset_group.asset_set.all(): + posts.append(post) + posts = list(set(posts)) + elif sid: + pass + + else: + if is_super_user(request): + if keyword: + posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | + Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).distinct().order_by('ip') + else: + posts = Asset.objects.all().order_by('ip') + + elif is_group_admin(request): + if keyword: + posts = Asset.objects.filter(Q(ip__contains=keyword) | Q(idc__name__contains=keyword) | + Q(bis_group__name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip') + else: + posts = Asset.objects.all().filter(dept=dept).order_by('ip') + + elif is_common_user(request): + user_id = request.session.get('user_id') + username = User.objects.get(id=user_id).name + posts = user_perm_asset_api(username) + + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - elif is_common_user(request): - user_id = request.session.get('user_id') - username = User.objects.get(id=user_id).name - posts = user_perm_asset_api(username) - contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - print posts, username return render_to_response('jasset/host_list.html', locals(), context_instance=RequestContext(request)) @@ -426,16 +477,24 @@ def list_group(request): dept_id = get_user_dept(request) dept = DEPT.objects.get(id=dept_id) keyword = request.GET.get('keyword', '') - if is_super_user(request): - if keyword: - posts = BisGroup.objects.exclude(name='ALL').filter(Q(name__contains=keyword) | Q(comment__contains=keyword)) - else: - posts = BisGroup.objects.exclude(name='ALL').order_by('id') - elif is_group_admin(request): - if keyword: - posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept) - else: - posts = BisGroup.objects.filter(dept=dept).order_by('id') + gid = request.GET.get('gid') + if gid: + posts = [] + user_group = UserGroup.objects.get(id=gid) + perms = Perm.objects.filter(user_group=user_group) + for perm in perms: + posts.append(perm.asset_group) + else: + if is_super_user(request): + if keyword: + posts = BisGroup.objects.exclude(name='ALL').filter(Q(name__contains=keyword) | Q(comment__contains=keyword)) + else: + posts = BisGroup.objects.exclude(name='ALL').order_by('id') + elif is_group_admin(request): + if keyword: + posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter(dept=dept) + else: + posts = BisGroup.objects.filter(dept=dept).order_by('id') contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) return render_to_response('jasset/group_list.html', locals(), context_instance=RequestContext(request)) diff --git a/jumpserver.conf b/jumpserver.conf index 099bd2bd9..148d51eb7 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -9,7 +9,7 @@ database = jumpserver [ldap] ldap_enable = 1 -host_url = ldap://127.0.0.1:389 +host_url = ldap://192.168.173.129:389 base_dn = dc=jumpserver, dc=org root_dn = cn=admin,dc=jumpserver,dc=org root_pw = secret234 diff --git a/jumpserver/api.py b/jumpserver/api.py index 537843aed..9fe83f99f 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -12,7 +12,7 @@ from ldap import modlist import hashlib from django.core.paginator import Paginator, EmptyPage, InvalidPage from django.http import HttpResponse, Http404 -from juser.models import User, UserGroup +from juser.models import User, UserGroup, DEPT from jasset.models import Asset, BisGroup from jlog.models import Log @@ -345,4 +345,9 @@ def validate(request, user_group=None, user=None, asset_group=None, asset=None, if not set(assets).issubset(eassets): return False - return True \ No newline at end of file + return True + + +def get_dept_asset(request): + dept_id = get_user_dept(request) + dept_asset = DEPT.objects.get(id=dept_id).asset_set.all() \ No newline at end of file diff --git a/jumpserver/settings.py b/jumpserver/settings.py index 50b6ae0db..c8527ba28 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -53,6 +53,7 @@ INSTALLED_APPS = ( 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'django.contrib.humanize', 'jumpserver', 'juser', 'jasset', diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 52ca04b76..7b61693ea 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -174,7 +174,12 @@ def group_type_to_str(type_name): @register.filter(name='ast_to_list') def ast_to_list(lis): - return ast.literal_eval(lis)[0:2] + ast_lis = ast.literal_eval(lis) + if len(ast_lis) <= 2: + return ','.join([i for i in ast_lis]) + else: + restr = ','.join([i for i in ast_lis[0:2]]) + '...' + return restr @register.filter(name='ast_to_list_1') @@ -191,6 +196,16 @@ def string_length(string, length): return '%s ...' % string[0:length] +@register.filter(name='get_dic_user') +def get_dic_user(dic): + return dic.get('user') + + +@register.filter(name='get_dic_times') +def get_dic_times(dic): + return dic.get('times') + + @register.filter(name='to_name') def to_name(user_id): try: diff --git a/jumpserver/views.py b/jumpserver/views.py index 5f4f82226..8cd1f76c9 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -1,5 +1,7 @@ # coding: utf-8 +from __future__ import division + import datetime from django.db.models import Count @@ -7,6 +9,7 @@ from django.shortcuts import render_to_response from django.template import RequestContext from jasset.models import IDC from juser.models import DEPT +from jperm.models import Apply from jumpserver.api import * @@ -46,8 +49,18 @@ def get_data(data, items, option): def index(request): users = User.objects.all() hosts = Asset.objects.all() - online_host = Log.objects.filter(is_finished=0) - online_user = online_host.distinct() + online = Log.objects.filter(is_finished=0) + online_host = online.values('host').distinct() + online_user = online.values('user').distinct() + active_users = User.objects.filter(is_active=1) + active_hosts = Asset.objects.filter(is_active=1) + + # percent of dashboard + percent_user = format(active_users.count() / users.count(), '.0%') + percent_host = format(active_hosts.count() / hosts.count(), '.0%') + percent_online_user = format(online_user.count() / users.count(), '.0%') + percent_online_host = format(online_host.count() / hosts.count(), '.0%') + li_date, li_str = getDaysByNum(7) today = datetime.datetime.now().day from_week = datetime.datetime.now() - datetime.timedelta(days=7) @@ -56,6 +69,27 @@ def index(request): host_top_ten = week_data.values('host').annotate(times=Count('host')).order_by('-times')[:10] user_dic, host_dic = get_data(week_data, user_top_ten, 'user'), get_data(week_data, host_top_ten, 'host') + # a week data + week_users = week_data.values('user').distinct().count() + week_hosts = week_data.count() + + user_top_five = week_data.values('user').annotate(times=Count('user')).order_by('-times')[:5] + color = ['label-success', 'label-info', 'label-primary', 'label-default', 'label-warnning'] + + # perm apply latest 10 + perm_apply_10 = Apply.objects.order_by('-date_add')[:10] + + # latest 10 login + login_10 = Log.objects.order_by('-start_time')[:10] + + # a week top 10 + # user_top_ten_more = [] + for user_info in user_top_ten: + username = user_info.get('user') + last = Log.objects.filter(user=username).latest('start_time') + user_info['last'] = last + print user_top_ten + top = {'user': '活跃用户数', 'host': '活跃主机数', 'times': '登录次数'} top_dic = {} for key, value in top.items(): @@ -75,51 +109,6 @@ def skin_config(request): return render_to_response('skin_config.html') -def jasset_group_add(name, comment, jtype): - if BisGroup.objects.filter(name=name): - emg = u'该业务组已存在!' - else: - BisGroup.objects.create(name=name, comment=comment, type=jtype) - smg = u'业务组%s添加成功' % name - - -def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment, j_user='', j_password=''): - groups, depts = [], [] - is_active = {u'是': '1', u'否': '2'} - login_types = {'LDAP': 'L', 'MAP': 'M'} - for group in j_group[0].split(): - c = BisGroup.objects.get(name=group.strip()) - groups.append(c) - print j_dept - for d in j_dept[0].split(): - p = DEPT.objects.get(name=d.strip()) - depts.append(p) - - j_type = login_types[j_type] - j_idc = IDC.objects.get(name=j_idc) - a = Asset.objects.get(id=j_id) - if j_type == 'M': - a.ip = j_ip - a.port = j_port - a.login_type = j_type - a.idc = j_idc - a.is_active = j_active - a.comment = j_comment - a.username = j_user - a.password = j_password - else: - a.ip = j_ip - a.port = j_port - a.idc = j_idc - a.login_type = j_type - a.is_active = is_active[j_active] - a.comment = j_comment - a.save() - a.bis_group = groups - a.dept = depts - a.save() - - def pages(posts, r): """分页公用函数""" contact_list = posts diff --git a/templates/index.html b/templates/index.html index f1d4cc6b8..aa62330bf 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,4 +1,6 @@ {% extends 'base.html' %} +{% load mytags %} +{% load humanize %} {% block content %} {% include 'nav_cat_bar.html' %} @@ -12,7 +14,7 @@

{{ users.count}}

-
98%
+
{{ percent_user }}
All user
@@ -25,7 +27,7 @@

{{ hosts.count }}

-
20%
+
{{ percent_host }}
All host
@@ -39,7 +41,7 @@

-
44%
+
{{ percent_online_user }}
Online user
@@ -53,28 +55,172 @@

-
38%
+
{{ percent_online_host }}
Connected host
-
-
-
-
-
-
-
+
+
+

活跃用户TOP5

+ 过去一周共有{{ week_users }}位用户登录{{ week_hosts }}次服务器. + +
+
+
+
+ +
+
+
+
+
权限申请
+ +
+
+

New messages

+ You have 22 new messages and 16 waiting in draft folder. +
+
+
+ {% for perm in perm_apply_10 %} +
+
+ {% ifequal perm.status 0 %} + {{ perm.date_add|naturaltime }} + {% else %} + {{ perm.date_add|naturaltime }} + {% endifequal %} + {{ perm.applyer }} +
申请 {{ perm.bisgroup|ast_to_list }} 主机组权限
+
申请 {{ perm.asset|ast_to_list }} 主机权限
+ {{ perm.date_add }} +
+
+ {% endfor %} +
+
+
+
+
+
+
+
最近十次登录
+
+ 10 Messages +
+
+
+ +
+
+ {% for login in login_10 %} +
+ + image + +
+ {% ifequal login.is_finished 0 %} + {{ login.start_time|naturaltime }} + {% else %} + {{ login.start_time|naturaltime }} + {% endifequal %} + {{ login.user }} 登录了{{ login.host }}
+ {{ login.start_time }} + +
+
+ {% endfor %} +
+ + + +
+ +
+
+
+ +
+
+
+
一周Top10用户
+ +
+
+

You have meeting today!

+ Meeting is on 6:00am. Check your schedule to see detail. +
+
+ {% for data in user_top_ten %} +
+
+
+ + {{ data|get_dic_user }} +
+ {{ data|get_dic_times }}次 +
+
+

最近一次登录

+

{{ data.last.host }}

+

于{{ data.last.start_time |date:"Y-m-d H:i:s" }}

+
+
+
+ {% endfor %} +
+
+
+ + + + + + +