forked from ondra/colnod-connector
Change functionality for input with only important data
This commit is contained in:
parent
e34aaeb2a6
commit
a7617762f7
@ -168,3 +168,6 @@ It is possible to chain requests: [see doc](https://support.insomnia.rest/articl
|
|||||||
#### Install snowboard
|
#### Install snowboard
|
||||||
|
|
||||||
`npm install -g snowboard`
|
`npm install -g snowboard`
|
||||||
|
|
||||||
|
## Possible checks
|
||||||
|
[here](https://gitlab.com/spaceti-app/integrations/acs/colnod-connector/blob/master/doc/possible-checks.md)
|
||||||
|
|||||||
5
doc/possible-checks.md
Normal file
5
doc/possible-checks.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
# Possible checks
|
||||||
|
|
||||||
|
## Cases:
|
||||||
|
- try to add new virtual token to subject who has no physical card
|
||||||
@ -13,9 +13,25 @@ int main(int argc, char *argv[])
|
|||||||
Logger *logger = Logger::getLogger();
|
Logger *logger = Logger::getLogger();
|
||||||
parser.setApplicationDescription("Colnod sync");
|
parser.setApplicationDescription("Colnod sync");
|
||||||
QCommandLineOption dryModeOption("dryMode", "ColnodAPI", "Read-only mode, prints changes which would normally be made");
|
QCommandLineOption dryModeOption("dryMode", "ColnodAPI", "Read-only mode, prints changes which would normally be made");
|
||||||
|
QCommandLineOption dropTablesOption("dropTables", "ColnodAPI", "Delete tables with datas from Colnod");
|
||||||
|
QCommandLineOption createTablesOption("createTables", "ColnodAPI", "Create tables to saving data from Colnod");
|
||||||
parser.addOption(dryModeOption);
|
parser.addOption(dryModeOption);
|
||||||
|
parser.addOption(dropTablesOption);
|
||||||
|
parser.addOption(createTablesOption);
|
||||||
parser.process(app);
|
parser.process(app);
|
||||||
SyncManager api(parser.isSet(dryModeOption));
|
SyncManager api(parser.isSet(dryModeOption));
|
||||||
|
QObject::connect(&api, &SyncManager::error, &app, [](){
|
||||||
|
qDebug() << "error";
|
||||||
|
QCoreApplication::exit(1);
|
||||||
|
}, Qt::QueuedConnection);
|
||||||
|
if (parser.isSet(dropTablesOption)){
|
||||||
|
api.dropTables();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if (parser.isSet(createTablesOption)){
|
||||||
|
api.createTables();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
api.loadDataFromDB();
|
api.loadDataFromDB();
|
||||||
api.loadDataFromColnod();
|
api.loadDataFromColnod();
|
||||||
QObject::connect(&api, &SyncManager::dataDownloaded, [&](){
|
QObject::connect(&api, &SyncManager::dataDownloaded, [&](){
|
||||||
@ -26,9 +42,5 @@ int main(int argc, char *argv[])
|
|||||||
api.syncDataFromColnodToDB();
|
api.syncDataFromColnodToDB();
|
||||||
api.syncDataFromDBToColnod();
|
api.syncDataFromDBToColnod();
|
||||||
});
|
});
|
||||||
QObject::connect(&api, &SyncManager::error, &app, [](){
|
|
||||||
qDebug() << "error";
|
|
||||||
QCoreApplication::exit(1);
|
|
||||||
}, Qt::QueuedConnection);
|
|
||||||
return QCoreApplication::exec();
|
return QCoreApplication::exec();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -186,19 +186,19 @@ void DatabaseManager::createTables()
|
|||||||
"deleted BOOLEAN)");
|
"deleted BOOLEAN)");
|
||||||
executeQuery(query, "CREATE TABLE colnod_token ("
|
executeQuery(query, "CREATE TABLE colnod_token ("
|
||||||
"id CHAR(36) primary key NOT NULL,"
|
"id CHAR(36) primary key NOT NULL,"
|
||||||
"auth_errors INTEGER NOT NULL,"
|
"auth_errors INTEGER DEFAULT 255,"
|
||||||
"description VARCHAR(255),"
|
"description VARCHAR(255),"
|
||||||
"enabled BOOLEAN NOT NULL,"
|
"enabled BOOLEAN DEFAULT true,"
|
||||||
"last_update timestamp NOT NULL,"
|
"last_update timestamp DEFAULT '1970-01-01 00:00:00'::timestamp without time zone,"
|
||||||
"name VARCHAR(255),"
|
"name VARCHAR(255),"
|
||||||
"no_validity BOOLEAN NOT NULL,"
|
"no_validity BOOLEAN DEFAULT true,"
|
||||||
"time_to_live INTEGER NOT NULL,"
|
"time_to_live INTEGER DEFAULT 255,"
|
||||||
"token_bits INTEGER,"
|
"token_bits INTEGER DEFAULT 32,"
|
||||||
"token_data VARCHAR(255),"
|
"token_data VARCHAR(255),"
|
||||||
"token_type VARCHAR(255),"
|
"token_type VARCHAR(255) DEFAULT 'Token'::character varying,"
|
||||||
"subject_id CHAR(36),"
|
"subject_id CHAR(36),"
|
||||||
"last_modified timestamp,"
|
"last_modified timestamp NOT NULL,"
|
||||||
"deleted BOOLEAN)");
|
"deleted BOOLEAN DEFAULT false)");
|
||||||
executeQuery(query, "CREATE TABLE colnod_subject_attribute ("
|
executeQuery(query, "CREATE TABLE colnod_subject_attribute ("
|
||||||
"id VARCHAR(255) primary key,"
|
"id VARCHAR(255) primary key,"
|
||||||
"attribute_key VARCHAR(255),"
|
"attribute_key VARCHAR(255),"
|
||||||
@ -307,6 +307,15 @@ bool DatabaseManager::checkDeletedToken(const QString &id)
|
|||||||
return query.value(0).toBool();
|
return query.value(0).toBool();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
qint16 DatabaseManager::getNumOfTokensOfSubject(const QString &id)
|
||||||
|
{
|
||||||
|
QSqlQuery query;
|
||||||
|
query.prepare("SELECT 1 FROM colnod_token WHERE subject_id = :id AND deleted = false");
|
||||||
|
query.bindValue(":id", id);
|
||||||
|
executeQuery(query);
|
||||||
|
return static_cast<qint16>(query.size());
|
||||||
|
}
|
||||||
|
|
||||||
bool DatabaseManager::isOpen()
|
bool DatabaseManager::isOpen()
|
||||||
{
|
{
|
||||||
return opened;
|
return opened;
|
||||||
|
|||||||
@ -40,6 +40,8 @@ public:
|
|||||||
QString getSubjectNameById(const QString &id);
|
QString getSubjectNameById(const QString &id);
|
||||||
bool checkDeletedToken(const QString &id);
|
bool checkDeletedToken(const QString &id);
|
||||||
|
|
||||||
|
qint16 getNumOfTokensOfSubject(const QString &id);
|
||||||
|
|
||||||
bool isOpen();
|
bool isOpen();
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -9,7 +9,7 @@ DataManager::DataManager(const bool &dryMode) :
|
|||||||
nextLastSync = db->getNow();
|
nextLastSync = db->getNow();
|
||||||
lastSync = db->getLastSync();
|
lastSync = db->getLastSync();
|
||||||
}
|
}
|
||||||
connect(db.get(), &DatabaseManager::error, this, &DataManager::error);
|
connect(db.get(), &DatabaseManager::error, this, &DataManager::error, Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
// load all tokens from colnod to comparing with working set from DB
|
// load all tokens from colnod to comparing with working set from DB
|
||||||
@ -84,6 +84,13 @@ void DataManager::fillTokensToChangeDB()
|
|||||||
{
|
{
|
||||||
auto token = std::make_shared<Token>();
|
auto token = std::make_shared<Token>();
|
||||||
token->fromSQLRecord(query);
|
token->fromSQLRecord(query);
|
||||||
|
if (token->getName() == NULL){
|
||||||
|
qint16 num = db->getNumOfTokensOfSubject(token->subjectIdToStringWithoutBraces());
|
||||||
|
token->setName(db->getSubjectNameByTokenId(token->idToStringWithoutBraces()) + "_mobile_" + QString::number(num + 1));
|
||||||
|
token->setDescription("mobile");
|
||||||
|
token->setLastUpdate(token->getLastModified());
|
||||||
|
db->saveToken(token, "colnod_token");
|
||||||
|
}
|
||||||
if (token->getDeleted()){ // sort to delete / to update
|
if (token->getDeleted()){ // sort to delete / to update
|
||||||
tokensFromDBToDeleteFromColnod.append(token->idToStringWithoutBraces());
|
tokensFromDBToDeleteFromColnod.append(token->idToStringWithoutBraces());
|
||||||
}
|
}
|
||||||
@ -252,10 +259,26 @@ void DataManager::setDeletedToTokensFromDB()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// recreate working tables
|
// recreate working tables
|
||||||
void DataManager::emptyDatabase()
|
void DataManager::dropTables()
|
||||||
{
|
{
|
||||||
|
if (db->isOpen()){
|
||||||
db->dropTables();
|
db->dropTables();
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
emit error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void DataManager::createTables()
|
||||||
|
{
|
||||||
|
if (db->isOpen()){
|
||||||
db->createTables();
|
db->createTables();
|
||||||
|
db->updateLastSync(0);
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
emit error();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
quint64 DataManager::getLastModifiedOfToken(const QString &id)
|
quint64 DataManager::getLastModifiedOfToken(const QString &id)
|
||||||
|
|||||||
@ -38,7 +38,9 @@ public:
|
|||||||
|
|
||||||
void updateLastSync();
|
void updateLastSync();
|
||||||
|
|
||||||
void emptyDatabase();
|
void dropTables();
|
||||||
|
|
||||||
|
void createTables();
|
||||||
|
|
||||||
quint64 getLastModifiedOfToken(const QString &id);
|
quint64 getLastModifiedOfToken(const QString &id);
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,8 @@ SyncManager::SyncManager(bool dryMode) :
|
|||||||
dataMan( std::make_unique<DataManager>(dryMode)),
|
dataMan( std::make_unique<DataManager>(dryMode)),
|
||||||
dryMode(dryMode)
|
dryMode(dryMode)
|
||||||
{
|
{
|
||||||
connect(reqMan.get(), &RequestManager::error, this, &SyncManager::error);
|
connect(reqMan.get(), &RequestManager::error, this, &SyncManager::error, Qt::QueuedConnection);
|
||||||
connect(dataMan.get(), &DataManager::error, this, &SyncManager::error);
|
connect(dataMan.get(), &DataManager::error, this, &SyncManager::error, Qt::QueuedConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyncManager::loadDataFromColnod()
|
void SyncManager::loadDataFromColnod()
|
||||||
@ -180,9 +180,14 @@ void SyncManager::syncDeletedDataFromDBToColnod()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// For full sync
|
// For full sync
|
||||||
void SyncManager::prepareDatabase()
|
void SyncManager::dropTables()
|
||||||
{
|
{
|
||||||
dataMan->emptyDatabase();
|
dataMan->dropTables();
|
||||||
|
}
|
||||||
|
|
||||||
|
void SyncManager::createTables()
|
||||||
|
{
|
||||||
|
dataMan->createTables();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SyncManager::isValid()
|
bool SyncManager::isValid()
|
||||||
|
|||||||
@ -14,7 +14,8 @@ public:
|
|||||||
void syncDataFromColnodToDB();
|
void syncDataFromColnodToDB();
|
||||||
void syncDataFromDBToColnod();
|
void syncDataFromDBToColnod();
|
||||||
|
|
||||||
void prepareDatabase();
|
void dropTables();
|
||||||
|
void createTables();
|
||||||
|
|
||||||
bool isValid();
|
bool isValid();
|
||||||
|
|
||||||
|
|||||||
@ -3,18 +3,13 @@
|
|||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
|
||||||
Token::Token(const QString& name, QString tokenData, const QString& decription)
|
Token::Token(const QString& name, QString tokenData, const QString& decription)
|
||||||
{
|
{
|
||||||
this->setName(name);
|
this->setName(name);
|
||||||
this->tokenData = std::move(tokenData);
|
this->tokenData = std::move(tokenData);
|
||||||
this->setDescription(decription);
|
this->setDescription(decription);
|
||||||
this->setId(QUuid::createUuid());
|
this->setId(QUuid::createUuid());
|
||||||
this->tokenType = "Token";
|
|
||||||
this->tokenBits = 32;
|
|
||||||
this->timeToLive = 255;
|
|
||||||
this->authErrors = 255;
|
|
||||||
this->flags = {"Enabled", "NoValidity"};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Token::Token(const QString &name)
|
Token::Token(const QString &name)
|
||||||
@ -35,6 +30,7 @@ void Token::fromJSON(const QJsonObject &json) {
|
|||||||
timeToLive = json["timeToLive"].toInt();
|
timeToLive = json["timeToLive"].toInt();
|
||||||
authErrors = json["authErrors"].toInt();
|
authErrors = json["authErrors"].toInt();
|
||||||
const auto &flagsArray = json["flags"].toArray();
|
const auto &flagsArray = json["flags"].toArray();
|
||||||
|
flags.clear();
|
||||||
for(const auto &flag : flagsArray){
|
for(const auto &flag : flagsArray){
|
||||||
flags.insert(flag.toString());
|
flags.insert(flag.toString());
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,6 +11,16 @@ class Token : public Entity {
|
|||||||
public:
|
public:
|
||||||
// using Entity::Entity;
|
// using Entity::Entity;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* # Needed values to proper sync
|
||||||
|
*
|
||||||
|
* * id - primary key
|
||||||
|
* * lastModified - when it was edited/modified
|
||||||
|
* * tokenData - key to open doors
|
||||||
|
* * subjectId - no way of make unassinged token (token without subject)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
Token() = default;
|
Token() = default;
|
||||||
Token(const QString& name, QString tokenData, const QString& decription = "mobile");
|
Token(const QString& name, QString tokenData, const QString& decription = "mobile");
|
||||||
explicit Token(const QString& name);
|
explicit Token(const QString& name);
|
||||||
@ -60,11 +70,11 @@ public:
|
|||||||
QJsonObject toJSON() const override;
|
QJsonObject toJSON() const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString tokenType;
|
QString tokenType = "Token";
|
||||||
int tokenBits{};
|
int tokenBits = 32;
|
||||||
QString tokenData;
|
QString tokenData;
|
||||||
int timeToLive{};
|
int timeToLive = 255;
|
||||||
int authErrors{};
|
int authErrors = 255;
|
||||||
QSet<QString> flags;
|
QSet<QString> flags = {"NoValidity", "Enabled"};
|
||||||
QUuid subjectId;
|
QUuid subjectId;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user