def SaveUserUserScope(request):
    try:
        targetUserId = request.POST['targetUserId']
    except:
        targetUserId = None

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

    response = HttpResponse()

    if not targetUserId:
        response.content = json.dumps({'Success': True, 'Data': '-1', 'Message': '用户主键为空!'})
        return response
    try:
        tmpUserids = UserPermission.GetScopeUserIdsByUserId(None, targetUserId, "Resource.ManagePermission")

        if not userIds:
            if tmpUserids and len(tmpUserids) > 0:
                UserPermission.RevokeUserUserScope(None, targetUserId, "Resource.ManagePermission", tmpUserids)
        else:
            # revokeIds = list(set(tmpRoleIds) ^ set(str(roleIds).split(',')))
            revokeIds = []
            for r in tmpUserids:
                if r not in userIds:
                    revokeIds.append(r)

            # grantIds = list(set(str(roleIds).split(',')) ^ set(tmpRoleIds))
            grantIds = []
            for g in str(userIds).split(','):
                if g not in tmpUserids:
                    grantIds.append(g)
            if len(grantIds) > 0:
                UserPermission.GrantUserUserScope(None, targetUserId, "Resource.ManagePermission", grantIds)

            if len(revokeIds) > 0:
                UserPermission.RevokeUserUserScope(None, targetUserId, "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 GetScopeUserIdsByUserId(request):
    try:
        userId = request.POST['userId']
    except:
        userId = None
    if userId:
        userIds = UserPermission.GetScopeUserIdsByUserId(None, userId, "Resource.ManagePermission")
        returnValue = '['
        for id in userIds:
            returnValue = returnValue + '"' + id + '",'
        returnValue = returnValue.strip(',')
        returnValue = returnValue + ']'
        response = HttpResponse()
        response.content = returnValue
        return response
    else:
        response = HttpResponse()
        response.content = '[]'
        return response