1. 用户的批量回收, 角色删除会回收推送的角色

pull/26/head
yumaojun 2015-12-06 23:44:13 +08:00
parent 8723d673d7
commit 58082179fe
3 changed files with 35 additions and 17 deletions

View File

@ -218,25 +218,25 @@ def get_role_info(role_id, type="all"):
""" """
# 获取role对应的授权规则 # 获取role对应的授权规则
role_obj = PermRole.objects.get(id=role_id) role_obj = PermRole.objects.get(id=role_id)
rules_obj = role_obj.perm_rule.all() rule_push_obj = role_obj.perm_rule.all()
# 获取role 对应的用户 和 用户组 # 获取role 对应的用户 和 用户组
# 获取role 对应的主机 和主机组 # 获取role 对应的主机 和主机组
users_obj = [] users_obj = []
assets_obj = [] assets_obj = []
user_groups_obj = [] user_groups_obj = []
asset_groups_obj = [] asset_groups_obj = []
for rule in rules_obj: for push in rule_push_obj:
for user in rule.user.all(): for user in push.user.all():
users_obj.append(user) users_obj.append(user)
for asset in rule.asset.all(): for asset in push.asset.all():
assets_obj.append(asset) assets_obj.append(asset)
for user_group in rule.user_group.all(): for user_group in push.user_group.all():
user_groups_obj.append(user_group) user_groups_obj.append(user_group)
for asset_group in rule.asset_group.all(): for asset_group in push.asset_group.all():
asset_groups_obj.append(asset_group) asset_groups_obj.append(asset_group)
if type == "all": if type == "all":
return {"rules": rules_obj, return {"rules": rule_push_obj,
"users": users_obj, "users": users_obj,
"user_groups": user_groups_obj, "user_groups": user_groups_obj,
"assets": assets_obj, "assets": assets_obj,
@ -244,7 +244,7 @@ def get_role_info(role_id, type="all"):
} }
elif type == "rule": elif type == "rule":
return rules_obj return rule_push_obj
elif type == "user": elif type == "user":
return users_obj return users_obj
elif type == "user_group": elif type == "user_group":

View File

@ -297,12 +297,25 @@ def perm_role_delete(request):
role_id = request.POST.get("id") role_id = request.POST.get("id")
role = PermRole.objects.get(id=role_id) role = PermRole.objects.get(id=role_id)
role_key = role.key_path role_key = role.key_path
# 删除推送到主机上的role
recycle_assets = [push.asset for push in role.perm_push.all() if push.success]
print recycle_assets
if recycle_assets:
recycle_resource = gen_resource(recycle_assets)
task = Tasks(recycle_resource)
msg = task.del_user(get_object(PermRole, id=role_id).name)
# TODO: 判断返回结果,处理异常
print msg
# 删除存储的秘钥,以及目录 # 删除存储的秘钥,以及目录
key_files = os.listdir(role_key) key_files = os.listdir(role_key)
for key_file in key_files: for key_file in key_files:
os.remove(os.path.join(role_key, key_file)) os.remove(os.path.join(role_key, key_file))
os.rmdir(role_key) os.rmdir(role_key)
# 数据库里删除记录
# 数据库里删除记录 TODO: 判断返回结果,处理异常
role.delete() role.delete()
return HttpResponse(u"删除角色: %s" % role.name) return HttpResponse(u"删除角色: %s" % role.name)
else: else:
@ -585,15 +598,20 @@ def perm_sudo_delete(request):
def perm_role_recycle(request): def perm_role_recycle(request):
role_id = request.GET.get('role_id') role_id = request.GET.get('role_id')
asset_ids = request.GET.get('asset_id').split(',') asset_ids = request.GET.get('asset_id').split(',')
success = request.GET.get("success")
print request.GET print request.GET
if success == "True": assets = [get_object(Asset, id=asset_id) for asset_id in asset_ids]
assets = [get_object(Asset, id=asset_id) for asset_id in asset_ids]
recycle_resource = gen_resource(assets) recycle_assets = []
task = Tasks(recycle_resource) for asset in assets:
msg = task.del_user(get_object(PermRole, id=role_id).name) if True in [push.success for push in asset.perm_push.all()]:
print msg recycle_assets.append(asset)
recycle_resource = gen_resource(recycle_assets)
task = Tasks(recycle_resource)
msg = task.del_user(get_object(PermRole, id=role_id).name)
# TODO: 判断返回结果,处理异常
print msg
for asset_id in asset_ids: for asset_id in asset_ids:
asset = get_object(Asset, id=asset_id) asset = get_object(Asset, id=asset_id)

View File

@ -198,7 +198,7 @@
{% else %} {% else %}
<td class="text-center" style="color: #ec4758;cursor: help" title="{{ info.result }}" >{{ info.success | yesno:"成功,失败,未知" }} </td> <td class="text-center" style="color: #ec4758;cursor: help" title="{{ info.result }}" >{{ info.success | yesno:"成功,失败,未知" }} </td>
{% endif %} {% endif %}
<td class="text-center" ><a class="fa fa-times del" href="/jperm/role/recycle/?role_id={{ role.id }}&asset_id={{ asset.id }}&success={{ info.success }}" style="color: #ec4758;"></a></td> <td class="text-center" ><a class="fa fa-times del" href="/jperm/role/recycle/?role_id={{ role.id }}&asset_id={{ asset.id }}" style="color: #ec4758;"></a></td>
</tr> </tr>
{% endfor %} {% endfor %}
</tbody> </tbody>