diff --git a/jasset/urls.py b/jasset/urls.py index ae8b89612..7e8912a67 100644 --- a/jasset/urls.py +++ b/jasset/urls.py @@ -3,7 +3,6 @@ from django.conf.urls import patterns, include, url from jasset.views import * urlpatterns = patterns('', - url(r'^$', index), url(r'^host_add/$', add_host), url(r"^host_add_multi/$", add_host_multi), url(r'^host_list/$', list_host), @@ -23,5 +22,4 @@ urlpatterns = patterns('', url(r'^host_del/(\w+)/$', host_del), url(r'^host_edit/$', host_edit), url(r'^host_edit/batch/$', batch_host_edit), - url(r'^test/$', test), ) \ No newline at end of file diff --git a/jasset/views.py b/jasset/views.py index 79c415cb3..a33c6cfa9 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -3,6 +3,7 @@ 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 @@ -12,15 +13,13 @@ 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 jumpserver.api import asset_perm_api -from jumpserver.api import user_perm_group_api, require_login, require_super_user, \ - require_admin, is_group_admin, is_super_user, is_common_user, get_user_dept +from jumpserver.api import * cryptor = PyCrypt(KEY) -def index(request): - return render_to_response('jasset/jasset.html', ) +class RaiseError(Exception): + pass def f_add_host(ip, port, idc, jtype, group, dept, active, comment, username='', password=''): @@ -70,6 +69,7 @@ def add_host(request): user_id = request.session.get('user_id') edept = DEPT.objects.get(id=dept_id) egroup = edept.bisgroup_set.all() + if request.method == 'POST': j_ip = request.POST.get('j_ip') j_idc = request.POST.get('j_idc') @@ -80,6 +80,10 @@ def add_host(request): j_comment = request.POST.get('j_comment') j_dept = request.POST.getlist('j_dept') + if is_group_admin(request) and not validate(request, asset_group=j_group, edept=j_dept): + emg = u'添加失败,您无权操作!' + return render_to_response('jasset/host_add.html', locals(), context_instance=RequestContext(request)) + if Asset.objects.filter(ip=str(j_ip)): emg = u'该IP %s 已存在!' % j_ip return render_to_response('jasset/host_add.html', locals(), context_instance=RequestContext(request)) @@ -136,6 +140,7 @@ def batch_host_edit(request): j_id = "editable[" + str(i) + "][j_id]" j_ip = "editable[" + str(i) + "][j_ip]" j_port = "editable[" + str(i) + "][j_port]" + j_dept = "editable[" + str(i) + "][j_dept]" j_idc = "editable[" + str(i) + "][j_idc]" j_type = "editable[" + str(i) + "][j_type]" j_group = "editable[" + str(i) + "][j_group]" @@ -145,11 +150,18 @@ def batch_host_edit(request): j_id = request.POST.get(j_id).strip() j_ip = request.POST.get(j_ip).strip() j_port = request.POST.get(j_port).strip() + j_dept = request.POST.getlist(j_dept).strip() j_idc = request.POST.get(j_idc).strip() j_type = request.POST.get(j_type).strip() j_group = request.POST.getlist(j_group) j_active = request.POST.get(j_active).strip() j_comment = request.POST.get(j_comment).strip() + print j_dept, j_group + # + # if is_group_admin(request) and not validate(request, asset=[j_id]): + # emg = u'删除失败,您无权操作!' + # print 'hehe' + # return HttpResponseRedirect('/jasset/host_list/') if j_type == 'M': j_user = "editable[" + str(i) + "][j_user]" @@ -157,9 +169,9 @@ def batch_host_edit(request): j_user = request.POST.get(j_user).strip() password = request.POST.get(j_password).strip() j_password = cryptor.encrypt(password) - jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment, j_user, j_password) + 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) else: - jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment) + jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment) return render_to_response('jasset/host_list.html') @@ -187,7 +199,11 @@ def list_host(request): contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) elif is_common_user(request): - pass + 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)) @@ -198,11 +214,18 @@ def host_del(request, offset): for i in range(int(len_list)): key = "id_list[" + str(i) + "]" jid = request.POST.get(key) + print jid + if is_group_admin(request) and not validate(request, asset=[jid]): + emg = u'删除失败,您无权操作!' + return HttpResponseRedirect('/jasset/host_list/') a = Asset.objects.get(id=jid).ip Asset.objects.filter(id=jid).delete() BisGroup.objects.filter(name=a).delete() else: jid = int(offset) + if is_group_admin(request) and not validate(request, asset=[jid]): + emg = u'删除失败,您无权操作!' + return HttpResponseRedirect('/jasset/host_list/') a = Asset.objects.get(id=jid).ip BisGroup.objects.filter(name=a).delete() Asset.objects.filter(id=jid).delete() @@ -234,8 +257,12 @@ def host_edit(request): j_active = request.POST.get('j_active') j_comment = request.POST.get('j_comment') j_idc = IDC.objects.get(name=j_idc) + + if is_group_admin(request) and not validate(request, asset_group=j_group, edept=j_dept): + emg = u'修改失败,您无权操作!' + return render_to_response('jasset/host_edit.html', locals(), context_instance=RequestContext(request)) + for group in j_group: - print group c = BisGroup.objects.get(name=group) groups.append(c) @@ -373,8 +400,12 @@ def add_group(request): j_dept = request.POST.get('j_dept') j_hosts = request.POST.getlist('j_hosts') j_comment = request.POST.get('j_comment') - j_dept = DEPT.objects.get(name=j_dept) + if is_group_admin(request) and not validate(request, asset=j_hosts, edept=[j_dept]): + emg = u'添加失败,您无权操作!' + return render_to_response('jasset/group_add.html', locals(), context_instance=RequestContext(request)) + + j_dept = DEPT.objects.get(name=j_dept) if BisGroup.objects.filter(name=j_group): emg = u'该主机组已存在!' return render_to_response('jasset/group_add.html', locals(), context_instance=RequestContext(request)) @@ -530,8 +561,4 @@ def host_search(request): comment__contains=keyword)).filter(dept=dept).distinct().order_by('ip') contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) - return render_to_response('jasset/host_search.html', locals(), context_instance=RequestContext(request)) - - -def test(request): - return render_to_response('jasset/test.html', locals()) + return render_to_response('jasset/host_search.html', locals(), context_instance=RequestContext(request)) \ No newline at end of file diff --git a/jlog/views.py b/jlog/views.py index 59a448b93..33d32b83c 100644 --- a/jlog/views.py +++ b/jlog/views.py @@ -45,7 +45,12 @@ def log_list_online(request): contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) elif is_common_user(request): - posts = Log.objects.filter(is_finished=0).filter(user=username).order_by('-start_time') + if keyword: + posts = Log.objects.filter(user=username).filter(Q(user__contains=keyword) | Q(host__contains=keyword))\ + .filter(is_finished=0).order_by('-start_time') + else: + posts = Log.objects.filter(is_finished=0).filter(user=username).order_by('-start_time') + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) return render_to_response('jlog/log_online.html', locals(), context_instance=RequestContext(request)) @@ -75,8 +80,12 @@ def log_list_offline(request): contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) elif is_common_user(request): - posts = Log.objects.filter(is_finished=1).filter(user=username).order_by('-start_time') - + if keyword: + posts = Log.objects.filter(user=username).filter(Q(user__contains=keyword) | Q(host__contains=keyword))\ + .filter(is_finished=1).order_by('-start_time') + else: + posts = Log.objects.filter(is_finished=1).filter(user=username).order_by('-start_time') + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) return render_to_response('jlog/log_offline.html', locals(), context_instance=RequestContext(request)) @@ -105,6 +114,8 @@ def log_search(request): env = request.GET.get('env') dept_id = get_user_dept(request) dept_name = DEPT.objects.get(id=dept_id).name + user_id = request.session.get('user_id') + username = User.objects.get(id=user_id).username if is_super_user(request): if env == 'online': posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \ @@ -122,4 +133,13 @@ def log_search(request): posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \ .filter(is_finished=1).filter(dept_name=dept_name).order_by('-start_time') contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + + elif is_common_user(request): + if env == 'online': + posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \ + .filter(is_finished=0).filter(user=username).order_by('-start_time') + elif env == 'offline': + posts = contact_list = Log.objects.filter(Q(user__contains=keyword) | Q(host__contains=keyword)) \ + .filter(is_finished=1).filter(user=username).order_by('-start_time') + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) return render_to_response('jlog/log_search.html', locals(), context_instance=RequestContext(request)) diff --git a/jperm/models.py b/jperm/models.py index cfa2b3f79..519657c76 100644 --- a/jperm/models.py +++ b/jperm/models.py @@ -1,3 +1,7 @@ +import datetime + +from uuidfield import UUIDField + from django.db import models from juser.models import UserGroup, DEPT from jasset.models import Asset, BisGroup @@ -29,4 +33,20 @@ class SudoPerm(models.Model): comment = models.CharField(max_length=30, null=True, blank=True) def __unicode__(self): - return self.user_group.name \ No newline at end of file + return self.user_group.name return self.name + + +class Apply(models.Model): + uuid = UUIDField(auto=True) + applyer = models.CharField(max_length=20) + approver = models.CharField(max_length=20) + dept = models.CharField(max_length=20) + bisgroup = models.CharField(max_length=500) + asset = models.CharField(max_length=500) + comment = models.TextField(blank=True, null=True) + status = models.IntegerField(max_length=2) + date_add = models.DateTimeField(default=datetime.datetime.now(), null=True) + date_end = models.DateTimeField(null=True) + + def __unicode__(self): + return self.applyer diff --git a/jperm/urls.py b/jperm/urls.py index cbbc2ac6e..783e00f28 100644 --- a/jperm/urls.py +++ b/jperm/urls.py @@ -23,4 +23,7 @@ urlpatterns = patterns('jperm.views', (r'^cmd_list/$', 'cmd_list'), (r'^cmd_del/$', 'cmd_del'), (r'^cmd_edit/$', 'cmd_edit'), + (r'^apply/$', 'perm_apply'), + (r'^apply_show/(\w+)/$', 'perm_apply_log'), + (r'^apply_exec/$', 'perm_apply_exec'), ) diff --git a/jperm/views.py b/jperm/views.py index 6e623e949..7db75cc98 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -1,11 +1,18 @@ # coding: utf-8 +import sys +reload(sys) +sys.setdefaultencoding('utf8') +import datetime + + +from django.core.mail import send_mail from django.shortcuts import render_to_response from django.http import HttpResponseRedirect, HttpResponse from django.template import RequestContext from juser.models import User, UserGroup, DEPT from jasset.models import Asset, BisGroup -from jperm.models import Perm, SudoPerm, CmdGroup +from jperm.models import Perm, SudoPerm, CmdGroup, Apply from django.core.paginator import Paginator, EmptyPage, InvalidPage from django.db.models import Q from jumpserver.views import LDAP_ENABLE, ldap_conn, CONF, page_list_return, pages @@ -617,3 +624,110 @@ def cmd_del(request): if cmd_group: cmd_group[0].delete() return HttpResponseRedirect('/jperm/cmd_list/') + + +@require_login +def perm_apply(request): + header_title, path1, path2 = u'主机权限申请', u'权限管理', u'申请主机' + user_id = request.session.get('user_id') + username = User.objects.get(id=user_id).username + dept_id = get_user_dept(request) + deptname = DEPT.objects.get(id=dept_id).name + dept = DEPT.objects.get(id=dept_id) + posts = Asset.objects.filter(dept=dept) + egroup = dept.bisgroup_set.all() + dept_da = User.objects.filter(dept_id=dept_id, role='DA') + + if request.method == 'POST': + applyer = request.POST.get('applyer') + dept = request.POST.get('dept') + da = request.POST.get('da') + group = request.POST.getlist('group') + hosts = request.POST.getlist('hosts') + comment = request.POST.get('comment') + da = User.objects.get(id=da) + mail_address = da.email + mail_title = '%s - 权限申请' % username + # print da.username, applyer, group, hosts, datetime.datetime.now(), comment, url + group_lis = ', '.join(group) + hosts_lis = ', '.join(hosts) + time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + Apply.objects.create(applyer=applyer, dept=dept, bisgroup=group, asset=hosts, status=0, comment=comment) + uuid = Apply.objects.get(applyer=applyer, asset=hosts, comment=comment).uuid + url = "http://127.0.0.1:8000/jperm/apply_exec/?uuid=%s" % uuid + mail_msg = """ + Hi,%s: + 有新的权限申请, 详情如下: + 申请人: %s + 申请主机组: %s + 申请的主机: %s + 申请时间: %s + 申请说明: %s + 请及时审批, 审批完成后点击以下链接,告知各位。 + %s + """ % (da.username, applyer, group_lis, hosts_lis, time_now, comment, url) + + send_mail(mail_title, mail_msg, 'jkfunshion@fun.tv', [mail_address], fail_silently=False) + smg = "提交成功,已发邮件通知部门管理员。" + return render_to_response('jperm/perm_apply.html', locals(), context_instance=RequestContext(request)) + return render_to_response('jperm/perm_apply.html', locals(), context_instance=RequestContext(request)) + + +def perm_apply_exec(request): + uuid = request.GET.get('uuid') + p_apply = Apply.objects.filter(uuid=str(uuid)) + q_apply = Apply.objects.get(uuid=str(uuid)) + if p_apply: + user = User.objects.get(username=q_apply.applyer) + mail_address = user.email + time_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') + p_apply.update(status=1, date_end=time_now) + mail_title = '%s - 权限审批完成' % q_apply.applyer + mail_msg = """ + Hi,%s: + 您所申请的权限已由 %s 在 %s 审批完成, 请登录验证。 + """ % (q_apply.applyer, q_apply.approver, time_now) + send_mail(mail_title, mail_msg, 'jkfunshion@fun.tv', [mail_address], fail_silently=False) + return render_to_response('jperm/perm_apply_exec.html', locals(), context_instance=RequestContext(request)) + + +def get_apply_posts(request, status, username, dept_name, keyword=None): + if is_super_user(request): + if keyword: + posts = Apply.objects.filter(Q(applyer__contains=keyword) | Q(approver__contains=keyword)) \ + .filter(status=status).order_by('-date_add') + else: + posts = Apply.objects.filter(status=status).order_by('-date_add') + + elif is_group_admin(request): + if keyword: + posts = Apply.objects.filter(Q(applyer__contains=keyword) | Q(approver__contains=keyword)) \ + .filter(status=status).filter(dept=dept_name).order_by('-date_add') + else: + posts = Log.objects.filter(status=status).filter(dept=dept_name).order_by('-date_add') + + elif is_common_user(request): + if keyword: + posts = Apply.objects.filter(applyer=username).filter(status=status).filter(Q(applyer__contains=keyword) | + Q(asset__contains=keyword)).order_by('-date_add') + else: + posts = Apply.objects.filter(applyer=username).filter(status=status).order_by('-date_add') + return posts + + +def perm_apply_log(request, offset): + header_title, path1, path2 = u'权限申请记录', u'权限管理', u'申请记录' + keyword = request.GET.get('keyword') + dept_id = get_user_dept(request) + dept_name = DEPT.objects.get(id=dept_id).name + user_id = request.session.get('user_id') + username = User.objects.get(id=user_id).username + if offset == 'online': + posts = get_apply_posts(request, 0, username, dept_name, keyword) + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + return render_to_response('jperm/perm_log_online.html', locals(), context_instance=RequestContext(request)) + + elif offset == 'offline': + posts = get_apply_posts(request, 1, username, dept_name, keyword) + contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request) + return render_to_response('jperm/perm_log_offline.html', locals(), context_instance=RequestContext(request)) diff --git a/jumpserver.conf b/jumpserver.conf index a24c2190a..95250fbea 100644 --- a/jumpserver.conf +++ b/jumpserver.conf @@ -9,10 +9,10 @@ database = jumpserver [ldap] ldap_enable = 1 -host_url = ldap://127.0.0.1:389 -base_dn = dc=jumpserver, dc=org -root_dn = cn=admin,dc=jumpserver,dc=org -root_pw = secret234 +host_url = ldap://192.168.8.230:389 +base_dn = dc=fengxing, dc=com +root_dn = cn=admin,dc=fengxing,dc=com +root_pw = 123456 [websocket] web_socket_host = 127.0.0.1:3000 @@ -20,3 +20,9 @@ web_socket_host = 127.0.0.1:3000 [web] key = 88aaaf7ffe3c6c04 +[mail] +email_host = 'mail.funshion.com' +email_port = '25' +email_host_user = 'jkfunshion' +email_host_password = 'jkmail%' +email_use_tls = False \ No newline at end of file diff --git a/jumpserver/api.py b/jumpserver/api.py index 55f31945c..537843aed 100644 --- a/jumpserver/api.py +++ b/jumpserver/api.py @@ -299,8 +299,12 @@ def asset_perm_api(asset): return user_permed_list -def validate(request, user_group=None, user=None, asset_group=None, asset=None): +def validate(request, user_group=None, user=None, asset_group=None, asset=None, edept=None): dept = get_session_user_dept(request)[1] + if edept: + if dept.name != edept[0]: + return False + if user_group: dept_user_groups = dept.usergroup_set.all() user_groups = [] @@ -321,19 +325,24 @@ def validate(request, user_group=None, user=None, asset_group=None, asset=None): if asset_group: dept_asset_groups = dept.bisgroup_set.all() asset_groups = [] - for asset_group_id in asset_group: - asset_groups.extend(BisGroup.objects.filter(id=asset_group_id)) + for asset_group_name in dept_asset_groups: + asset_groups.extend(asset_group_name.name) - if not set(asset_groups).issubset(set(dept_asset_groups)): + if len(asset_groups) == 0: + return False + + if not set(asset_group).issubset(set(asset_groups)): return False if asset: dept_assets = dept.asset_set.all() - assets = [] - for asset_id in asset: - assets.extend(asset_id) + assets, eassets = [], [] + for asset_id in dept_assets: + eassets.append(int(asset_id.id)) + for i in asset: + assets.append(int(i)) - if not set(assets).issubset(dept_assets): + if not set(assets).issubset(eassets): return False return True \ No newline at end of file diff --git a/jumpserver/settings.py b/jumpserver/settings.py index 489577dc1..50b6ae0db 100644 --- a/jumpserver/settings.py +++ b/jumpserver/settings.py @@ -23,6 +23,13 @@ DB_USER = config.get('db', 'user') DB_PASSWORD = config.get('db', 'password') DB_DATABASE = config.get('db', 'database') +# mail config +EMAIL_HOST = 'mail.funshion.com' +EMAIL_PORT = '25' +EMAIL_HOST_USER = 'jkfunshion' +EMAIL_HOST_PASSWORD = 'jkmail%' +EMAIL_USE_TLS = False + # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/ @@ -121,3 +128,4 @@ USE_TZ = False STATIC_URL = '/static/' + diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index a0e1f5a34..55729b4ba 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -1,5 +1,7 @@ # coding: utf-8 +import re +import ast import time from django import template @@ -158,6 +160,11 @@ def group_type_to_str(type_name): return group_types.get(type_name) +@register.filter(name='ast_to_list') +def ast_to_list(lis): + return ast.literal_eval(lis) + + # @register.filter(name='perm_asset_count') # def perm_asset_count(user_id): # return len(perm_user_asset(user_id)) diff --git a/jumpserver/views.py b/jumpserver/views.py index 7f03b8f44..5f4f82226 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -6,6 +6,7 @@ from django.db.models import Count from django.shortcuts import render_to_response from django.template import RequestContext from jasset.models import IDC +from juser.models import DEPT from jumpserver.api import * @@ -82,13 +83,18 @@ def jasset_group_add(name, comment, jtype): smg = u'业务组%s添加成功' % name -def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_active, j_comment, j_user='', j_password=''): - groups = [] +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', 'SSH_KEY': 'S', 'PASSWORD': 'P', 'MAP': 'M'} + 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) @@ -110,6 +116,7 @@ def jasset_host_edit(j_id, j_ip, j_idc, j_port, j_type, j_group, j_active, j_com a.comment = j_comment a.save() a.bis_group = groups + a.dept = depts a.save() diff --git a/templates/jasset/group_add.html b/templates/jasset/group_add.html index 837c95f78..d3defd781 100644 --- a/templates/jasset/group_add.html +++ b/templates/jasset/group_add.html @@ -54,9 +54,9 @@