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

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

    response = HttpResponse()

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

    try:
        tmpOrgIds = RolePermission.GetScopeOrganizeIdsByRoleId(None, targetRoleId, "Resource.ManagePermission")

        if not organizeIds:
            if tmpOrgIds and len(tmpOrgIds) > 0:
                RolePermission.RevokeRoleOrganizeScope(None, targetRoleId, "Resource.ManagePermission", tmpOrgIds)
        else:

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

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

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

            if len(revokeIds) > 0:
                RolePermission.RevokeRoleOrganizeScope(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 GetScopeOrganizeIdsByRoleId(request):
    try:
        roleId = request.POST['roleId']
    except:
        roleId = None
    if roleId:
        scopeIds = RolePermission.GetScopeOrganizeIdsByRoleId(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