2015-01-07 10:35:09 +00:00
|
|
|
# coding:utf-8
|
2015-02-09 11:02:25 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
import ast
|
|
|
|
|
2015-02-09 11:02:25 +00:00
|
|
|
from django.db.models import Q
|
2015-01-05 10:21:34 +00:00
|
|
|
from django.template import RequestContext
|
|
|
|
from django.shortcuts import render_to_response
|
2015-01-07 10:35:09 +00:00
|
|
|
|
2015-04-18 05:51:47 +00:00
|
|
|
from jperm.models import Perm
|
2015-03-26 10:42:52 +00:00
|
|
|
from jumpserver.api import *
|
2014-12-22 09:18:51 +00:00
|
|
|
|
2015-01-19 02:09:16 +00:00
|
|
|
cryptor = PyCrypt(KEY)
|
|
|
|
|
2015-01-05 10:21:34 +00:00
|
|
|
|
2015-03-24 10:34:00 +00:00
|
|
|
class RaiseError(Exception):
|
|
|
|
pass
|
2015-01-05 10:21:34 +00:00
|
|
|
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
def my_render(template, data, request):
|
|
|
|
return render_to_response(template, data, context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
def httperror(request, emg):
|
|
|
|
message = emg
|
|
|
|
return render_to_response('error.html', locals(), context_instance=RequestContext(request))
|
|
|
|
|
|
|
|
|
|
|
|
def get_host_groups(groups):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 获取主机所属的组类 """
|
2015-04-13 13:25:43 +00:00
|
|
|
ret = []
|
|
|
|
for group_id in groups:
|
|
|
|
group = BisGroup.objects.filter(id=group_id)
|
|
|
|
if group:
|
|
|
|
group = group[0]
|
|
|
|
ret.append(group)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
def get_host_depts(depts):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 获取主机所属的部门类 """
|
2015-04-13 13:25:43 +00:00
|
|
|
ret = []
|
|
|
|
for dept_id in depts:
|
|
|
|
dept = DEPT.objects.filter(id=dept_id)
|
|
|
|
if dept:
|
|
|
|
dept = dept[0]
|
|
|
|
ret.append(dept)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
2015-04-16 11:03:02 +00:00
|
|
|
def db_host_insert(host_info, username='', password=''):
|
2015-04-13 13:25:43 +00:00
|
|
|
""" 添加主机时数据库操作函数 """
|
2015-04-16 11:03:02 +00:00
|
|
|
ip, port, idc, jtype, group, dept, active, comment = host_info
|
2015-04-13 13:25:43 +00:00
|
|
|
idc = IDC.objects.filter(id=idc)
|
|
|
|
if idc:
|
|
|
|
idc = idc[0]
|
2015-03-14 05:13:46 +00:00
|
|
|
if jtype == 'M':
|
2015-04-13 13:25:43 +00:00
|
|
|
password = cryptor.encrypt(password)
|
2015-02-05 10:53:57 +00:00
|
|
|
a = Asset(ip=ip, port=port,
|
|
|
|
login_type=jtype, idc=idc,
|
|
|
|
is_active=int(active),
|
|
|
|
comment=comment,
|
|
|
|
username=username,
|
|
|
|
password=password)
|
|
|
|
else:
|
|
|
|
a = Asset(ip=ip, port=port,
|
|
|
|
login_type=jtype, idc=idc,
|
|
|
|
is_active=int(active),
|
|
|
|
comment=comment)
|
|
|
|
a.save()
|
|
|
|
|
|
|
|
all_group = BisGroup.objects.get(name='ALL')
|
2015-04-13 13:25:43 +00:00
|
|
|
groups = get_host_groups(group)
|
2015-03-17 06:22:35 +00:00
|
|
|
groups.append(all_group)
|
2015-02-05 10:53:57 +00:00
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
depts = get_host_depts(dept)
|
2015-03-14 09:54:27 +00:00
|
|
|
|
2015-02-05 10:53:57 +00:00
|
|
|
a.bis_group = groups
|
2015-03-14 09:54:27 +00:00
|
|
|
a.dept = depts
|
2015-02-05 10:53:57 +00:00
|
|
|
a.save()
|
2015-04-16 11:03:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
def db_host_update(host_info, username='', password=''):
|
2015-04-13 13:25:43 +00:00
|
|
|
""" 修改主机时数据库操作函数 """
|
2015-04-16 11:03:02 +00:00
|
|
|
ip, port, idc, jtype, group, dept, active, comment, host = host_info
|
2015-04-13 13:25:43 +00:00
|
|
|
idc = IDC.objects.filter(id=idc)
|
|
|
|
if idc:
|
|
|
|
idc = idc[0]
|
|
|
|
groups = get_host_groups(group)
|
|
|
|
depts = get_host_depts(dept)
|
|
|
|
host.ip = ip
|
|
|
|
host.port = port
|
|
|
|
host.login_type = jtype
|
|
|
|
host.idc = idc
|
|
|
|
host.is_active = int(active)
|
|
|
|
host.comment = comment
|
2015-02-05 10:53:57 +00:00
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
if jtype == 'M':
|
|
|
|
if password != host.password:
|
|
|
|
password = cryptor.encrypt(password)
|
|
|
|
host.password = password
|
|
|
|
host.username = username
|
|
|
|
host.password = password
|
|
|
|
host.save()
|
|
|
|
host.bis_group = groups
|
|
|
|
host.dept = depts
|
|
|
|
host.save()
|
|
|
|
|
|
|
|
|
2015-04-16 11:03:02 +00:00
|
|
|
def batch_host_edit(host_info, j_user='', j_password=''):
|
2015-04-13 13:25:43 +00:00
|
|
|
""" 批量修改主机函数 """
|
2015-04-16 11:03:02 +00:00
|
|
|
j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment = host_info
|
2015-04-05 14:52:37 +00:00
|
|
|
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)
|
|
|
|
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':
|
2015-04-13 13:25:43 +00:00
|
|
|
if a.password != j_password:
|
|
|
|
j_password = cryptor.decrypt(j_password)
|
2015-04-05 14:52:37 +00:00
|
|
|
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()
|
|
|
|
|
2015-04-07 11:15:45 +00:00
|
|
|
|
2015-04-16 11:03:02 +00:00
|
|
|
def db_host_delete(request, host_id):
|
|
|
|
""" 删除主机操作 """
|
|
|
|
if is_group_admin(request) and not validate(request, asset=[host_id]):
|
|
|
|
return httperror(request, '删除失败, 您无权删除!')
|
|
|
|
|
|
|
|
asset = Asset.objects.filter(id=host_id)
|
|
|
|
if asset:
|
|
|
|
asset.delete()
|
|
|
|
else:
|
|
|
|
return httperror(request, '删除失败, 没有此主机!')
|
|
|
|
|
|
|
|
|
|
|
|
def db_idc_delete(request, idc_id):
|
2015-04-18 04:30:45 +00:00
|
|
|
""" 删除IDC操作 """
|
2015-04-16 11:03:02 +00:00
|
|
|
if idc_id == 1:
|
|
|
|
return httperror(request, '删除失败, 默认IDC不能删除!')
|
|
|
|
|
|
|
|
default_idc = IDC.objects.get(id=1)
|
|
|
|
|
|
|
|
idc = IDC.objects.filter(id=idc_id)
|
|
|
|
if idc:
|
|
|
|
idc_class = idc.first()
|
|
|
|
idc_class.asset_set.update(idc=default_idc)
|
|
|
|
idc.delete()
|
|
|
|
else:
|
|
|
|
return httperror(request, '删除失败, 没有这个IDC!')
|
|
|
|
|
|
|
|
|
2015-03-18 10:05:46 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def host_add(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 添加主机 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'添加主机', u'资产管理', u'添加主机'
|
2015-04-13 13:25:43 +00:00
|
|
|
login_types = {'L': 'LDAP', 'M': 'MAP'}
|
2015-03-21 04:58:29 +00:00
|
|
|
eidc = IDC.objects.exclude(name='ALL')
|
2015-03-18 10:05:46 +00:00
|
|
|
if is_super_user(request):
|
|
|
|
edept = DEPT.objects.all()
|
2015-03-19 10:32:10 +00:00
|
|
|
egroup = BisGroup.objects.exclude(name='ALL')
|
2015-03-18 10:05:46 +00:00
|
|
|
elif is_group_admin(request):
|
2015-04-13 13:25:43 +00:00
|
|
|
dept = get_session_user_info(request)[5]
|
|
|
|
egroup = dept.bisgroup_set.all()
|
2015-03-24 10:34:00 +00:00
|
|
|
|
2015-01-05 10:21:34 +00:00
|
|
|
if request.method == 'POST':
|
2015-01-07 10:35:09 +00:00
|
|
|
j_ip = request.POST.get('j_ip')
|
|
|
|
j_idc = request.POST.get('j_idc')
|
|
|
|
j_port = request.POST.get('j_port')
|
|
|
|
j_type = request.POST.get('j_type')
|
|
|
|
j_group = request.POST.getlist('j_group')
|
|
|
|
j_active = request.POST.get('j_active')
|
|
|
|
j_comment = request.POST.get('j_comment')
|
2015-03-14 09:54:27 +00:00
|
|
|
j_dept = request.POST.getlist('j_dept')
|
2015-04-16 11:03:02 +00:00
|
|
|
|
|
|
|
host_info = [j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment]
|
2015-04-13 13:25:43 +00:00
|
|
|
if is_group_admin(request) and not verify(request, asset_group=j_group, edept=j_dept):
|
2015-04-16 11:03:02 +00:00
|
|
|
return httperror(request, u'添加失败,您无权操作!')
|
2015-03-24 10:34:00 +00:00
|
|
|
|
2015-01-07 10:35:09 +00:00
|
|
|
if Asset.objects.filter(ip=str(j_ip)):
|
2015-03-04 10:33:38 +00:00
|
|
|
emg = u'该IP %s 已存在!' % j_ip
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_add.html', locals(), request)
|
2015-01-19 02:09:16 +00:00
|
|
|
if j_type == 'M':
|
2015-01-11 11:27:21 +00:00
|
|
|
j_user = request.POST.get('j_user')
|
2015-04-13 13:25:43 +00:00
|
|
|
j_password = request.POST.get('j_password', '')
|
2015-04-16 11:03:02 +00:00
|
|
|
db_host_insert(host_info, j_user, j_password)
|
2015-01-07 10:35:09 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
db_host_insert(host_info)
|
2015-03-04 10:33:38 +00:00
|
|
|
smg = u'主机 %s 添加成功' % j_ip
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_add.html', locals(), request)
|
2015-01-07 10:35:09 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def host_add_batch(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 批量添加主机 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'批量添加主机', u'资产管理', u'批量添加主机'
|
2015-03-19 10:32:10 +00:00
|
|
|
login_types = {'LDAP': 'L', 'MAP': 'M'}
|
2015-04-14 09:25:56 +00:00
|
|
|
active_types = {'激活': 1, '禁用': 0}
|
2015-03-19 10:32:10 +00:00
|
|
|
dept_id = get_user_dept(request)
|
2015-02-01 15:00:23 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
multi_hosts = request.POST.get('j_multi').split('\n')
|
|
|
|
for host in multi_hosts:
|
|
|
|
if host == '':
|
|
|
|
break
|
2015-03-19 10:32:10 +00:00
|
|
|
j_ip, j_port, j_type, j_idc, j_groups, j_depts, j_active, j_comment = host.split()
|
2015-02-01 15:00:23 +00:00
|
|
|
j_type = login_types[j_type]
|
2015-04-14 09:25:56 +00:00
|
|
|
j_active = active_types[str(j_active)]
|
2015-03-19 10:32:10 +00:00
|
|
|
j_group = ast.literal_eval(j_groups)
|
|
|
|
j_dept = ast.literal_eval(j_depts)
|
2015-02-09 11:02:25 +00:00
|
|
|
|
2015-04-14 09:25:56 +00:00
|
|
|
idc = IDC.objects.filter(name=j_idc)
|
|
|
|
if idc:
|
|
|
|
j_idc = idc[0].id
|
2015-04-16 11:03:02 +00:00
|
|
|
else:
|
|
|
|
return httperror(request, '添加失败, 没有%s这个IDC' % j_idc)
|
2015-04-14 09:25:56 +00:00
|
|
|
|
|
|
|
group_ids, dept_ids = [], []
|
|
|
|
for group_name in j_group:
|
|
|
|
group = BisGroup.objects.filter(name=group_name)
|
|
|
|
if group:
|
|
|
|
group_id = group[0].id
|
2015-04-16 11:03:02 +00:00
|
|
|
else:
|
|
|
|
return httperror(request, '添加失败, 没有%s这个主机组' % group_name)
|
2015-04-14 09:25:56 +00:00
|
|
|
group_ids.append(group_id)
|
|
|
|
|
|
|
|
for dept_name in j_dept:
|
|
|
|
dept = DEPT.objects.filter(name=dept_name)
|
|
|
|
if dept:
|
|
|
|
dept_id = dept[0].id
|
2015-04-16 11:03:02 +00:00
|
|
|
else:
|
|
|
|
return httperror(request, '添加失败, 没有%s这个部门' % dept_name)
|
2015-04-14 09:25:56 +00:00
|
|
|
dept_ids.append(dept_id)
|
|
|
|
|
|
|
|
if is_group_admin(request) and not verify(request, asset_group=group_ids, edept=dept_ids):
|
2015-04-16 11:03:02 +00:00
|
|
|
return httperror(request, '添加失败, 没有%s这个主机组' % group_name)
|
2015-04-14 09:25:56 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
if Asset.objects.filter(ip=str(j_ip)):
|
2015-04-16 11:03:02 +00:00
|
|
|
return httperror(request, '添加失败, 改IP%s已存在' % j_ip)
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-04-16 11:03:02 +00:00
|
|
|
host_info = [j_ip, j_port, j_idc, j_type, group_ids, dept_ids, j_active, j_comment]
|
|
|
|
db_host_insert(host_info)
|
2015-02-05 10:53:57 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
smg = u'批量添加添加成功'
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_add_multi.html', locals(), request)
|
2015-02-05 10:53:57 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_add_multi.html', locals(), request)
|
2015-02-01 15:00:23 +00:00
|
|
|
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def host_edit_batch(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 批量修改主机 """
|
2015-01-31 04:55:10 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
len_table = request.POST.get('len_table')
|
|
|
|
for i in range(int(len_table)):
|
2015-03-04 10:33:38 +00:00
|
|
|
j_id = "editable[" + str(i) + "][j_id]"
|
|
|
|
j_ip = "editable[" + str(i) + "][j_ip]"
|
|
|
|
j_port = "editable[" + str(i) + "][j_port]"
|
2015-03-25 11:02:14 +00:00
|
|
|
j_dept = "editable[" + str(i) + "][j_dept]"
|
2015-03-04 10:33:38 +00:00
|
|
|
j_idc = "editable[" + str(i) + "][j_idc]"
|
|
|
|
j_type = "editable[" + str(i) + "][j_type]"
|
|
|
|
j_group = "editable[" + str(i) + "][j_group]"
|
|
|
|
j_active = "editable[" + str(i) + "][j_active]"
|
|
|
|
j_comment = "editable[" + str(i) + "][j_comment]"
|
2015-01-31 04:55:10 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
j_id = request.POST.get(j_id).strip()
|
2015-01-31 04:55:10 +00:00
|
|
|
j_ip = request.POST.get(j_ip).strip()
|
|
|
|
j_port = request.POST.get(j_port).strip()
|
2015-04-11 04:52:00 +00:00
|
|
|
j_dept = request.POST.getlist(j_dept)
|
2015-01-31 04:55:10 +00:00
|
|
|
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()
|
|
|
|
|
2015-04-16 11:03:02 +00:00
|
|
|
host_info = [j_id, j_ip, j_idc, j_port, j_type, j_group, j_dept, j_active, j_comment]
|
|
|
|
batch_host_edit(host_info)
|
2015-01-31 04:55:10 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return HttpResponseRedirect('/jasset/host_list/')
|
2015-01-31 04:55:10 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
|
2015-04-07 11:15:45 +00:00
|
|
|
@require_login
|
2015-04-11 04:52:00 +00:00
|
|
|
def host_edit_common_batch(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 普通用户批量修改主机别名 """
|
2015-04-18 04:30:45 +00:00
|
|
|
u = get_session_user_info(request)[2]
|
2015-04-07 11:15:45 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
len_table = request.POST.get('len_table')
|
|
|
|
for i in range(int(len_table)):
|
|
|
|
j_id = "editable[" + str(i) + "][j_id]"
|
|
|
|
j_alias = "editable[" + str(i) + "][j_alias]"
|
2015-04-18 04:30:45 +00:00
|
|
|
j_id = request.POST.get(j_id, '').strip()
|
|
|
|
j_alias = request.POST.get(j_alias, '').strip()
|
2015-04-07 11:15:45 +00:00
|
|
|
a = Asset.objects.get(id=j_id)
|
|
|
|
asset_alias = AssetAlias.objects.filter(user=u, host=a)
|
|
|
|
if asset_alias:
|
|
|
|
asset_alias = asset_alias[0]
|
|
|
|
asset_alias.alias = j_alias
|
|
|
|
asset_alias.save()
|
|
|
|
else:
|
|
|
|
AssetAlias.objects.create(user=u, host=a, alias=j_alias)
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_list_common.html')
|
2015-04-07 11:15:45 +00:00
|
|
|
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_login
|
2015-04-11 04:52:00 +00:00
|
|
|
def host_list(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 列出主机 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'查看主机', u'资产管理', u'查看主机'
|
2015-03-12 03:03:53 +00:00
|
|
|
keyword = request.GET.get('keyword', '')
|
2015-04-16 11:03:02 +00:00
|
|
|
dept_id = get_session_user_info(request)[3]
|
2015-03-18 10:05:46 +00:00
|
|
|
dept = DEPT.objects.get(id=dept_id)
|
2015-04-13 13:25:43 +00:00
|
|
|
did = request.GET.get('did', '')
|
|
|
|
gid = request.GET.get('gid', '')
|
2015-04-18 05:51:47 +00:00
|
|
|
user_id = get_session_user_info(request)[0]
|
2015-04-16 11:03:02 +00:00
|
|
|
|
2015-04-18 05:51:47 +00:00
|
|
|
post_all = Asset.objects.all().order_by('ip')
|
2015-04-16 11:03:02 +00:00
|
|
|
post_keyword_all = 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')
|
2015-04-05 14:52:37 +00:00
|
|
|
if did:
|
2015-04-18 05:51:47 +00:00
|
|
|
if is_common_user(request) or is_group_admin(request):
|
|
|
|
return httperror(request, u'您无权查看!')
|
|
|
|
|
2015-04-05 14:52:37 +00:00
|
|
|
dept = DEPT.objects.get(id=did)
|
|
|
|
posts = dept.asset_set.all()
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_list_nop.html', locals(), request)
|
2015-04-07 11:15:45 +00:00
|
|
|
|
2015-04-05 14:52:37 +00:00
|
|
|
elif gid:
|
2015-04-18 05:51:47 +00:00
|
|
|
if is_common_user(request):
|
|
|
|
return httperror(request, u'您无权查看!')
|
|
|
|
|
|
|
|
elif is_group_admin(request) and not verify(request, user_group=[gid]):
|
|
|
|
return httperror(request, u'您无权查看!')
|
2015-04-07 11:15:45 +00:00
|
|
|
|
|
|
|
posts = []
|
2015-04-18 05:51:47 +00:00
|
|
|
user_group = UserGroup.objects.filter(id=gid)
|
|
|
|
if user_group:
|
|
|
|
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))
|
|
|
|
else:
|
|
|
|
return httperror(request, u'没有这个小组!')
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_list_nop.html', locals(), request)
|
2015-03-19 10:32:10 +00:00
|
|
|
|
2015-04-05 14:52:37 +00:00
|
|
|
else:
|
|
|
|
if is_super_user(request):
|
|
|
|
if keyword:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_keyword_all
|
2015-04-05 14:52:37 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_all
|
2015-04-07 11:15:45 +00:00
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_list.html', locals(), request)
|
2015-04-05 14:52:37 +00:00
|
|
|
|
|
|
|
elif is_group_admin(request):
|
|
|
|
if keyword:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_keyword_all.filter(dept=dept)
|
2015-04-05 14:52:37 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_all.filter(dept=dept)
|
2015-04-05 14:52:37 +00:00
|
|
|
|
2015-04-07 11:15:45 +00:00
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_list.html', locals(), request)
|
2015-04-07 11:15:45 +00:00
|
|
|
|
2015-04-05 14:52:37 +00:00
|
|
|
elif is_common_user(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
user_id, username = get_session_user_info(request)[0:2]
|
2015-04-05 14:52:37 +00:00
|
|
|
posts = user_perm_asset_api(username)
|
2015-04-07 11:15:45 +00:00
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_list_common.html', locals(), request)
|
2015-01-19 02:09:16 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_admin
|
2015-01-19 02:09:16 +00:00
|
|
|
def host_del(request, offset):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 删除主机 """
|
2015-02-01 15:00:23 +00:00
|
|
|
if offset == 'multi':
|
|
|
|
len_list = request.POST.get("len_list")
|
|
|
|
for i in range(int(len_list)):
|
2015-03-04 10:33:38 +00:00
|
|
|
key = "id_list[" + str(i) + "]"
|
2015-04-16 11:03:02 +00:00
|
|
|
host_id = request.POST.get(key)
|
|
|
|
db_host_delete(request, host_id)
|
2015-02-01 15:00:23 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
host_id = int(offset)
|
|
|
|
db_host_delete(request, host_id)
|
|
|
|
|
2015-01-19 02:09:16 +00:00
|
|
|
return HttpResponseRedirect('/jasset/host_list/')
|
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
@require_super_user
|
2015-02-11 09:53:17 +00:00
|
|
|
def host_edit(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 修改主机 """
|
2015-04-13 13:25:43 +00:00
|
|
|
header_title, path1, path2 = u'修改主机', u'资产管理', u'修改主机'
|
2015-01-19 02:09:16 +00:00
|
|
|
actives = {1: u'激活', 0: u'禁用'}
|
2015-03-14 09:54:27 +00:00
|
|
|
login_types = {'L': 'LDAP', 'M': 'MAP'}
|
2015-01-19 02:09:16 +00:00
|
|
|
eidc = IDC.objects.all()
|
2015-04-11 04:52:00 +00:00
|
|
|
egroup = BisGroup.objects.exclude(name='ALL')
|
2015-03-14 09:54:27 +00:00
|
|
|
edept = DEPT.objects.all()
|
2015-04-13 13:25:43 +00:00
|
|
|
host_id = request.GET.get('id', '')
|
|
|
|
post = Asset.objects.filter(id=int(host_id))
|
|
|
|
if post:
|
|
|
|
post = post[0]
|
|
|
|
else:
|
|
|
|
return httperror(request, '没有此主机!')
|
|
|
|
|
|
|
|
e_group = post.bis_group.all()
|
|
|
|
e_dept = post.dept.all()
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
j_ip = request.POST.get('j_ip', '')
|
|
|
|
j_idc = request.POST.get('j_idc', '')
|
|
|
|
j_port = request.POST.get('j_port', '')
|
|
|
|
j_type = request.POST.get('j_type', '')
|
|
|
|
j_dept = request.POST.getlist('j_dept', '')
|
|
|
|
j_group = request.POST.getlist('j_group', '')
|
|
|
|
j_active = request.POST.get('j_active', '')
|
|
|
|
j_comment = request.POST.get('j_comment', '')
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
host_info = [j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment, post]
|
2015-04-13 13:25:43 +00:00
|
|
|
if j_type == 'M':
|
|
|
|
j_user = request.POST.get('j_user')
|
|
|
|
j_password = request.POST.get('j_password')
|
2015-04-18 04:30:45 +00:00
|
|
|
db_host_update(host_info, j_user, j_password)
|
2015-04-13 13:25:43 +00:00
|
|
|
else:
|
2015-04-18 04:30:45 +00:00
|
|
|
db_host_update(host_info)
|
2015-04-13 13:25:43 +00:00
|
|
|
|
|
|
|
smg = u'主机 %s 修改成功' % j_ip
|
|
|
|
return HttpResponseRedirect('/jasset/host_detail/?id=%s' % host_id)
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_edit.html', locals(), request)
|
2015-04-13 13:25:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
@require_admin
|
|
|
|
def host_edit_adm(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 部门管理员修改主机 """
|
2015-04-13 13:25:43 +00:00
|
|
|
header_title, path1, path2 = u'修改主机', u'资产管理', u'修改主机'
|
|
|
|
actives = {1: u'激活', 0: u'禁用'}
|
|
|
|
login_types = {'L': 'LDAP', 'M': 'MAP'}
|
|
|
|
eidc = IDC.objects.all()
|
|
|
|
dept = get_session_user_info(request)[5]
|
|
|
|
egroup = BisGroup.objects.exclude(name='ALL').filter(dept=dept)
|
|
|
|
host_id = request.GET.get('id', '')
|
|
|
|
post = Asset.objects.filter(id=int(host_id))
|
|
|
|
if post:
|
|
|
|
post = post[0]
|
|
|
|
else:
|
|
|
|
return httperror(request, '没有此主机!')
|
|
|
|
|
|
|
|
e_group = post.bis_group.all()
|
|
|
|
|
2015-01-19 02:09:16 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
j_ip = request.POST.get('j_ip')
|
|
|
|
j_idc = request.POST.get('j_idc')
|
|
|
|
j_port = request.POST.get('j_port')
|
|
|
|
j_type = request.POST.get('j_type')
|
2015-03-14 09:54:27 +00:00
|
|
|
j_dept = request.POST.getlist('j_dept')
|
2015-01-19 02:09:16 +00:00
|
|
|
j_group = request.POST.getlist('j_group')
|
|
|
|
j_active = request.POST.get('j_active')
|
|
|
|
j_comment = request.POST.get('j_comment')
|
2015-03-25 11:02:14 +00:00
|
|
|
|
2015-04-16 11:03:02 +00:00
|
|
|
host_info = [j_ip, j_port, j_idc, j_type, j_group, j_dept, j_active, j_comment]
|
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
if not verify(request, asset_group=j_group, edept=j_dept):
|
2015-03-25 11:02:14 +00:00
|
|
|
emg = u'修改失败,您无权操作!'
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_edit.html', locals(), request)
|
2015-03-25 11:02:14 +00:00
|
|
|
|
2015-01-19 02:09:16 +00:00
|
|
|
if j_type == 'M':
|
|
|
|
j_user = request.POST.get('j_user')
|
2015-04-13 13:25:43 +00:00
|
|
|
j_password = request.POST.get('j_password')
|
2015-04-16 11:03:02 +00:00
|
|
|
db_host_update(host_info, j_user, j_password, post)
|
2015-01-19 02:09:16 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
db_host_update(host_info, post)
|
2015-04-13 13:25:43 +00:00
|
|
|
|
2015-03-04 10:33:38 +00:00
|
|
|
smg = u'主机 %s 修改成功' % j_ip
|
2015-04-13 13:25:43 +00:00
|
|
|
return HttpResponseRedirect('/jasset/host_detail/?id=%s' % host_id)
|
2015-01-19 02:09:16 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_edit.html', locals(), request)
|
2015-01-19 02:09:16 +00:00
|
|
|
|
2015-01-07 10:35:09 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_login
|
2015-04-11 04:52:00 +00:00
|
|
|
def host_detail(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 主机详情 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'主机详细信息', u'资产管理', u'主机详情'
|
2015-04-13 13:25:43 +00:00
|
|
|
host_id = request.GET.get('id', '')
|
|
|
|
post = Asset.objects.filter(id=host_id)
|
|
|
|
if not post:
|
|
|
|
return httperror(request, '没有此主机!')
|
|
|
|
post = post.first()
|
|
|
|
|
|
|
|
if is_group_admin(request) and not verify(request, asset=[host_id]):
|
|
|
|
return httperror(request, '您无权查看!')
|
|
|
|
|
|
|
|
elif is_common_user(request):
|
2015-04-14 09:25:56 +00:00
|
|
|
username = get_session_user_info(request)[1]
|
2015-04-13 13:25:43 +00:00
|
|
|
user_permed_hosts = user_perm_asset_api(username)
|
|
|
|
if post not in user_permed_hosts:
|
|
|
|
return httperror(request, '您无权查看!')
|
|
|
|
else:
|
|
|
|
log_all = Log.objects.filter(host=post.ip)
|
|
|
|
log, log_more = log_all[:10], log_all[10:]
|
|
|
|
user_permed_list = asset_perm_api(post)
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_detail.html', locals(), request)
|
2015-01-07 10:35:09 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_super_user
|
2015-04-11 04:52:00 +00:00
|
|
|
def idc_add(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 添加IDC """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'添加IDC', u'资产管理', u'添加IDC'
|
2015-01-11 11:27:21 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
j_idc = request.POST.get('j_idc')
|
|
|
|
j_comment = request.POST.get('j_comment')
|
|
|
|
if IDC.objects.filter(name=j_idc):
|
|
|
|
emg = u'该IDC已存在!'
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/idc_add.html', locals(), request)
|
2015-01-11 11:27:21 +00:00
|
|
|
else:
|
2015-03-04 10:33:38 +00:00
|
|
|
smg = u'IDC:%s添加成功' % j_idc
|
2015-01-11 11:27:21 +00:00
|
|
|
IDC.objects.create(name=j_idc, comment=j_comment)
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/idc_add.html', locals(), request)
|
2015-01-07 10:35:09 +00:00
|
|
|
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def idc_list(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 列出IDC """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'查看IDC', u'资产管理', u'查看IDC'
|
2015-04-11 04:52:00 +00:00
|
|
|
dept_id = get_user_dept(request)
|
|
|
|
dept = DEPT.objects.get(id=dept_id)
|
2015-03-12 10:54:26 +00:00
|
|
|
keyword = request.GET.get('keyword', '')
|
|
|
|
if keyword:
|
|
|
|
posts = IDC.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword))
|
|
|
|
else:
|
2015-04-09 03:09:18 +00:00
|
|
|
posts = IDC.objects.exclude(name='ALL').order_by('id')
|
2015-03-07 09:37:40 +00:00
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/idc_list.html', locals(), request)
|
2015-01-11 11:27:21 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_super_user
|
2015-04-11 04:52:00 +00:00
|
|
|
def idc_edit(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 修改IDC """
|
2015-03-12 03:03:53 +00:00
|
|
|
header_title, path1, path2 = u'编辑IDC', u'资产管理', u'编辑IDC'
|
2015-04-16 11:03:02 +00:00
|
|
|
idc_id = request.GET.get('id', '')
|
2015-04-11 11:56:53 +00:00
|
|
|
idc = IDC.objects.filter(id=idc_id)
|
2015-04-18 04:30:45 +00:00
|
|
|
if int(idc_id) == 1:
|
|
|
|
return httperror(request, u'默认IDC不能编辑!')
|
2015-04-11 11:56:53 +00:00
|
|
|
if idc:
|
|
|
|
idc = idc[0]
|
2015-04-18 04:30:45 +00:00
|
|
|
default = IDC.objects.get(id=1).asset_set.all()
|
2015-04-11 11:56:53 +00:00
|
|
|
eposts = Asset.objects.filter(idc=idc).order_by('ip')
|
|
|
|
posts = [g for g in default if g not in eposts]
|
|
|
|
else:
|
2015-04-18 04:30:45 +00:00
|
|
|
return httperror(request, u'此IDC不存在')
|
2015-04-11 11:56:53 +00:00
|
|
|
|
2015-03-12 03:03:53 +00:00
|
|
|
if request.method == 'POST':
|
2015-04-11 11:56:53 +00:00
|
|
|
idc_id = request.POST.get('id')
|
|
|
|
j_idc = request.POST.get('j_idc')
|
2015-03-12 03:03:53 +00:00
|
|
|
j_hosts = request.POST.getlist('j_hosts')
|
|
|
|
j_comment = request.POST.get('j_comment')
|
|
|
|
idc_default = request.POST.getlist('idc_default')
|
|
|
|
|
2015-04-11 11:56:53 +00:00
|
|
|
idc = IDC.objects.filter(id=idc_id)
|
|
|
|
if idc:
|
|
|
|
idc.update(name=j_idc, comment=j_comment)
|
2015-04-13 13:25:43 +00:00
|
|
|
for host_id in j_hosts:
|
|
|
|
Asset.objects.filter(id=host_id).update(idc=idc[0])
|
2015-04-11 11:56:53 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
i = IDC.objects.get(id=1)
|
2015-04-11 11:56:53 +00:00
|
|
|
for host in idc_default:
|
2015-04-13 13:25:43 +00:00
|
|
|
g = Asset.objects.filter(id=host).update(idc=i)
|
2015-04-11 11:56:53 +00:00
|
|
|
else:
|
2015-04-18 04:30:45 +00:00
|
|
|
return httperror(request, u'此IDC不存在')
|
2015-03-12 03:03:53 +00:00
|
|
|
|
2015-04-11 11:56:53 +00:00
|
|
|
return HttpResponseRedirect('/jasset/idc_list/?id=%s' % idc_id)
|
2015-03-12 03:03:53 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/idc_edit.html', locals(), request)
|
2015-03-12 03:03:53 +00:00
|
|
|
|
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
@require_admin
|
|
|
|
def idc_detail(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" IDC详情 """
|
2015-04-13 13:25:43 +00:00
|
|
|
header_title, path1, path2 = u'IDC详情', u'资产管理', u'IDC详情'
|
|
|
|
login_types = {'L': 'LDAP', 'M': 'MAP'}
|
2015-04-16 11:03:02 +00:00
|
|
|
idc_id = request.GET.get('id', '')
|
2015-04-13 13:25:43 +00:00
|
|
|
idc_filter = IDC.objects.filter(id=idc_id)
|
|
|
|
if idc_filter:
|
|
|
|
idc = idc_filter[0]
|
|
|
|
else:
|
|
|
|
return httperror(request, '没有此IDC')
|
2015-04-16 11:03:02 +00:00
|
|
|
dept = get_session_user_info(request)[5]
|
2015-04-13 13:25:43 +00:00
|
|
|
if is_super_user(request):
|
|
|
|
posts = Asset.objects.filter(idc=idc).order_by('ip')
|
|
|
|
elif is_group_admin(request):
|
|
|
|
posts = Asset.objects.filter(idc=idc, dept=dept).order_by('ip')
|
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/idc_detail.html', locals(), request)
|
2015-04-13 13:25:43 +00:00
|
|
|
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_super_user
|
2015-04-11 11:56:53 +00:00
|
|
|
def idc_del(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 删除IDC """
|
2015-04-11 11:56:53 +00:00
|
|
|
offset = request.GET.get('id', '')
|
2015-03-07 10:37:23 +00:00
|
|
|
if offset == 'multi':
|
|
|
|
len_list = request.POST.get("len_list")
|
|
|
|
for i in range(int(len_list)):
|
|
|
|
key = "id_list[" + str(i) + "]"
|
2015-04-16 11:03:02 +00:00
|
|
|
idc_id = request.POST.get(key)
|
|
|
|
db_idc_delete(request, idc_id)
|
2015-03-07 10:37:23 +00:00
|
|
|
else:
|
2015-04-16 11:03:02 +00:00
|
|
|
db_idc_delete(request, int(offset))
|
2015-01-19 02:09:16 +00:00
|
|
|
return HttpResponseRedirect('/jasset/idc_list/')
|
2015-01-11 11:27:21 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-03-18 10:05:46 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def group_add(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 添加主机组 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'添加主机组', u'资产管理', u'添加主机组'
|
2015-03-18 10:05:46 +00:00
|
|
|
if is_super_user(request):
|
|
|
|
posts = Asset.objects.all()
|
|
|
|
edept = DEPT.objects.all()
|
|
|
|
elif is_group_admin(request):
|
|
|
|
dept_id = get_user_dept(request)
|
|
|
|
dept = DEPT.objects.get(id=dept_id)
|
|
|
|
posts = Asset.objects.filter(dept=dept)
|
2015-04-14 09:25:56 +00:00
|
|
|
edept = get_session_user_info(request)[5]
|
2015-04-13 13:25:43 +00:00
|
|
|
|
2015-01-11 11:27:21 +00:00
|
|
|
if request.method == 'POST':
|
2015-04-13 13:25:43 +00:00
|
|
|
j_group = request.POST.get('j_group', '')
|
|
|
|
j_dept = request.POST.get('j_dept', '')
|
|
|
|
j_hosts = request.POST.getlist('j_hosts', '')
|
|
|
|
j_comment = request.POST.get('j_comment', '')
|
2015-01-11 11:27:21 +00:00
|
|
|
|
2015-04-13 13:25:43 +00:00
|
|
|
try:
|
|
|
|
if is_group_admin(request) and not verify(request, asset=j_hosts, edept=[j_dept]):
|
|
|
|
emg = u'添加失败, 您无权操作!'
|
|
|
|
raise RaiseError
|
|
|
|
|
|
|
|
elif BisGroup.objects.filter(name=j_group):
|
|
|
|
emg = u'添加失败, 该主机组已存在!'
|
|
|
|
raise RaiseError
|
|
|
|
|
|
|
|
except RaiseError:
|
|
|
|
pass
|
2015-03-25 11:02:14 +00:00
|
|
|
|
2015-01-11 11:27:21 +00:00
|
|
|
else:
|
2015-04-13 13:25:43 +00:00
|
|
|
j_dept = DEPT.objects.filter(id=j_dept).first()
|
|
|
|
group = BisGroup.objects.create(name=j_group, dept=j_dept, comment=j_comment)
|
2015-02-03 14:45:51 +00:00
|
|
|
for host in j_hosts:
|
|
|
|
g = Asset.objects.get(id=host)
|
|
|
|
group.asset_set.add(g)
|
2015-04-13 13:25:43 +00:00
|
|
|
smg = u'主机组 %s 添加成功' % j_group
|
2015-01-11 11:27:21 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/group_add.html', locals(), request)
|
2015-01-11 11:27:21 +00:00
|
|
|
|
|
|
|
|
2015-03-18 10:05:46 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def group_list(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 列出主机组 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'查看主机组', u'资产管理', u'查看主机组'
|
2015-03-18 10:05:46 +00:00
|
|
|
dept_id = get_user_dept(request)
|
|
|
|
dept = DEPT.objects.get(id=dept_id)
|
2015-03-12 10:54:26 +00:00
|
|
|
keyword = request.GET.get('keyword', '')
|
2015-04-05 14:52:37 +00:00
|
|
|
gid = request.GET.get('gid')
|
2015-04-07 11:15:45 +00:00
|
|
|
sid = request.GET.get('sid')
|
2015-04-05 14:52:37 +00:00
|
|
|
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)
|
2015-04-07 11:15:45 +00:00
|
|
|
|
|
|
|
elif sid:
|
|
|
|
posts = []
|
|
|
|
user_group = UserGroup.objects.get(id=sid)
|
|
|
|
perms = Perm.objects.filter(user_group=user_group)
|
|
|
|
for perm in perms:
|
|
|
|
posts.append(perm.asset_group)
|
|
|
|
|
2015-04-05 14:52:37 +00:00
|
|
|
else:
|
|
|
|
if is_super_user(request):
|
|
|
|
if keyword:
|
2015-04-07 11:15:45 +00:00
|
|
|
posts = BisGroup.objects.exclude(name='ALL').filter(
|
|
|
|
Q(name__contains=keyword) | Q(comment__contains=keyword))
|
2015-04-05 14:52:37 +00:00
|
|
|
else:
|
|
|
|
posts = BisGroup.objects.exclude(name='ALL').order_by('id')
|
|
|
|
elif is_group_admin(request):
|
|
|
|
if keyword:
|
2015-04-07 11:15:45 +00:00
|
|
|
posts = BisGroup.objects.filter(Q(name__contains=keyword) | Q(comment__contains=keyword)).filter(
|
|
|
|
dept=dept)
|
2015-04-05 14:52:37 +00:00
|
|
|
else:
|
|
|
|
posts = BisGroup.objects.filter(dept=dept).order_by('id')
|
2015-03-07 09:37:40 +00:00
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/group_list.html', locals(), request)
|
2015-01-19 02:09:16 +00:00
|
|
|
|
2015-02-01 15:00:23 +00:00
|
|
|
|
2015-03-18 10:05:46 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def group_edit(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 修改主机组 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'编辑主机组', u'资产管理', u'编辑主机组'
|
2015-04-13 13:25:43 +00:00
|
|
|
group_id = request.GET.get('id', '')
|
2015-02-09 11:02:25 +00:00
|
|
|
group = BisGroup.objects.get(id=group_id)
|
2015-04-14 09:25:56 +00:00
|
|
|
host_all = Asset.objects.all()
|
2015-03-18 10:05:46 +00:00
|
|
|
dept_id = get_user_dept(request)
|
2015-04-14 09:25:56 +00:00
|
|
|
eposts = Asset.objects.filter(bis_group=group)
|
2015-03-19 10:32:10 +00:00
|
|
|
|
2015-03-18 10:05:46 +00:00
|
|
|
if is_super_user(request):
|
2015-03-19 10:32:10 +00:00
|
|
|
edept = DEPT.objects.all()
|
2015-04-14 09:25:56 +00:00
|
|
|
posts = [g for g in host_all if g not in eposts]
|
2015-03-19 10:32:10 +00:00
|
|
|
|
2015-03-18 10:05:46 +00:00
|
|
|
elif is_group_admin(request):
|
2015-04-14 09:25:56 +00:00
|
|
|
if not verify(request, asset_group=[group_id]):
|
|
|
|
return httperror(request, '编辑失败, 您无权操作!')
|
2015-03-18 10:05:46 +00:00
|
|
|
dept = DEPT.objects.get(id=dept_id)
|
|
|
|
all_dept = Asset.objects.filter(dept=dept)
|
|
|
|
posts = [g for g in all_dept if g not in eposts]
|
2015-03-19 10:32:10 +00:00
|
|
|
|
2015-02-03 14:45:51 +00:00
|
|
|
if request.method == 'POST':
|
2015-04-13 13:25:43 +00:00
|
|
|
j_group = request.POST.get('j_group', '')
|
|
|
|
j_hosts = request.POST.getlist('j_hosts', '')
|
|
|
|
j_dept = request.POST.get('j_dept', '')
|
|
|
|
j_comment = request.POST.get('j_comment', '')
|
|
|
|
|
|
|
|
j_dept = DEPT.objects.filter(id=int(j_dept))
|
|
|
|
j_dept = j_dept[0]
|
2015-02-03 14:45:51 +00:00
|
|
|
|
|
|
|
group.asset_set.clear()
|
|
|
|
for host in j_hosts:
|
|
|
|
g = Asset.objects.get(id=host)
|
|
|
|
group.asset_set.add(g)
|
2015-04-13 13:25:43 +00:00
|
|
|
BisGroup.objects.filter(id=group_id).update(name=j_group, dept=j_dept, comment=j_comment)
|
2015-03-04 10:33:38 +00:00
|
|
|
smg = u'主机组%s修改成功' % j_group
|
2015-04-13 13:25:43 +00:00
|
|
|
return HttpResponseRedirect('/jasset/group_list')
|
2015-02-03 14:45:51 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/group_edit.html', locals(), request)
|
2015-02-03 14:45:51 +00:00
|
|
|
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_admin
|
2015-04-11 04:52:00 +00:00
|
|
|
def group_detail(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 主机组详情 """
|
2015-03-04 15:14:07 +00:00
|
|
|
header_title, path1, path2 = u'主机组详情', u'资产管理', u'主机组详情'
|
2015-04-14 09:25:56 +00:00
|
|
|
login_types = {'L': 'LDAP', 'M': 'MAP'}
|
|
|
|
dept = get_session_user_info(request)[5]
|
|
|
|
group_id = request.GET.get('id', '')
|
|
|
|
group = BisGroup.objects.get(id=group_id)
|
2015-03-19 10:32:10 +00:00
|
|
|
if is_super_user(request):
|
2015-04-14 09:25:56 +00:00
|
|
|
posts = Asset.objects.filter(bis_group=group).order_by('ip')
|
2015-03-05 10:46:12 +00:00
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
elif is_group_admin(request):
|
2015-04-14 09:25:56 +00:00
|
|
|
if not verify(request, asset_group=[group_id]):
|
2015-04-18 04:30:45 +00:00
|
|
|
return httperror(request, u'您无权查看!')
|
2015-04-14 09:25:56 +00:00
|
|
|
posts = Asset.objects.filter(bis_group=group).filter(dept=dept).order_by('ip')
|
|
|
|
|
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/group_detail.html', locals(), request)
|
2015-02-01 15:00:23 +00:00
|
|
|
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_admin
|
2015-04-14 09:25:56 +00:00
|
|
|
def group_del_host(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 主机组中剔除主机, 并不删除真实主机 """
|
2015-02-03 14:45:51 +00:00
|
|
|
if request.method == 'POST':
|
2015-04-14 09:25:56 +00:00
|
|
|
group_id = request.POST.get('group_id')
|
|
|
|
offset = request.GET.get('id', '')
|
|
|
|
group = BisGroup.objects.get(id=group_id)
|
2015-02-03 14:45:51 +00:00
|
|
|
if offset == 'group':
|
2015-04-14 09:25:56 +00:00
|
|
|
len_list = request.POST.get("len_list")
|
|
|
|
for i in range(int(len_list)):
|
|
|
|
key = "id_list[" + str(i) + "]"
|
|
|
|
jid = request.POST.get(key)
|
|
|
|
g = Asset.objects.get(id=jid)
|
2015-03-05 10:46:12 +00:00
|
|
|
group.asset_set.remove(g)
|
|
|
|
|
2015-04-14 09:25:56 +00:00
|
|
|
else:
|
|
|
|
offset = request.GET.get('id', '')
|
|
|
|
group_id = request.GET.get('gid', '')
|
|
|
|
group = BisGroup.objects.get(id=group_id)
|
|
|
|
g = Asset.objects.get(id=offset)
|
|
|
|
group.asset_set.remove(g)
|
|
|
|
|
|
|
|
return HttpResponseRedirect('/jasset/group_detail/?id=%s' % group.id)
|
2015-02-03 14:45:51 +00:00
|
|
|
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
@require_admin
|
2015-04-13 13:25:43 +00:00
|
|
|
def group_del(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 删除主机组 """
|
2015-04-13 13:25:43 +00:00
|
|
|
offset = request.GET.get('id', '')
|
2015-03-07 10:37:23 +00:00
|
|
|
if offset == 'multi':
|
|
|
|
len_list = request.POST.get("len_list")
|
|
|
|
for i in range(int(len_list)):
|
|
|
|
key = "id_list[" + str(i) + "]"
|
|
|
|
gid = request.POST.get(key)
|
2015-04-16 07:10:46 +00:00
|
|
|
if is_group_admin(request) and not verify(request, asset_group=[gid]):
|
2015-04-14 09:25:56 +00:00
|
|
|
return httperror(request, '删除失败, 您无权删除!')
|
2015-03-07 10:37:23 +00:00
|
|
|
BisGroup.objects.filter(id=gid).delete()
|
|
|
|
else:
|
|
|
|
gid = int(offset)
|
2015-04-16 07:10:46 +00:00
|
|
|
if is_group_admin(request) and not verify(request, asset_group=[gid]):
|
2015-04-14 09:25:56 +00:00
|
|
|
return httperror(request, '删除失败, 您无权删除!')
|
2015-03-07 10:37:23 +00:00
|
|
|
BisGroup.objects.filter(id=gid).delete()
|
2015-04-11 04:52:00 +00:00
|
|
|
return HttpResponseRedirect('/jasset/group_list/')
|
2015-03-07 10:37:23 +00:00
|
|
|
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
@require_admin
|
2015-04-13 13:25:43 +00:00
|
|
|
def dept_host_ajax(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 添加主机组时, 部门联动主机异步 """
|
2015-04-13 13:25:43 +00:00
|
|
|
dept_id = request.GET.get('id', '')
|
|
|
|
if dept_id not in ['1', '2']:
|
|
|
|
dept = DEPT.objects.filter(id=dept_id)
|
|
|
|
if dept:
|
|
|
|
dept = dept[0]
|
|
|
|
hosts = dept.asset_set.all()
|
|
|
|
else:
|
|
|
|
hosts = Asset.objects.all()
|
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/dept_host_ajax.html', locals())
|
2015-04-13 13:25:43 +00:00
|
|
|
|
|
|
|
|
2015-04-07 11:15:45 +00:00
|
|
|
@require_login
|
2015-02-09 11:02:25 +00:00
|
|
|
def host_search(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
""" 搜索主机 """
|
2015-02-09 11:02:25 +00:00
|
|
|
keyword = request.GET.get('keyword')
|
2015-03-19 10:32:10 +00:00
|
|
|
login_types = {'L': 'LDAP', 'M': 'MAP'}
|
2015-04-16 11:03:02 +00:00
|
|
|
dept = get_session_user_info(request)[5]
|
|
|
|
post_all = 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')
|
2015-03-19 10:32:10 +00:00
|
|
|
if is_super_user(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_all
|
|
|
|
|
2015-03-19 10:32:10 +00:00
|
|
|
elif is_group_admin(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
posts = post_all.filter(dept=dept)
|
|
|
|
|
2015-04-07 11:15:45 +00:00
|
|
|
elif is_common_user(request):
|
2015-04-16 11:03:02 +00:00
|
|
|
username = get_session_user_info(request)[2]
|
2015-04-07 11:15:45 +00:00
|
|
|
post_perm = user_perm_asset_api(username)
|
|
|
|
posts = list(set(post_all) & set(post_perm))
|
2015-04-16 11:03:02 +00:00
|
|
|
|
2015-03-07 09:37:40 +00:00
|
|
|
contact_list, p, contacts, page_range, current_page, show_first, show_end = pages(posts, request)
|
2015-02-09 11:02:25 +00:00
|
|
|
|
2015-04-18 04:30:45 +00:00
|
|
|
return my_render('jasset/host_search.html', locals(), request)
|