def SaveRoleModuleScope(request):
    try:
        targetRoleId = request.POST['targetRoleId']
    except:
        targetRoleId = None

    try:
        moduleIds = request.POST['moduleIds']
    except:
        moduleIds = None

    response = HttpResponse()

    if not targetRoleId:
        response.content = json.dumps({'Success': True, 'Data': '-1', 'Message': '角色主键为空!'})
        return response

    try:
        tmpModuleIds = RolePermission.GetScopeModuleIdsByRoleId(None, targetRoleId, "Resource.ManagePermission")

        if not moduleIds:
            if tmpModuleIds and len(tmpModuleIds) > 0:
                RolePermission.RevokeRoleModuleScope(None, targetRoleId, "Resource.ManagePermission", tmpModuleIds)
        else:

            #revokeIds = list(set(tmpRoleIds) ^ set(str(roleIds).split(',')))
            revokeIds = []
            for r in tmpModuleIds:
                if r not in moduleIds:
                    revokeIds.append(r)

            #grantIds = list(set(str(roleIds).split(',')) ^ set(tmpRoleIds))
            grantIds = []
            for g in str(moduleIds).split(','):
                if g not in tmpModuleIds:
                    grantIds.append(g)

            if len(grantIds) > 0:
                RolePermission.GrantRoleModuleScope(CommonUtils.Current(response, request), targetRoleId, "Resource.ManagePermission", grantIds)

            if len(revokeIds) > 0:
                RolePermission.RevokeRoleModuleScope(None, targetRoleId, "Resource.ManagePermission", revokeIds)
        response.content = json.dumps({'Success': True, 'Data': '1', 'Message': "授权成功!"})
        return response
    except:
        response.content = json.dumps({'Success': False, 'Data': '-2', 'Message': '操作失败!'})
        return response
def GetModuleByRoleId(request):
    try:
        roleId = request.POST['roleId']
    except:
        roleId = None

    if roleId:
        moduleIds = RolePermission.GetScopeModuleIdsByRoleId(
            None, roleId, "Resource.AccessPermission")
        returnValue = StringHelper.GetSpitString(moduleIds, ',')
        response = HttpResponse()
        response.content = returnValue
        return response
    else:
        response = HttpResponse()
        response.content = ''
        return response
def GetScopeModuleIdsByRoleId(request):
    try:
        roleId = request.POST['roleId']
    except:
        roleId = None
    if roleId:
        scopeIds = RolePermission.GetScopeModuleIdsByRoleId(None, roleId, "Resource.ManagePermission")
        returnValue = '['
        for id in scopeIds:
            returnValue = returnValue + '"' + id + '",'
        returnValue = returnValue.strip(',')
        returnValue = returnValue + ']'
        response = HttpResponse()
        response.content = returnValue
        return response
    else:
        response = HttpResponse()
        response.content = '[]'
        return response