From 8d358a7a689834df14f027328f5041013bc6e192 Mon Sep 17 00:00:00 2001 From: ibuler Date: Mon, 14 Nov 2016 19:28:21 +0800 Subject: [PATCH] UPdate some --- .../templates/audits/login_log_list.html | 100 ++++++++++++++++++ apps/audits/urls.py | 1 + apps/audits/utils.py | 9 +- apps/audits/views.py | 42 +++++++- apps/static/js/jumpserver.js | 24 +++-- apps/templates/_nav.html | 6 +- .../users/user_asset_permission.html | 3 - apps/users/templates/users/user_detail.html | 1 - .../templates/users/user_granted_asset.html | 3 - apps/users/templates/users/user_list.html | 2 +- requirements.txt | 3 +- 11 files changed, 165 insertions(+), 29 deletions(-) create mode 100644 apps/audits/templates/audits/login_log_list.html diff --git a/apps/audits/templates/audits/login_log_list.html b/apps/audits/templates/audits/login_log_list.html new file mode 100644 index 000000000..53b4ef0a2 --- /dev/null +++ b/apps/audits/templates/audits/login_log_list.html @@ -0,0 +1,100 @@ +{% extends '_base_list.html' %} +{% load i18n %} +{% load static %} +{% load common_tags %} +{% block content_left_head %} + + +{% endblock %} + + +{% block table_search %} +
+
+
+ + + to + +
+
+
+ +
+
+ +
+
+
+ +
+
+
+{% endblock %} + +{% block table_head %} + {% trans 'ID' %} + {% trans 'Username' %} + {% trans 'Name' %} + {% trans 'Type' %} + {% trans 'UA' %} + {% trans 'IP' %} + {% trans 'City' %} + {% trans 'Date' %} +{% endblock %} + +{% block table_body %} + {% for login_log in login_log_list %} + + + {{ login_log.id }} +{# {{ login_log.id }}#} + + {{ login_log.username }} + {{ login_log.name }} + {{ login_log.get_login_type_display }} + {% if login_log.login_type == 'W' %} + + {{ login_log.user_agent | truncatechars:20 }} + + {% else %} + {{ login_log.terminal }} + {% endif %} + {{ login_log.login_ip }} + {{ login_log.login_city }} + {{ login_log.date_login }} + + {% endfor %} +{% endblock %} + +{% block custom_foot_js %} + + +{% endblock %} + diff --git a/apps/audits/urls.py b/apps/audits/urls.py index a752378ff..b3556733f 100644 --- a/apps/audits/urls.py +++ b/apps/audits/urls.py @@ -12,6 +12,7 @@ urlpatterns = [ url(r'^proxy-log/(?P\d+)$', views.ProxyLogDetailView.as_view(), name='proxy-log-detail'), url(r'^proxy-log/(?P\d+)/commands$', views.ProxyLogCommandsListView.as_view(), name='proxy-log-commands-list'), url(r'^command-log$', views.CommandLogListView.as_view(), name='command-log-list'), + url(r'^login-log$', views.LoginLogListView.as_view(), name='login-log-list'), ] router = routers.DefaultRouter() diff --git a/apps/audits/utils.py b/apps/audits/utils.py index c495dd6a5..303ba18c9 100644 --- a/apps/audits/utils.py +++ b/apps/audits/utils.py @@ -10,16 +10,15 @@ from .models import LoginLog def validate_ip(ip): try: - ipaddress.ip_address(ip) + ipaddress.ip_address(ip.decode('utf-8')) return True except ValueError: - pass + print('valid error') return False def write_login_log(username, name='', login_type='W', terminal='', login_ip='', user_agent=''): - print(login_ip) if not (login_ip and validate_ip(login_ip)): login_ip = '0.0.0.0' if not name: @@ -29,11 +28,11 @@ def write_login_log(username, name='', login_type='W', terminal=terminal, login_city=login_city, user_agent=user_agent) -def get_ip_city(ip, timeout=3): +def get_ip_city(ip, timeout=10): # Taobao ip api: http://ip.taobao.com//service/getIpInfo.php?ip=8.8.8.8 # Sina ip api: http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=8.8.8.8&format=js - url = 'http://ip.taobao.com//service/getIpInfo.php?ip=' + ip + url = 'http://ip.taobao.com/service/getIpInfo.php?ip=' + ip r = requests.get(url, timeout=timeout) city = 'Unknown' if r.status_code == 200: diff --git a/apps/audits/views.py b/apps/audits/views.py index 35371894b..cc9755107 100644 --- a/apps/audits/views.py +++ b/apps/audits/views.py @@ -10,7 +10,7 @@ from django.urls import reverse_lazy from django.conf import settings from django.db.models import Q -from .models import ProxyLog, CommandLog +from .models import ProxyLog, CommandLog, LoginLog from .hands import User, Asset, SystemUser, AdminUserRequiredMixin @@ -151,3 +151,43 @@ class CommandLogListView(AdminUserRequiredMixin, ListView): } kwargs.update(context) return super(CommandLogListView, self).get_context_data(**kwargs) + + +class LoginLogListView(AdminUserRequiredMixin, ListView): + model = LoginLog + template_name = 'audits/login_log_list.html' + context_object_name = 'login_log_list' + + def get_queryset(self): + self.queryset = super(LoginLogListView, self).get_queryset() + self.keyword = keyword = self.request.GET.get('keyword', '') + self.username = username = self.request.GET.get('username', '') + self.date_from_s = date_from_s = self.request.GET.get('date_from', '%s' % seven_days_ago_s) + self.date_to_s = date_to_s = self.request.GET.get('date_to', '%s' % now_s) + + if date_from_s: + date_from = timezone.datetime.strptime(date_from_s, '%m/%d/%Y') + self.queryset = self.queryset.filter(date_login__gt=date_from) + if date_to_s: + date_to = timezone.datetime.strptime(date_to_s + ' 23:59:59', '%m/%d/%Y %H:%M:%S') + self.queryset = self.queryset.filter(date_login__lt=date_to) + if username: + self.queryset = self.queryset.filter(username=username) + if keyword: + self.queryset = self.queryset.filter(Q(username__contains=keyword) | + Q(name__icontains=keyword) | + Q(login_ip=keyword)).distinct() + return self.queryset + + def get_context_data(self, **kwargs): + context = { + 'app': _('Audits'), + 'action': _('Proxy log list'), + 'user_list': User.objects.all().order_by('username'), + 'keyword': self.keyword, + 'date_from': self.date_from_s, + 'date_to': self.date_to_s, + 'username': self.username, + } + kwargs.update(context) + return super(LoginLogListView, self).get_context_data(**kwargs) diff --git a/apps/static/js/jumpserver.js b/apps/static/js/jumpserver.js index 01d62b607..367e51d05 100644 --- a/apps/static/js/jumpserver.js +++ b/apps/static/js/jumpserver.js @@ -323,7 +323,9 @@ jumpserver.initDataTable = function (options) { } ], columnDefs: columnDefs, - select: options.select || {style: 'multi'}, + // select: 'single', + // select: options.select || {style: 'single'}, + // select: false, ajax: { url: options.ajax_url , dataSrc: "" @@ -331,16 +333,16 @@ jumpserver.initDataTable = function (options) { columns: options.columns || [], lengthMenu: [[15, 25, 50, -1], [15, 25, 50, "All"]] }); - table.on('select', function(e, dt, type, indexes) { - var $node = table[ type ]( indexes ).nodes().to$(); - $node.find('input.ipt_check').prop('checked', true); - }).on('deselect', function(e, dt, type, indexes) { - var $node = table[ type ]( indexes ).nodes().to$(); - $node.find('input.ipt_check').prop('checked', false); - }).on('draw', function(){ - $('#op').html(options.op_html || ''); - $('#uc').html(options.uc_html || ''); - }); + // table.on('select', function(e, dt, type, indexes) { + // var $node = table[ type ]( indexes ).nodes().to$(); + // $node.find('input.ipt_check').prop('checked', true); + // }).on('deselect', function(e, dt, type, indexes) { + // var $node = table[ type ]( indexes ).nodes().to$(); + // $node.find('input.ipt_check').prop('checked', false); + // }).on('draw', function(){ + // $('#op').html(options.op_html || ''); + // $('#uc').html(options.uc_html || ''); + // }); $('.ipt_check_all').on('click', function() { if (!jumpserver.checked) { $(this).closest('table').find('.ipt_check').prop('checked', true); diff --git a/apps/templates/_nav.html b/apps/templates/_nav.html index 5a49c0a91..4d62e185d 100644 --- a/apps/templates/_nav.html +++ b/apps/templates/_nav.html @@ -48,10 +48,10 @@
  • {% trans 'Command log' %}
  • -
  • - {% trans 'Login log' %} +
  • + {% trans 'Login log' %}
  • -
  • +
  • {% trans 'Admin log' %}
  • diff --git a/apps/users/templates/users/user_asset_permission.html b/apps/users/templates/users/user_asset_permission.html index 8c6eba155..9937b5af8 100644 --- a/apps/users/templates/users/user_asset_permission.html +++ b/apps/users/templates/users/user_asset_permission.html @@ -25,9 +25,6 @@
  • {% trans 'Asset granted' %}
  • -
  • - {% trans 'Login history' %} -
  • diff --git a/apps/users/templates/users/user_detail.html b/apps/users/templates/users/user_detail.html index d4e328465..49e9fb62e 100644 --- a/apps/users/templates/users/user_detail.html +++ b/apps/users/templates/users/user_detail.html @@ -24,7 +24,6 @@ {% trans 'Asset permission' %}
  • {% trans 'Asset granted' %}
  • -
  • {% trans 'Login history' %}
  • Update
  • diff --git a/apps/users/templates/users/user_granted_asset.html b/apps/users/templates/users/user_granted_asset.html index 0ffba37c3..1009d6540 100644 --- a/apps/users/templates/users/user_granted_asset.html +++ b/apps/users/templates/users/user_granted_asset.html @@ -25,9 +25,6 @@
  • {% trans 'Asset granted' %}
  • -
  • - {% trans 'Login history' %} -
  • diff --git a/apps/users/templates/users/user_list.html b/apps/users/templates/users/user_list.html index cf2c0d2b7..d38e1183b 100644 --- a/apps/users/templates/users/user_list.html +++ b/apps/users/templates/users/user_list.html @@ -54,7 +54,7 @@ $(document).ready(function(){ }}, {targets: 4, createdCell: function (td, cellData) { var innerHtml = cellData.length > 20 ? cellData.substring(0, 20) + '...': cellData; - $(td).html('' + innerHtml + ''); + $(td).html('' + innerHtml + ''); }}, {targets: 6, createdCell: function (td, cellData) { if (!cellData) { diff --git a/requirements.txt b/requirements.txt index 698006f9a..15ad75500 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,5 @@ sshpubkeys==2.2.0 djangorestframework-bulk==0.2.1 paramiko==2.0.2 django-redis-cache==1.7.1 -requests==2.11.1 \ No newline at end of file +requests==2.11.1 +itsdangerous==0.24 \ No newline at end of file