From 4283707daf71d7001c4f97396a8a6a1b8f62618a Mon Sep 17 00:00:00 2001 From: ondra Date: Mon, 14 Dec 2020 11:04:32 +0100 Subject: [PATCH] Add update auth token --- server-sdk | 2 +- src/colnod/colnodapi.cpp | 33 +++++++++++++++++ src/colnod/colnodapi.h | 3 ++ src/colnod/databaseapi.cpp | 41 ++++++++++++---------- src/colnod/syncmanager.cpp | 72 ++++++++++++++++++++++---------------- 5 files changed, 100 insertions(+), 51 deletions(-) diff --git a/server-sdk b/server-sdk index 8aa786f..fcf43b0 160000 --- a/server-sdk +++ b/server-sdk @@ -1 +1 @@ -Subproject commit 8aa786fa51dbbd9c82ec20f10892295c93c4054c +Subproject commit fcf43b043e032151a15c40a3778bc195f45ae509 diff --git a/src/colnod/colnodapi.cpp b/src/colnod/colnodapi.cpp index 9c0a420..a0fcd07 100644 --- a/src/colnod/colnodapi.cpp +++ b/src/colnod/colnodapi.cpp @@ -386,3 +386,36 @@ bool ColnodAPI::checkResponse(const QByteArray &responseData) QJsonDocument doc = QJsonDocument::fromJson(responseData); return doc.object().contains("items"); } + +bool ColnodAPI::updateAuthToken() { + auto login = requestLogin(); + Q_ASSERT(login); + if (login->networkReply() == nullptr) { + qCCritical(colnod) << "Not possible to create login request"; + return false; + } + connect(login.get(), &serversdk::Request::finished, this, [=](){ + // Get login token and check if is correct + if (!checkRequest(login)) { + emit authTokenUpdated(false); + return; + } + AuthToken token; + + if (checkResponse(login->responseData())) { + qCWarning(colnod) << "empty response data when logging in"; + emit authTokenUpdated(false); + return; + } + + token.fromJson(login->responseData()); + authToken = token.id(); + if (authToken.isEmpty()) { + qCCritical(colnod) << "Token uuid is empty"; + emit authTokenUpdated(false);; + return ; + } + qDebug(colnod) << "Auth token updated."; + emit authTokenUpdated(true); + }); +} diff --git a/src/colnod/colnodapi.h b/src/colnod/colnodapi.h index 360c422..6ee0c05 100644 --- a/src/colnod/colnodapi.h +++ b/src/colnod/colnodapi.h @@ -89,6 +89,8 @@ public: bool isValid(); + bool updateAuthToken(); + private: std::shared_ptr requestLogin(); @@ -123,6 +125,7 @@ signals: */ void downloadDataFinished(std::shared_ptr = std::shared_ptr()); void dataProcessFinished(bool success = true); + void authTokenUpdated(bool success); private: serversdk::RequestManager* mReqMan = nullptr; diff --git a/src/colnod/databaseapi.cpp b/src/colnod/databaseapi.cpp index 834788d..e42bb86 100644 --- a/src/colnod/databaseapi.cpp +++ b/src/colnod/databaseapi.cpp @@ -155,8 +155,8 @@ void DatabaseAPI::reupdateDeleted(const QList& ids) for (const auto &id : ids){ query.prepare("UPDATE colnod_token SET last_update = CURRENT_TIMESTAMP() WHERE id = :id"); // query.bindValue(":last_update", QDateTime::fromMSecsSinceEpoch(getNow()).toString("yyyy-MM-dd hh:mm:ss.zzz")); - QString tempId = "'" + id + "'"; - query.bindValue(":id", tempId); +// QString tempId = "'" + id + "'"; + query.bindValue(":id", id); if (!executeQuery(query)) { if (!executeQuery(query)) { qWarning(database) << "ERROR during updating last update of deleted token:" << id << ", it will be synced from Colnod to db in next sync. Last modified will be invalid"; @@ -179,6 +179,7 @@ qint64 DatabaseAPI::getNow() // All tokens which are pushed to Colnad are update back to db, because of diffrent last_update and init of new tokens bool DatabaseAPI::uploadTokens(const QHash> &tokens, const QSet &filters) { + bool res = true; auto query = mDbMan->createQuery(); for (const auto &token : tokens) { if (exists(token->id(), "colnod_token")) { @@ -192,15 +193,16 @@ bool DatabaseAPI::uploadTokens(const QHash> &tok if (!query.isActive()) { mValid = false; + res = false; qWarning(database) << "ERROR durning inserting/updating of token:" << token->name(); - return false; } } - return true; + return res; } bool DatabaseAPI::uploadSubjects(const QHash> &subjects) { + bool res = true; auto query = mDbMan->createQuery(); for (const auto &subject : subjects) { for (const auto &attribute : subject->getAttributes()) { @@ -213,6 +215,7 @@ bool DatabaseAPI::uploadSubjects(const QHash> } if (!query.isActive()) { mValid = false; + res = false; qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << attribute->attributeValue(); } } @@ -224,11 +227,11 @@ bool DatabaseAPI::uploadSubjects(const QHash> } if (!query.isActive()) { mValid = false; - qWarning(database) << "ERROR durning inserting/updating of subject attribute:" << subject->name(); - return false; + res = false; + qWarning(database) << "ERROR durning inserting/updating of subject:" << subject->name(); } } - return true; + return res; } bool DatabaseAPI::deleteItems(const QList &ids, const QString &tableName) @@ -239,8 +242,8 @@ bool DatabaseAPI::deleteItems(const QList &ids, const QString &tableNam if (tableName == "colnod_subject") { for (const auto &id : ids) { query.prepare("DELETE FROM colnod_subject_attribute WHERE subject_id = :id"); - QString tempId = "'" + id + "'"; - query.bindValue(":id", tempId); +// QString tempId = "'" + id + "'"; + query.bindValue(":id", id); if (!executeQuery(query)) { qWarning(database) << "ERROR during marking as deleted:" << id; mValid = false; @@ -251,8 +254,8 @@ bool DatabaseAPI::deleteItems(const QList &ids, const QString &tableNam for (const auto &id : ids) { query.prepare(QString("UPDATE %1 SET deleted = true, last_update = :now, last_modified = :now WHERE id = :id").arg(tableName)); - QString tempId = "'" + id + "'"; - query.bindValue(":id", tempId); +// QString tempId = "'" + id + "'"; + query.bindValue(":id", id); query.bindValue(":now", nowString); if (!executeQuery(query)) { qWarning(database) << "ERROR during marking as deleted:" << id; @@ -267,8 +270,8 @@ std::shared_ptr DatabaseAPI::getToken(const QString &id) { auto query = mDbMan->createQuery(); query.prepare("SELECT * FROM colnod_token WHERE id = :id"); - QString tempId = "'" + id + "'"; - query.bindValue(":id", tempId); +// QString tempId = "'" + id + "'"; + query.bindValue(":id", id); if (!executeQuery(query)) { qCritical(database) << "ERROR during accessing existing tokens. EXIT"; return nullptr; @@ -288,8 +291,8 @@ int DatabaseAPI::exists(const QString &id, const QString &tableName) { auto query = mDbMan->createQuery(); query.prepare(QString("SELECT 1 FROM %1 WHERE id = :id").arg(tableName)); - QString tempId = "'" + id + "'"; - query.bindValue(":id", tempId); +// QString tempId = "'" + id + "'"; + query.bindValue(":id", id); if (!executeQuery(query)) { qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)"; return -1; @@ -301,8 +304,8 @@ qint16 DatabaseAPI::getNumOfTokensOfSubject(const QString &id) { auto query = mDbMan->createQuery(); query.prepare("SELECT 1 FROM colnod_token WHERE subject_id = :id AND description = 'mobile'"); - QString tempId = "'" + id + "'"; - query.bindValue(":id", tempId); +// QString tempId = "'" + id + "'"; + query.bindValue(":id", id); if (!executeQuery(query)) { qCritical(database) << "ERROR during accessing existing tokens. EXIT(1)"; return -1; @@ -314,8 +317,8 @@ QString DatabaseAPI::getSubjectNameByTokenId(const QString &tokenId) { auto query = mDbMan->createQuery(); query.prepare("SELECT name FROM colnod_subject WHERE id = (SELECT subject_id FROM colnod_token WHERE id = :tokenId)"); - QString tempId = "'" + tokenId + "'"; - query.bindValue(":tokenId", tempId); +// QString tempId = "'" + tokenId + "'"; + query.bindValue(":tokenId", tokenId); if (!executeQuery(query)) { qCritical(database) << "ERROR during accessing existing subject. EXIT(1)"; return "error"; diff --git a/src/colnod/syncmanager.cpp b/src/colnod/syncmanager.cpp index b416ef0..17eb453 100644 --- a/src/colnod/syncmanager.cpp +++ b/src/colnod/syncmanager.cpp @@ -81,43 +81,53 @@ bool SyncManager::startSynchronization() } // REUPDATE tokens pushed from database to colnod (cause of lastUpdate time) - Timestamp time; - time.setTimestamp(syncBegin); + // download data changes in this sync - auto tokensRequest = colnod->getTokensUpdated(time); - - // updated data are ready - connect(tokensRequest.get(), &serversdk::Request::finished, [=](){ - - auto uploadedData = std::make_shared(); - uploadedData->loadTokensFromJson(tokensRequest->responseData()); - - if (!uploadedData) { - qCritical(syncmanager) << "Not possible to get correct data from colnod"; - QCoreApplication::exit(-1); + colnod->updateAuthToken(); + connect(colnod.get(), &ColnodAPI::authTokenUpdated, [&,syncBegin](bool success) { + if (!success) { + qCritical(syncmanager) << "Cannot login to Colnod."; return; } - if (!database->uploadTokens(uploadedData->updatedTokens, {"lastModified"})) { - qCritical(syncmanager) << "Not possible to upload data to database"; - QCoreApplication::exit(-1); - return; - } - database->reupdateDeleted(databaseData->deletedTokens); + Timestamp time; + time.setTimestamp(syncBegin); - if (!database->isValid() || !colnod->isValid()) { - qWarning(syncmanager) << "Unsuccessful FINISH, check warnings"; - } - else { - // set new time of last sync to this time of this run - if (!database->setLastSync(syncBegin)) { + auto tokensRequest = colnod->getTokensUpdated(time); + + // updated data are ready + connect(tokensRequest.get(), &serversdk::Request::finished, [=](){ + + auto uploadedData = std::make_shared(); + uploadedData->loadTokensFromJson(tokensRequest->responseData()); + + if (!uploadedData) { + qCritical(syncmanager) << "Not possible to get correct data from colnod"; + QCoreApplication::exit(-1); + return; + } + if (!database->uploadTokens(uploadedData->updatedTokens, {"lastModified"})) { + qCritical(syncmanager) << "Not possible to upload data to database"; + QCoreApplication::exit(-1); + return; + } + database->reupdateDeleted(databaseData->deletedTokens); + + if (!database->isValid() || !colnod->isValid()) { qWarning(syncmanager) << "Unsuccessful FINISH, check warnings"; } - qInfo(syncmanager) << "Successful FINISH"; - } - QCoreApplication::exit(); + else { + // set new time of last sync to this time of this run + if (!database->setLastSync(syncBegin)) { + qWarning(syncmanager) << "Unsuccessful FINISH, check warnings"; + } + qInfo(syncmanager) << "Successful FINISH"; + } + QCoreApplication::exit(); + }); }); + }); colnod->processData(preparedData->databaseData); }); @@ -171,8 +181,8 @@ std::unique_ptr SyncManager::prepareData(const std::shared_ptrid().isEmpty() ) { + // already synced ( tokens are same ) if (token == colnodToken) { alreadySynced.append(colnodToken->id()); continue; @@ -193,7 +203,7 @@ std::unique_ptr SyncManager::prepareData(const std::shared_ptrdeletedTokens) { auto corespondingToken = database->getToken(id); - if ( !corespondingToken) { + if ( corespondingToken->id().isEmpty()) { qWarning(syncmanager) << "Token:" << id << ", deleted in colnod, but not exists in db"; continue; }