mirror of
https://gitlab.com/spaceti-app/integrations/acs/colnod-connector.git
synced 2026-07-12 22:40:48 +02:00
105 lines
3.0 KiB
C++
105 lines
3.0 KiB
C++
#include "token.h"
|
|
|
|
#include <QJsonArray>
|
|
#include <qjsondocument.h>
|
|
#include <utility>
|
|
|
|
Token::~Token()
|
|
= default;
|
|
|
|
|
|
QByteArray Token::tokensToColnodJson(const QHash<QString, std::shared_ptr<Token>> &tokens)
|
|
{
|
|
QJsonObject items;
|
|
QJsonArray jsonTokens;
|
|
for (const auto& token : tokens)
|
|
{
|
|
jsonTokens.push_back(QJsonValue(QJsonDocument::fromJson(token->jsonData()).object()));
|
|
}
|
|
items.insert("items", jsonTokens);
|
|
QJsonDocument doc;
|
|
doc.setObject(items);
|
|
return doc.toJson();
|
|
}
|
|
|
|
QByteArray Token::jsonData(const QSet<QString> &filters) const
|
|
{
|
|
auto variantMap = this->toVariantMap(filters);
|
|
auto enabled = variantMap["enabled"].toBool();
|
|
auto noValidity = variantMap["noValidity"].toBool();
|
|
variantMap.remove("enabled");
|
|
variantMap.remove("noValidity");
|
|
variantMap.remove("lastModified");
|
|
variantMap.remove("deleted");
|
|
variantMap.remove("lastUpdate");
|
|
|
|
QVariantList flags;
|
|
if ( enabled ) {
|
|
flags << "Enabled";
|
|
}
|
|
if ( noValidity ) {
|
|
flags << "NoValidity";
|
|
}
|
|
variantMap.insert("flags", flags);
|
|
|
|
QJsonDocument doc = QJsonDocument::fromVariant(variantMap);
|
|
return doc.toJson(QJsonDocument::Compact);
|
|
}
|
|
bool Token::fromJson(const QByteArray &data)
|
|
{
|
|
QJsonDocument doc = QJsonDocument::fromJson(data);
|
|
QVariantMap variant = qvariant_cast<QVariantMap>(doc.toVariant());
|
|
|
|
QVariantList flags = variant["flags"].toList();
|
|
for (const auto &value : flags) {
|
|
if ( value.toString() == "Enabled") {
|
|
variant.insert("enabled", true);
|
|
}
|
|
if ( value.toString() == "NoValidity") {
|
|
variant.insert("noValidity", true);
|
|
}
|
|
}
|
|
variant.remove("flags");
|
|
auto lastUpdate = variant["lastUpdate"].toLongLong();
|
|
variant["lastUpdate"] = QDateTime::fromMSecsSinceEpoch(lastUpdate);
|
|
variant.insert("lastModified", QDateTime::fromMSecsSinceEpoch(lastUpdate));
|
|
// Populate this object data
|
|
bool res = fromVariantMap(variant);
|
|
return res;
|
|
}
|
|
|
|
QByteArray Token::idsToJSON(const QList<QString> &ids)
|
|
{
|
|
QJsonArray jsonIds;
|
|
for (const auto& id : ids)
|
|
{
|
|
jsonIds.push_back(id);
|
|
}
|
|
QJsonObject items;
|
|
items.insert("items", jsonIds);
|
|
QJsonDocument doc;
|
|
doc.setObject(items);
|
|
return doc.toJson();
|
|
}
|
|
|
|
|
|
void Token::fromSQLRecord(const QSqlQuery& query)
|
|
{
|
|
setId(query.value(0).toString());
|
|
a_authErrors = query.value(1).toInt();
|
|
setDescription(query.value(2).toString());
|
|
setNoValidity(query.value(6).toBool());
|
|
setEnabled(query.value(3).toBool());
|
|
QDateTime date = query.value(4).toDateTime();
|
|
setLastUpdate(date);
|
|
setName(query.value(5).toString());
|
|
a_timeToLive = query.value(7).toInt();
|
|
a_tokenBits = query.value(8).toInt();
|
|
a_tokenData = query.value(9).toString();
|
|
a_tokenType = query.value(10).toString();
|
|
a_subjectId = query.value(11).toString();
|
|
date = query.value(12).toDateTime();
|
|
setLastModified(date);
|
|
setDeleted(query.value(13).toBool());
|
|
}
|