From afbbad160439381ada1b884ba263e6849bc7ea11 Mon Sep 17 00:00:00 2001 From: "ibuler@qq.com" Date: Wed, 7 Oct 2015 17:16:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=88=E6=9D=83=E7=AE=A1=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jasset/models.py | 114 ++++++++-------- jasset/views.py | 9 +- jperm/perm_api.py | 70 +++++----- jperm/views.py | 116 +--------------- jumpserver/models.py | 1 + jumpserver/templatetags/mytags.py | 14 +- jumpserver/views.py | 10 +- juser/models.py | 192 +++++++++++++-------------- static/js/base.js | 46 ++++--- templates/jasset/asset_edit.html | 10 +- templates/jperm/perm_group_edit.html | 2 +- templates/jperm/perm_user_edit.html | 36 +---- templates/jperm/perm_user_list.html | 4 +- templates/setting.html | 22 +-- 14 files changed, 252 insertions(+), 394 deletions(-) diff --git a/jasset/models.py b/jasset/models.py index 01e91f99c..8398cc679 100644 --- a/jasset/models.py +++ b/jasset/models.py @@ -14,48 +14,48 @@ class AssetGroup(models.Model): def __unicode__(self): return self.name - def get_asset(self): - return self.asset_set.all() - - def get_asset_info(self, printable=False): - assets = self.get_asset() - ip_comment = {} - for asset in assets: - ip_comment[asset.ip] = asset.comment - - for ip in sorted(ip_comment): - if ip_comment[ip]: - print '%-15s -- %s' % (ip, ip_comment[ip]) - else: - print '%-15s' % ip - print '' - - def get_asset_num(self): - return len(self.get_asset()) - - def get_user_group(self): - perm_list = self.perm_set.all() - user_group_list = [] - for perm in perm_list: - user_group_list.append(perm.user_group) - return user_group_list - - def get_user(self): - user_list = [] - user_group_list = self.get_user_group() - for user_group in user_group_list: - user_list.extend(user_group.user_set.all()) - return user_list - - def is_permed(self, user=None, user_group=None): - if user: - if user in self.get_user(): - return True - - if user_group: - if user_group in self.get_user_group(): - return True - return False + # def get_asset(self): + # return self.asset_set.all() + # + # def get_asset_info(self, printable=False): + # assets = self.get_asset() + # ip_comment = {} + # for asset in assets: + # ip_comment[asset.ip] = asset.comment + # + # for ip in sorted(ip_comment): + # if ip_comment[ip]: + # print '%-15s -- %s' % (ip, ip_comment[ip]) + # else: + # print '%-15s' % ip + # print '' + # + # def get_asset_num(self): + # return len(self.get_asset()) + # + # def get_user_group(self): + # perm_list = self.perm_set.all() + # user_group_list = [] + # for perm in perm_list: + # user_group_list.append(perm.user_group) + # return user_group_list + # + # def get_user(self): + # user_list = [] + # user_group_list = self.get_user_group() + # for user_group in user_group_list: + # user_list.extend(user_group.user_set.all()) + # return user_list + # + # def is_permed(self, user=None, user_group=None): + # if user: + # if user in self.get_user(): + # return True + # + # if user_group: + # if user_group in self.get_user_group(): + # return True + # return False class Asset(models.Model): @@ -72,21 +72,21 @@ class Asset(models.Model): def __unicode__(self): return self.ip - def get_user(self): - perm_list = [] - asset_group_all = self.bis_group.all() - for asset_group in asset_group_all: - perm_list.extend(asset_group.perm_set.all()) - - user_group_list = [] - for perm in perm_list: - user_group_list.append(perm.user_group) - - user_permed_list = [] - for user_group in user_group_list: - user_permed_list.extend(user_group.user_set.all()) - user_permed_list = list(set(user_permed_list)) - return user_permed_list + # def get_user(self): + # perm_list = [] + # asset_group_all = self.bis_group.all() + # for asset_group in asset_group_all: + # perm_list.extend(asset_group.perm_set.all()) + # + # user_group_list = [] + # for perm in perm_list: + # user_group_list.append(perm.user_group) + # + # user_permed_list = [] + # for user_group in user_group_list: + # user_permed_list.extend(user_group.user_set.all()) + # user_permed_list = list(set(user_permed_list)) + # return user_permed_list class AssetAlias(models.Model): diff --git a/jasset/views.py b/jasset/views.py index eb500050d..4a33f647c 100644 --- a/jasset/views.py +++ b/jasset/views.py @@ -278,15 +278,15 @@ def asset_edit(request): if request.method == 'POST': ip = request.POST.get('ip') - port = request.POST.get('port') groups = request.POST.getlist('groups') - use_default_auth = True if request.POST.getlist('use_default_auth', []) else False + use_default = True if request.POST.getlist('use_default', []) else False is_active = True if request.POST.get('is_active') else False comment = request.POST.get('comment') - if not use_default_auth: + if not use_default: username = request.POST.get('username') password = request.POST.get('password') + port = request.POST.get('port') if password == asset.password: password_encode = password else: @@ -294,6 +294,7 @@ def asset_edit(request): else: username = None password_encode = None + port = 22 try: asset_test = get_object(Asset, ip=ip) @@ -303,7 +304,7 @@ def asset_edit(request): except ServerError: pass else: - db_asset_update(id=asset_id, ip=ip, port=port, use_default_auth=use_default_auth, + db_asset_update(id=asset_id, ip=ip, port=port, use_default=use_default, username=username, password=password_encode, is_active=is_active, comment=comment) msg = u'主机 %s 修改成功' % ip diff --git a/jperm/perm_api.py b/jperm/perm_api.py index 1321482fe..185fe969d 100644 --- a/jperm/perm_api.py +++ b/jperm/perm_api.py @@ -59,24 +59,43 @@ def playbook_run(inventory, playbook, default_user=None, default_port=None, defa playbook_cb = callbacks.PlaybookCallbacks(verbose=utils.VERBOSITY) runner_cb = callbacks.PlaybookRunnerCallbacks(stats, verbose=utils.VERBOSITY) # run the playbook - results = PlayBook(host_list=inventory, - playbook=playbook, - forks=5, - remote_user=default_user, - remote_port=default_port, - private_key_file=default_pri_key_path, - callbacks=playbook_cb, - runner_callbacks=runner_cb, - stats=stats, - become=True, - become_user='root').run() + print default_user, default_port, default_pri_key_path, inventory, playbook + if default_user and default_port and default_pri_key_path: + playbook = PlayBook(host_list=inventory, + playbook=playbook, + forks=5, + remote_user=default_user, + remote_port=default_port, + private_key_file=default_pri_key_path, + callbacks=playbook_cb, + runner_callbacks=runner_cb, + stats=stats, + become=True, + become_user='root') + else: + playbook = PlayBook(host_list=inventory, + playbook=playbook, + forks=5, + callbacks=playbook_cb, + runner_callbacks=runner_cb, + stats=stats, + become=True, + become_user='root') + results = playbook.run() + results_r = {'unreachable': [], 'failures': [], 'success': []} for hostname, result in results.items(): - if result.get('failures', 2): + if result.get('unreachable', 2): + results_r['unreachable'].append(hostname) + print "%s >>> unreachable" % hostname + elif result.get('failures', 2): + results_r['failures'].append(hostname) print "%s >>> Failed" % hostname else: + results_r['success'].append(hostname) print "%s >>> Success" % hostname - return results + + return results_r def perm_user_api(asset_new, asset_del, asset_group_new, asset_group_del, user=None, user_group=None): @@ -84,21 +103,12 @@ def perm_user_api(asset_new, asset_del, asset_group_new, asset_group_del, user=N asset_new_ip = [] # 新授权的ip列表 asset_del_ip = [] # 回收授权的ip列表 - if '' in asset_group_new: - asset_group_new.remove('') - - if '' in asset_group_del: - asset_group_del.remove('') - - asset_new_ip.extend([asset.ip for asset in get_object_list(Asset, asset_new)]) # 查库,获取新授权ip - for asset_group_id in asset_group_new: - asset_new_ip.extend([asset.ip for asset in get_object(AssetGroup, id=asset_group_id).asset_set.all()]) # 同理 - asset_del_ip.extend([asset.ip for asset in get_object_list(Asset, asset_del)]) # 查库,获取回收授权的ip - for asset_group_id in asset_group_del: - asset_del_ip.extend([asset.ip for asset in get_object(AssetGroup, id=asset_group_id).asset_set.all()]) # 同理 - - print asset_new_ip - print asset_del_ip + asset_new_ip.extend([asset.ip for asset in asset_new]) # 查库,获取新授权ip + for asset_group in asset_group_new: + asset_new_ip.extend([asset.ip for asset in asset_group.asset_set.all()]) # 同理 + asset_del_ip.extend([asset.ip for asset in asset_del]) # 查库,获取回收授权的ip + for asset_group in asset_group_del: + asset_del_ip.extend([asset.ip for asset in asset_group.asset_set.all()]) # 同理 if asset_new_ip or asset_del_ip: host_group = {'new': asset_new_ip, 'del': asset_del_ip} @@ -123,8 +133,8 @@ def perm_user_api(asset_new, asset_del, asset_group_new, asset_group_del, user=N else: default_user = default_port = default_pri_key_path = '' - results = playbook_run(inventory, playbook, default_user, default_port, default_pri_key_path) - return results + results_r = playbook_run(inventory, playbook, default_user, default_port, default_pri_key_path) + return results_r def refresh_group_api(user_group=None, asset_group=None): diff --git a/jperm/views.py b/jperm/views.py index 609ef917c..15f375d79 100644 --- a/jperm/views.py +++ b/jperm/views.py @@ -1,115 +1 @@ -# # coding: utf-8 -# import sys -# -# reload(sys) -# sys.setdefaultencoding('utf8') -# -# from django.shortcuts import render_to_response -# from django.template import RequestContext -# from jperm.models import Perm, SudoPerm, CmdGroup, Apply -from django.db.models import Q -from jperm.models import * -from jumpserver.api import * -from jperm.perm_api import * - - -@require_role('admin') -def perm_user_list(request): - header_title, path1, path2 = '用户授权', '授权管理', '用户授权' - keyword = request.GET.get('search', '') - users_list = User.objects.all() # 获取所有用户 - - if keyword: - users_list = users_list.filter(Q(name=keyword) | Q(username=keyword)) # 搜索 - users_list, p, users, page_range, current_page, show_first, show_end = pages(users_list, request) # 分页 - - return my_render('jperm/perm_user_list.html', locals(), request) - - -@require_role('admin') -def perm_user_edit(request): - header_title, path1, path2 = '用户授权', '授权管理', '授权更改' - user_id = request.GET.get('id', '') - user = get_object(User, id=user_id) - asset_all = Asset.objects.all() # 获取所有资产 - asset_group_all = AssetGroup.objects.all() # 获取所有资产组 - - asset_id_list = user.assets.split(',') # 获取授权的资产id列表 - asset_group_id_list = user.asset_groups.split(',') # 获取授权的资产组id列表 - if request.method == 'GET' and user: - asset_permed = get_object_list(Asset, asset_id_list) # 获取授权的资产对象列表 - asset_group_permed = get_object_list(AssetGroup, asset_group_id_list) # 获取授权的资产组对象列表 - assets = [asset for asset in asset_all if asset not in asset_permed] # 获取没有授权的资产对象列表 - asset_groups = [asset_group for asset_group in asset_group_all if asset_group not in asset_group_permed] # 同理 - return my_render('jperm/perm_user_edit.html', locals(), request) - - elif request.method == 'POST' and user: - asset_select = request.POST.getlist('asset_select', []) # 获取选择的资产id列表 - asset_group_select = request.POST.getlist('asset_groups_select', []) # 获取选择的资产组id列表 - asset_new = list(set(asset_select) - set(asset_id_list)) # 计算的得到新授权的资产对象列表 - asset_del = list(set(asset_id_list) - set(asset_select)) # 计算得到回收权限的资产对象列表 - asset_group_new = list(set(asset_group_select) - set(asset_group_id_list)) # 新授权的资产组对象列表 - asset_group_del = list(set(asset_group_id_list) - set(asset_group_select)) # 回收的资产组对象列表 - user.assets = ','.join(asset_select) # 获取选择的资产id字符串 '1, 2 ,3' - user.asset_groups = ','.join(asset_group_select) # 获取选择的资产组id字符串 '2, 3' - user.save() # 保存到数据库 - - perm_user_api(asset_new, asset_del, asset_group_new, asset_group_del, user=user) # 通过API授权或回收 - - return HttpResponseRedirect('/jperm/user/') - - else: - return HttpResponse('输入错误') - - -@require_role('admin') -def perm_group_list(request): - header_title, path1, path2 = '用户组授权', '授权管理', '用户组授权' - keyword = request.GET.get('search', '') - user_groups_list = UserGroup.objects.all() - - if keyword: - request = user_groups_list.filter(Q(name=keyword) | Q(comment=keyword)) - user_groups_list, p, user_groups, page_range, current_page, show_first, show_end = pages(user_groups_list, request) - - return my_render('jperm/perm_group_list.html', locals(), request) - - -@require_role('admin') -def perm_group_edit(request): - header_title, path1, path2 = '用户组授权', '授权管理', '授权更改' - user_group_id = request.GET.get('id', '') - user_group = get_object(UserGroup, id=user_group_id) - asset_all = Asset.objects.all() - asset_group_all = AssetGroup.objects.all() - - asset_id_list = user_group.assets.split(',') - asset_group_id_list = user_group.asset_groups.split(',') - print asset_id_list, asset_group_id_list - if request.method == 'GET' and user_group: - asset_permed = get_object_list(Asset, asset_id_list) - asset_group_permed = get_object_list(AssetGroup, asset_group_id_list) - assets = [asset for asset in asset_all if asset not in asset_permed] - asset_groups = [asset_group for asset_group in asset_group_all if asset_group not in asset_group_permed] - return my_render('jperm/perm_group_edit.html', locals(), request) - - elif request.method == 'POST' and user_group: - asset_select = request.POST.getlist('asset_select', []) - asset_group_select = request.POST.getlist('asset_groups_select', []) - asset_new = list(set(asset_select) - set(asset_id_list)) - asset_del = list(set(asset_id_list) - set(asset_select)) - asset_group_new = list(set(asset_group_select) - set(asset_group_id_list)) - asset_group_del = list(set(asset_group_id_list) - set(asset_group_select)) - user_group.assets = ','.join(asset_select) - user_group.asset_groups = ','.join(asset_group_select) - user_group.save() - - perm_user_api(asset_new, asset_del, asset_group_new, asset_group_del, user_group=user_group) - - return HttpResponseRedirect('/jperm/group/') - - else: - return HttpResponse('输入错误') - - - +# # coding: utf-8 # import sys # # reload(sys) # sys.setdefaultencoding('utf8') # # from django.shortcuts import render_to_response # from django.template import RequestContext # from jperm.models import Perm, SudoPerm, CmdGroup, Apply import json from django.db.models import Q from jperm.models import * from jumpserver.api import * from jperm.perm_api import * @require_role('admin') def perm_user_list(request): header_title, path1, path2 = '用户授权', '授权管理', '用户授权' keyword = request.GET.get('search', '') users_list = User.objects.all() # 获取所有用户 if keyword: users_list = users_list.filter(Q(name=keyword) | Q(username=keyword)) # 搜索 users_list, p, users, page_range, current_page, show_first, show_end = pages(users_list, request) # 分页 return my_render('jperm/perm_user_list.html', locals(), request) @require_role('admin') def perm_user_edit(request): header_title, path1, path2 = '用户授权', '授权管理', '授权更改' user_id = request.GET.get('id', '') user = get_object(User, id=user_id) asset_all = Asset.objects.all() # 获取所有资产 asset_group_all = AssetGroup.objects.all() # 获取所有资产组 asset_permed = user.asset.all() # 获取授权的资产对象列表 asset_group_permed = user.asset_group.all() # 获取授权的资产组对象列表 if request.method == 'GET' and user: assets = [asset for asset in asset_all if asset not in asset_permed] # 获取没有授权的资产对象列表 asset_groups = [asset_group for asset_group in asset_group_all if asset_group not in asset_group_permed] # 同理 return my_render('jperm/perm_user_edit.html', locals(), request) elif request.method == 'POST' and user: asset_id_select = request.POST.getlist('asset_select', []) # 获取选择的资产id列表 asset_group_id_select = request.POST.getlist('asset_groups_select', []) # 获取选择的资产组id列表 asset_select = get_object_list(Asset, asset_id_select) asset_group_select = get_object_list(AssetGroup, asset_group_id_select) asset_new = list(set(asset_select) - set(asset_permed)) # 计算的得到新授权的资产对象列表 asset_del = list(set(asset_permed) - set(asset_select)) # 计算得到回收权限的资产对象列表 asset_group_new = list(set(asset_group_select) - set(asset_group_permed)) # 新授权的资产组对象列表 asset_group_del = list(set(asset_group_permed) - set(asset_group_select)) # 回收的资产组对象列表 results = perm_user_api(asset_new, asset_del, asset_group_new, asset_group_del, user=user) # 通过API授权或回收 unreachable_asset = [] failures_asset = [] for ip in results.get('unreachable'): unreachable_asset.extend(filter(lambda x: x, Asset.objects.filter(ip=ip))) for ip in results.get('failures'): failures_asset.extend(filter(lambda x: x, Asset.objects.filter(ip=ip))) failures_asset.extend(unreachable_asset) # 失败的授权要统计 for asset in failures_asset: if asset in asset_select: asset_select.remove(asset) else: asset_select.append(asset) user.asset = asset_select user.asset_group = asset_group_select user.save() # 保存到数据库 return HttpResponse(json.dumps(results, sort_keys=True, indent=4), content_type="application/json") else: return HttpResponse('输入错误') @require_role('admin') def perm_group_list(request): header_title, path1, path2 = '用户组授权', '授权管理', '用户组授权' keyword = request.GET.get('search', '') user_groups_list = UserGroup.objects.all() if keyword: request = user_groups_list.filter(Q(name=keyword) | Q(comment=keyword)) user_groups_list, p, user_groups, page_range, current_page, show_first, show_end = pages(user_groups_list, request) return my_render('jperm/perm_group_list.html', locals(), request) @require_role('admin') def perm_group_edit(request): header_title, path1, path2 = '用户组授权', '授权管理', '授权更改' user_group_id = request.GET.get('id', '') user_group = get_object(UserGroup, id=user_group_id) asset_all = Asset.objects.all() asset_group_all = AssetGroup.objects.all() asset_permed = user_group.asset.all() # 获取授权的资产对象列表 asset_group_permed = user_group.asset_group.all() # 获取授权的资产组对象列表 if request.method == 'GET' and user_group: assets = [asset for asset in asset_all if asset not in asset_permed] asset_groups = [asset_group for asset_group in asset_group_all if asset_group not in asset_group_permed] return my_render('jperm/perm_group_edit.html', locals(), request) elif request.method == 'POST' and user_group: asset_id_select = request.POST.getlist('asset_select', []) asset_group_id_select = request.POST.getlist('asset_groups_select', []) asset_select = get_object_list(Asset, asset_id_select) asset_group_select = get_object_list(AssetGroup, asset_group_id_select) asset_new = list(set(asset_select) - set(asset_permed)) # 计算的得到新授权的资产对象列表 asset_del = list(set(asset_permed) - set(asset_select)) # 计算得到回收权限的资产对象列表 asset_group_new = list(set(asset_group_select) - set(asset_group_permed)) # 新授权的资产组对象列表 asset_group_del = list(set(asset_group_permed) - set(asset_group_select)) # 回收的资产组对象列表 results = perm_user_api(asset_new, asset_del, asset_group_new, asset_group_del, user_group=user_group) # 通过API授权或回收 unreachable_asset = [] failures_asset = [] for ip in results.get('unreachable'): unreachable_asset.extend(filter(lambda x: x, Asset.objects.filter(ip=ip))) for ip in results.get('failures'): failures_asset.extend(filter(lambda x: x, Asset.objects.filter(ip=ip))) failures_asset.extend(unreachable_asset) # 失败的授权要统计 for asset in failures_asset: if asset in asset_select: asset_select.remove(asset) else: asset_select.append(asset) user_group.asset = asset_select user_group.asset_group = asset_group_select user_group.save() # 保存到数据库 return HttpResponse(json.dumps(results, sort_keys=True, indent=4), content_type="application/json") else: return HttpResponse('输入错误') \ No newline at end of file diff --git a/jumpserver/models.py b/jumpserver/models.py index 046ebb730..5958c5e1a 100644 --- a/jumpserver/models.py +++ b/jumpserver/models.py @@ -4,6 +4,7 @@ from django.db import models class Setting(models.Model): + name = models.CharField(max_length=100) default_user = models.CharField(max_length=100, null=True, blank=True) default_port = models.IntegerField(max_length=10, null=True, blank=True) default_pri_key_path = models.CharField(max_length=100, null=True, blank=True) diff --git a/jumpserver/templatetags/mytags.py b/jumpserver/templatetags/mytags.py index 54d9a9ab8..160970ad2 100644 --- a/jumpserver/templatetags/mytags.py +++ b/jumpserver/templatetags/mytags.py @@ -70,16 +70,14 @@ def user_asset_count(user): """ 返回用户权限主机的数量 """ - assets_id = user.assets.split(',') - asset_groups = user.asset_groups.split(',') + assets = user.asset.all() + asset_groups = user.asset_group.all() - for asset_group_id in asset_groups: - asset_group = get_object(AssetGroup, id=asset_group_id) + for asset_group in asset_groups: if asset_group: - assets_id.extend(asset.id for asset in asset_group.asset_set.all()) + assets.extend(asset_group.asset_set.all()) - assets_id = set(map(str, assets_id)) - return len(assets_id) + return len(assets) @register.filter(name='user_asset_group_count') @@ -87,7 +85,7 @@ def user_asset_group_count(user): """ 返回用户权限主机组的数量 """ - return len(filter(lambda x: x, user.asset_groups.split(','))) + return len(user.asset_group.all()) # # @register.filter(name='user_group_asset_count') diff --git a/jumpserver/views.py b/jumpserver/views.py index cdf1b3591..e7264d205 100644 --- a/jumpserver/views.py +++ b/jumpserver/views.py @@ -232,6 +232,8 @@ def logout(request): def setting(request): header_title, path1 = '项目设置', '设置' + setting_r = get_object(Setting, name='default') + if request.method == "POST": username = request.POST.get('username', '') port = request.POST.get('port', '') @@ -246,11 +248,11 @@ def setting(request): f.write(private_key) os.chmod(private_key_path, 0600) if settings: - Setting.objects.filter(id=1).update(default_user=username, default_port=port, - default_pri_key_path=private_key_path) + Setting.objects.filter(name='default').update(default_user=username, default_port=port, + default_pri_key_path=private_key_path) else: - settings = Setting(default_user=username, default_port=port, - default_pri_key_path=private_key_path).save() + setting_r = Setting(name='default', default_user=username, default_port=port, + default_pri_key_path=private_key_path).save() msg = "设置成功" return my_render('setting.html', locals(), request) diff --git a/juser/models.py b/juser/models.py index 900c4bfbf..50835f63c 100644 --- a/juser/models.py +++ b/juser/models.py @@ -1,4 +1,4 @@ -#coding: utf-8 +# coding: utf-8 from django.db import models @@ -9,20 +9,20 @@ class UserGroup(models.Model): name = models.CharField(max_length=80, unique=True) # assets = models.TextField(max_length=1000, verbose_name="Assets", default='') # asset_groups = models.CharField(max_length=1000, verbose_name="Asset Groups", default='') - assets = models.ManyToManyField(Asset) - asset_groups = models.ManyToManyField(AssetGroup) comment = models.CharField(max_length=160, blank=True, null=True) + asset = models.ManyToManyField(Asset) + asset_group = models.ManyToManyField(AssetGroup) def __unicode__(self): return self.name - def get_user(self): - return self.user_set.all() - - def update(self, **kwargs): - for key, value in kwargs.items(): - self.__setattr__(key, value) - self.save() + # def get_user(self): + # return self.user_set.all() + # + # def update(self, **kwargs): + # for key, value in kwargs.items(): + # self.__setattr__(key, value) + # self.save() class User(models.Model): @@ -38,100 +38,100 @@ class User(models.Model): role = models.CharField(max_length=2, choices=USER_ROLE_CHOICES, default='CU') uuid = models.CharField(max_length=100) group = models.ManyToManyField(UserGroup) - assets = models.ManyToManyField(Asset) - asset_groups = models.ManyToManyField(AssetGroup) ssh_key_pwd = models.CharField(max_length=200) is_active = models.BooleanField(default=True) last_login = models.DateTimeField(null=True) date_joined = models.DateTimeField(null=True) + asset = models.ManyToManyField(Asset) + asset_group = models.ManyToManyField(AssetGroup) def __unicode__(self): return self.username - def get_asset_group(self): - """ - Get user host_groups. - 获取用户有权限的主机组 - """ - host_group_list = [] - perm_list = [] - user_group_all = self.group.all() - for user_group in user_group_all: - perm_list.extend(user_group.perm_set.all()) - - for perm in perm_list: - host_group_list.append(perm.asset_group) - - return host_group_list - - def get_asset_group_info(self, printable=False): - """ - Get or print asset group info - 获取或打印用户授权资产组 - """ - asset_groups_info = {} - asset_groups = self.get_asset_group() - - for asset_group in asset_groups: - asset_groups_info[asset_group.id] = [asset_group.name, asset_group.comment] - - if printable: - for group_id in asset_groups_info: - if asset_groups_info[group_id][1]: - print "[%3s] %s -- %s" % (group_id, - asset_groups_info[group_id][0], - asset_groups_info[group_id][1]) - else: - print "[%3s] %s" % (group_id, asset_groups_info[group_id][0]) - print '' - else: - return asset_groups_info - - def get_asset(self): - """ - Get the assets of under the user control. - 获取主机列表 - """ - assets = [] - asset_groups = self.get_asset_group() - - for asset_group in asset_groups: - assets.extend(asset_group.asset_set.all()) - - return assets - - def get_asset_info(self, printable=False): - """ - Get or print the user asset info - 获取或打印用户资产信息 - """ - from jasset.models import AssetAlias - assets_info = {} - assets = self.get_asset() - - for asset in assets: - asset_alias = AssetAlias.objects.filter(user=self, asset=asset) - if asset_alias and asset_alias[0].alias != '': - assets_info[asset.ip] = [asset.id, asset.ip, str(asset_alias[0].alias)] - else: - assets_info[asset.ip] = [asset.id, asset.ip, str(asset.comment)] - - if printable: - ips = assets_info.keys() - ips.sort() - for ip in ips: - if assets_info[ip][2]: - print '%-15s -- %s' % (ip, assets_info[ip][2]) - else: - print '%-15s' % ip - print '' - else: - return assets_info - - def update(self, **kwargs): - for key, value in kwargs.items(): - self.__setattr__(key, value) - self.save() + # def get_asset_group(self): + # """ + # Get user host_groups. + # 获取用户有权限的主机组 + # """ + # host_group_list = [] + # perm_list = [] + # user_group_all = self.group.all() + # for user_group in user_group_all: + # perm_list.extend(user_group.perm_set.all()) + # + # for perm in perm_list: + # host_group_list.append(perm.asset_group) + # + # return host_group_list + # + # def get_asset_group_info(self, printable=False): + # """ + # Get or print asset group info + # 获取或打印用户授权资产组 + # """ + # asset_groups_info = {} + # asset_groups = self.get_asset_group() + # + # for asset_group in asset_groups: + # asset_groups_info[asset_group.id] = [asset_group.name, asset_group.comment] + # + # if printable: + # for group_id in asset_groups_info: + # if asset_groups_info[group_id][1]: + # print "[%3s] %s -- %s" % (group_id, + # asset_groups_info[group_id][0], + # asset_groups_info[group_id][1]) + # else: + # print "[%3s] %s" % (group_id, asset_groups_info[group_id][0]) + # print '' + # else: + # return asset_groups_info + # + # def get_asset(self): + # """ + # Get the assets of under the user control. + # 获取主机列表 + # """ + # assets = [] + # asset_groups = self.get_asset_group() + # + # for asset_group in asset_groups: + # assets.extend(asset_group.asset_set.all()) + # + # return assets + # + # def get_asset_info(self, printable=False): + # """ + # Get or print the user asset info + # 获取或打印用户资产信息 + # """ + # from jasset.models import AssetAlias + # assets_info = {} + # assets = self.get_asset() + # + # for asset in assets: + # asset_alias = AssetAlias.objects.filter(user=self, asset=asset) + # if asset_alias and asset_alias[0].alias != '': + # assets_info[asset.ip] = [asset.id, asset.ip, str(asset_alias[0].alias)] + # else: + # assets_info[asset.ip] = [asset.id, asset.ip, str(asset.comment)] + # + # if printable: + # ips = assets_info.keys() + # ips.sort() + # for ip in ips: + # if assets_info[ip][2]: + # print '%-15s -- %s' % (ip, assets_info[ip][2]) + # else: + # print '%-15s' % ip + # print '' + # else: + # return assets_info + # + # def update(self, **kwargs): + # for key, value in kwargs.items(): + # self.__setattr__(key, value) + # self.save() class AdminGroup(models.Model): diff --git a/static/js/base.js b/static/js/base.js index 5445ab9b6..be5b3cade 100644 --- a/static/js/base.js +++ b/static/js/base.js @@ -16,6 +16,13 @@ function check_all(form) { } } +function checkAll(){ + // 选择该页面所有checkbox + $('input[type=checkbox]').each(function(){ + $(this).attr('checked', true) + }) +} + //提取指定行的数据,JSON格式 function GetRowData(row){ var rowData = {}; @@ -89,29 +96,32 @@ function move(from, to, from_o, to_o) { //} // -function selectAllOption(){ - var checklist = document.getElementsByName ("selected"); - if(document.getElementById("select_all").checked) - { - for(var i=0;i
- - - - {% endblock %} \ No newline at end of file