Skip to content

Commit

Permalink
Merge pull request #192 from Targoman/create_apitoken
Browse files Browse the repository at this point in the history
fix in save i18n fields
  • Loading branch information
kambizzandi authored Aug 10, 2022
2 parents 0e9dd0e + 4a7255a commit 20945ae
Show file tree
Hide file tree
Showing 32 changed files with 313 additions and 265 deletions.
8 changes: 4 additions & 4 deletions Interfaces/AAA/Accounting_Interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ ORMSelectQuery intfAccountUserAssets::makeSelectQuery(INTFAPICALLBOOM_IMPL &APIC

QVariant IMPL_ORMGET(intfAccountUserAssets) {
if (Authorization::hasPriv(APICALLBOOM_PARAM, this->privOn(EHTTP_GET, this->moduleBaseName())) == false)
this->setSelfFilters({{tblAccountUserAssetsBase::Fields::uas_usrID, APICALLBOOM_PARAM.getUserID() }}, _filters);
this->setSelfFilters({{tblAccountUserAssetsBase::Fields::uas_usrID, APICALLBOOM_PARAM.getActorID() }}, _filters);

return this->Select(GET_METHOD_ARGS_CALL_VALUES);
}
Expand All @@ -686,7 +686,7 @@ bool IMPL_REST_UPDATE(intfAccountUserAssets, setAsPrefered, (

this->callSP(APICALLBOOM_PARAM,
"spUserAsset_SetAsPrefered", {
{ "iUserID", APICALLBOOM_PARAM.getUserID() },
{ "iUserID", APICALLBOOM_PARAM.getActorID() },
{ "iUASID", UserPackageID },
});
return false;
Expand All @@ -702,7 +702,7 @@ bool IMPL_REST_UPDATE(intfAccountUserAssets, disablePackage, (
throw exHTTPBadRequest("Invalid UserPackageID provided");

Authorization::checkPriv(APICALLBOOM_PARAM, this->privOn(EHTTP_PATCH, this->moduleBaseName()));
/*return this->update(APICALLBOOM_PARAM.getUserID(), {
/*return this->update(APICALLBOOM_PARAM.getActorID(), {
{tblAccountUserAssets::Fields::uasID, UserPackageID}
}, {
{tblAccountUserAssets::Fields::uasStatus, TAPI::enuAuditableStatus::Banned},
Expand Down Expand Up @@ -755,7 +755,7 @@ intfAccountAssetUsage::intfAccountAssetUsage(

QVariant IMPL_ORMGET(intfAccountAssetUsage) {
if (Authorization::hasPriv(APICALLBOOM_PARAM, this->privOn(EHTTP_GET, this->moduleBaseName())) == false)
this->setSelfFilters({{tblAccountUserAssetsBase::Fields::uas_usrID, APICALLBOOM_PARAM.getUserID()}}, _filters);
this->setSelfFilters({{tblAccountUserAssetsBase::Fields::uas_usrID, APICALLBOOM_PARAM.getActorID()}}, _filters);

return this->Select(GET_METHOD_ARGS_CALL_VALUES);
}
Expand Down
6 changes: 3 additions & 3 deletions Interfaces/AAA/PrivHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ QVariant PrivHelpers::getPrivValue(const QJsonObject& _privs, const QString& _se
}

stuActiveAccount PrivHelpers::processUserObject(
QJsonObject& _userObj,
const QStringList& _requiredAccess,
const QStringList& _services
QJsonObject& _userObj,
const QStringList& _requiredAccess,
const QStringList& _services
) {
if (_userObj.contains(DBM_SPRESULT_ROWS))
_userObj = _userObj[DBM_SPRESULT_ROWS].toArray().at(0).toObject();
Expand Down
45 changes: 25 additions & 20 deletions Interfaces/AAA/clsJWT.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,42 +31,47 @@

namespace Targoman::API::AAA {

namespace JWTItems{
namespace JWTItems {
TARGOMAN_CREATE_CONSTEXPR(jti);
TARGOMAN_CREATE_CONSTEXPR(iat);
TARGOMAN_CREATE_CONSTEXPR(exp);
TARGOMAN_CREATE_CONSTEXPR(jti);
//
TARGOMAN_CREATE_CONSTEXPR(uid); //usrID -> actor id
//
TARGOMAN_CREATE_CONSTEXPR(priv);
TARGOMAN_CREATE_CONSTEXPR(privs);
TARGOMAN_CREATE_CONSTEXPR(usrLogin);
TARGOMAN_CREATE_CONSTEXPR(usrName);
TARGOMAN_CREATE_CONSTEXPR(usrFamily);
TARGOMAN_CREATE_CONSTEXPR(usrID);
TARGOMAN_CREATE_CONSTEXPR(usrApproval);
TARGOMAN_CREATE_CONSTEXPR(usrStatus);
TARGOMAN_CREATE_CONSTEXPR(canChangePass);
TARGOMAN_CREATE_CONSTEXPR(rolID);
TARGOMAN_CREATE_CONSTEXPR(rolName);
}

class clsJWT{
class clsJWT {
public:
clsJWT(const QJsonObject& _token) : Token(_token) { ; }
inline QString login() const {return this->Token.value(JWTItems::usrLogin).toString();}
inline QString name() const {return this->Token.value(JWTItems::usrName).toString();}
inline QString family() const {return this->Token.value(JWTItems::usrFamily).toString();}
inline QString rolName() const {return this->Token.value(JWTItems::rolName).toString();}
inline quint64 rolID() const {return static_cast<quint64>(this->Token.value(JWTItems::rolID).toDouble());}
inline QVariantMap privs() const {return this->Token.value(JWTItems::privs).toObject().toVariantMap();}
inline QJsonObject privsObject() const {return this->Token.value(JWTItems::privs).toObject();}
inline quint64 usrID() const {return static_cast<quint64>(this->Token.value(JWTItems::usrID).toDouble());}
inline TAPI::enuUserApproval::Type usrApproval() const {return TAPI::enuUserApproval::toEnum(this->Token.value(JWTItems::usrApproval).toString().toLatin1().constData());}
inline TAPI::enuUserStatus::Type usrStatus() const {return TAPI::enuUserStatus::toEnum(this->Token.value(JWTItems::usrStatus).toString().toLatin1().constData());}
inline QString session() const {return this->Token.value(JWTItems::jti).toString();}
inline QVariantMap privatePart() const {return this->Token.value(JWTItems::priv).toObject().toVariantMap();}
inline QJsonValue value(const QLatin1String& _key) const {return this->Token.value(_key);}
inline bool canChangePass() const {return this->Token.value(JWTItems::canChangePass).toBool();}
inline quint64 issuedAt() const { return static_cast<quint64>(this->Token.value(JWTItems::iat).toDouble()); }
inline quint64 expireAt() const { return static_cast<quint64>(this->Token.value(JWTItems::exp).toDouble()); }
inline QString session() const { return this->Token.value(JWTItems::jti).toString(); }
inline quint64 issuedAt() const { return static_cast<quint64>(this->Token.value(JWTItems::iat).toDouble()); }
inline quint64 expireAt() const { return static_cast<quint64>(this->Token.value(JWTItems::exp).toDouble()); }
//
inline quint64 actorID() const { return static_cast<quint64>(this->Token.value(JWTItems::uid).toDouble()); }
//
inline QVariantMap privatePart() const { return this->Token.value(JWTItems::priv).toObject().toVariantMap(); }
inline QVariantMap privs() const { return this->Token.value(JWTItems::privs).toObject().toVariantMap(); }
inline QJsonObject privsObject() const { return this->Token.value(JWTItems::privs).toObject(); }
inline QString login() const { return this->Token.value(JWTItems::usrLogin).toString(); }
inline QString name() const { return this->Token.value(JWTItems::usrName).toString(); }
inline QString family() const { return this->Token.value(JWTItems::usrFamily).toString(); }
inline TAPI::enuUserApproval::Type usrApproval() const { return TAPI::enuUserApproval::toEnum(this->Token.value(JWTItems::usrApproval).toString().toLatin1().constData()); }
inline TAPI::enuUserStatus::Type usrStatus() const { return TAPI::enuUserStatus::toEnum(this->Token.value(JWTItems::usrStatus).toString().toLatin1().constData()); }
inline bool canChangePass() const { return this->Token.value(JWTItems::canChangePass).toBool(); }
inline QString rolName() const { return this->Token.value(JWTItems::rolName).toString(); }
inline quint64 rolID() const { return static_cast<quint64>(this->Token.value(JWTItems::rolID).toDouble()); }

inline QJsonValue value(const QLatin1String& _key) const { return this->Token.value(_key); }

/**
* @brief createSignedJWT creates an string containing HEADER.PAYLOAD.SIGNATURE as described by JWT standard.
Expand Down
12 changes: 6 additions & 6 deletions Interfaces/AAA/intfAccountingBasedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void intfAccountingBasedModule::checkUsageIsAllowed(
if (Privs.contains(this->ServiceName) == false)
throw exHTTPForbidden("[81] You don't have access to: " + this->ServiceName);
stuActiveCredit BestMatchedCredit = this->findBestMatchedCredit(APICALLBOOM_PARAM.getUserID(), _requestedUsage);
stuActiveCredit BestMatchedCredit = this->findBestMatchedCredit(APICALLBOOM_PARAM.getActorID(), _requestedUsage);
if (BestMatchedCredit.TTL == 0) ///@TODO: TTL must be checked
throw exHTTPForbidden("[82] You don't have access to: " + this->ServiceName);
Expand Down Expand Up @@ -377,7 +377,7 @@ Targoman::API::AAA::stuBasketActionResult IMPL_REST_POST(intfAccountingBasedModu
//-- validate preVoucher and owner --------------------------------
checkPreVoucherSanity(_lastPreVoucher);
quint64 CurrentUserID = APICALLBOOM_PARAM.getUserID();
quint64 CurrentUserID = APICALLBOOM_PARAM.getActorID();
if (_lastPreVoucher.Items.isEmpty())
_lastPreVoucher.UserID = CurrentUserID;
Expand Down Expand Up @@ -721,7 +721,7 @@ Targoman::API::AAA::stuBasketActionResult intfAccountingBasedModule::internalUpd
//-- validate preVoucher and owner --------------------------------
checkPreVoucherSanity(_lastPreVoucher);

quint64 CurrentUserID = APICALLBOOM_PARAM.getUserID();
quint64 CurrentUserID = APICALLBOOM_PARAM.getActorID();

if (_lastPreVoucher.Items.isEmpty())
throw exHTTPBadRequest("Pre-Voucher is empty");
Expand Down Expand Up @@ -934,7 +934,7 @@ void intfAccountingBasedModule::processItemForBasket(
**/

//-- --------------------------------
quint64 CurrentUserID = APICALLBOOM_PARAM.getUserID();
quint64 CurrentUserID = APICALLBOOM_PARAM.getActorID();

//-- --------------------------------
if ((_oldVoucherItem == nullptr) && (_assetItem.Qty == 0))
Expand Down Expand Up @@ -1112,7 +1112,7 @@ void intfAccountingBasedModule::computeCouponDiscount(
INOUT stuAssetItem &_assetItem,
const stuVoucherItem *_oldVoucherItem /*= nullptr*/
) {
quint64 CurrentUserID = APICALLBOOM_PARAM.getUserID();
quint64 CurrentUserID = APICALLBOOM_PARAM.getActorID();

/**
* discount code:
Expand Down Expand Up @@ -1474,7 +1474,7 @@ bool intfAccountingBasedModule::cancelVoucherItem(
this->AccountSaleables->callSP(APICALLBOOM_PARAM,
"spSaleable_unReserve", {
{ "iSaleableID", SaleableID },
{ "iUserID", APICALLBOOM_PARAM.getUserID() },
{ "iUserID", APICALLBOOM_PARAM.getActorID() },
{ "iQty", _voucherItem.Qty },
});

Expand Down
88 changes: 46 additions & 42 deletions Interfaces/API/intfSQLBasedModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,33 +252,35 @@ quint64 intfSQLBasedModule::Create(

this->prepareFiltersList();

TAPI::ORMFields_t CreateInfo;
// TAPI::ORMFields_t CreateInfo;
//key: language
QMap<QString, TAPI::ORMFields_t> I18NCreateInfo;

//1: extract I18N fields
TAPI::ORMFields_t::iterator it = _createInfo.begin();
while (it != _createInfo.end()) {
if (it.key().endsWith("I18N")) {
QString ColName = it.key().chopped(QString("I18N").length());

QVariantMap ValuesByLanguage = it.value().toMap();
it = _createInfo.erase(it);

IteratorHelper::ConstIterator(ValuesByLanguage)
.runAll([&ColName, &I18NCreateInfo](QString _key, QVariant _value) -> bool {
I18NCreateInfo[_key].insert(ColName, _value);
return true;
});
if (this->name().endsWith("I18N") == false) {
TAPI::ORMFields_t::iterator it = _createInfo.begin();
while (it != _createInfo.end()) {
if (it.key().endsWith("I18N")) {
QString ColName = it.key(); //.chopped(QString("I18N").length());

QVariantMap ValuesByLanguage = it.value().toMap();
it = _createInfo.erase(it);

IteratorHelper::ConstIterator(ValuesByLanguage)
.runAll([&ColName, &I18NCreateInfo](QString _key, QVariant _value) -> bool {
I18NCreateInfo[_key].insert(ColName, _value);
return true;
});

continue;
} else
it++;
}

continue;
} else
it++;
if ((I18NCreateInfo.isEmpty() == false) && (clsTable::Registry.contains(this->nameWithSchema() + "I18N") == false))
throw exHTTPBadRequest(QString("Table %1 cannot be translated").arg(this->nameWithSchema()));
}

if ((I18NCreateInfo.isEmpty() == false) && (clsTable::Registry.contains(this->nameWithSchema() + "I18N") == false))
throw exHTTPBadRequest(QString("Table %1 cannot be translated").arg(this->nameWithSchema()));

//2: run wo/ I18N
ORMCreateQuery query = this->makeCreateQuery(APICALLBOOM_PARAM);

Expand All @@ -287,7 +289,7 @@ quint64 intfSQLBasedModule::Create(

query.values(_createInfo);

quint64 LastID = query.execute(APICALLBOOM_PARAM.getUserID(SYSTEM_USER_ID));
quint64 LastID = query.execute(APICALLBOOM_PARAM.getActorID(SYSTEM_USER_ID));

//3: run I18N
if ((LastID > 0) && (I18NCreateInfo.isEmpty() == false)) {
Expand Down Expand Up @@ -333,33 +335,35 @@ bool intfSQLBasedModule::Update(
if (_updateInfo.isEmpty())
throw exHTTPBadRequest("No change provided to update");

TAPI::ORMFields_t UpdateInfo;
// TAPI::ORMFields_t UpdateInfo;
//key: language
QMap<QString, TAPI::ORMFields_t> I18NUpdateInfo;

//1: extract I18N fields
TAPI::ORMFields_t::iterator it = _updateInfo.begin();
while (it != _updateInfo.end()) {
if (it.key().endsWith("I18N")) {
QString ColName = it.key().chopped(QString("I18N").length());

QVariantMap ValuesByLanguage = it.value().toMap();
it = _updateInfo.erase(it);

IteratorHelper::ConstIterator(ValuesByLanguage)
.runAll([&ColName, &I18NUpdateInfo](QString _key, QVariant _value) -> bool {
I18NUpdateInfo[_key].insert(ColName, _value);
return true;
});
if (this->name().endsWith("I18N") == false) {
while (it != _updateInfo.end()) {
if (it.key().endsWith("I18N")) {
QString ColName = it.key(); //.chopped(QString("I18N").length());

QVariantMap ValuesByLanguage = it.value().toMap();
it = _updateInfo.erase(it);

IteratorHelper::ConstIterator(ValuesByLanguage)
.runAll([&ColName, &I18NUpdateInfo](QString _key, QVariant _value) -> bool {
I18NUpdateInfo[_key].insert(ColName, _value);
return true;
});

continue;
} else
it++;
}

continue;
} else
it++;
if ((I18NUpdateInfo.isEmpty() == false) && (clsTable::Registry.contains(this->nameWithSchema() + "I18N") == false))
throw exHTTPBadRequest(QString("Table %1 cannot be translated").arg(this->nameWithSchema()));
}

if ((I18NUpdateInfo.isEmpty() == false) && (clsTable::Registry.contains(this->nameWithSchema() + "I18N") == false))
throw exHTTPBadRequest(QString("Table %1 cannot be translated").arg(this->nameWithSchema()));

//2: run wo/ I18N
ORMUpdateQuery query = this->makeUpdateQuery(APICALLBOOM_PARAM)
.setPksByPath(_pksByPath)
Expand All @@ -381,7 +385,7 @@ bool intfSQLBasedModule::Update(
}
}

quint64 NumRowsAffected = query.execute(APICALLBOOM_PARAM.getUserID(SYSTEM_USER_ID)) > 0;
quint64 NumRowsAffected = query.execute(APICALLBOOM_PARAM.getActorID(SYSTEM_USER_ID)) > 0;

//3: run I18N
if ((NumRowsAffected > 0) && (I18NUpdateInfo.isEmpty() == false)) {
Expand Down Expand Up @@ -454,7 +458,7 @@ bool intfSQLBasedModule::DeleteByPks(
query.andWhere({ relatedORMField.Col.name(), enuConditionOperator::Equal, FilterIter.value() });
}

return query.execute(APICALLBOOM_PARAM.getUserID(SYSTEM_USER_ID), {}, _realDelete) > 0;
return query.execute(APICALLBOOM_PARAM.getActorID(SYSTEM_USER_ID), {}, _realDelete) > 0;
}

} // namespace Targoman::API::API
8 changes: 4 additions & 4 deletions Interfaces/ObjectStorage/ObjectStorageManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ quint64 ObjectStorageManager::saveFile(
throw exTargomanBase("Could not create storage folder.", ESTATUS_INTERNAL_SERVER_ERROR);
}
//--
QString StrOwnerID = QString("user_%1").arg(APICALLBOOM_PARAM.getUserID());
QString StrOwnerID = QString("user_%1").arg(APICALLBOOM_PARAM.getActorID());
if (FullPathDir.exists(StrOwnerID) == false) {
if (FullPathDir.mkpath(StrOwnerID) == false)
throw exTargomanBase("Could not create path under storage folder.", ESTATUS_INTERNAL_SERVER_ERROR);
Expand Down Expand Up @@ -149,7 +149,7 @@ quint64 ObjectStorageManager::saveFile(
{ "iFileSize", _file.Size },
{ "iFileType", FileType },
{ "iMimeType", MimeType },
{ "iCreatorUserID", APICALLBOOM_PARAM.getUserID() },
{ "iCreatorUserID", APICALLBOOM_PARAM.getActorID() },
{ "iLockedBy", ServerCommonConfigs::InstanceID.value() },
})
.spDirectOutputs();
Expand Down Expand Up @@ -185,7 +185,7 @@ quint64 ObjectStorageManager::saveFile(
ObjectStorageManager::processQueue,
stuProcessQueueParams(
APICALLBOOM_PARAM,
APICALLBOOM_PARAM.getUserID(),
APICALLBOOM_PARAM.getActorID(),
_uploadFiles,
_uploadQueue,
_uploadGateways,
Expand All @@ -206,7 +206,7 @@ quint64 ObjectStorageManager::saveFile(
.set(tblUploadQueue::Fields::uquLockedBy, DBExpression::NIL())
.where({ tblUploadQueue::Fields::uqu_uflID, enuConditionOperator::Equal, UploadedFileID })
.andWhere({ tblUploadQueue::Fields::uquLockedAt, enuConditionOperator::NotNull })
.execute(APICALLBOOM_PARAM.getUserID())
.execute(APICALLBOOM_PARAM.getActorID())
;
}

Expand Down
10 changes: 5 additions & 5 deletions Interfaces/Server/APICallBoom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,11 @@ void intfAPICallBoom::setJWT(/*TAPI::JWT_t*/QJsonObject &_JWT) {
/*TAPI::JWT_t*/QJsonObject &intfAPICallBoom::getJWT() {
return this->Data->JWT;
}
quint64 intfAPICallBoom::getUserID(quint64 _default) {
quint64 UserID = Targoman::API::AAA::clsJWT(this->Data->JWT).usrID();
if (UserID == 0)
UserID = _default;
return UserID;
quint64 intfAPICallBoom::getActorID(quint64 _default) {
quint64 ActorID = Targoman::API::AAA::clsJWT(this->Data->JWT).actorID();
if (ActorID == 0)
ActorID = _default;
return ActorID;
}
QJsonObject intfAPICallBoom::getJWTPrivsObject() {
return Targoman::API::AAA::clsJWT(this->Data->JWT).privsObject();
Expand Down
2 changes: 1 addition & 1 deletion Interfaces/Server/APICallBoom.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class intfAPICallBoom
virtual bool needJWT() = 0;
void setJWT(/*TAPI::JWT_t*/QJsonObject &_JWT);
/*TAPI::JWT_t*/QJsonObject &getJWT();
quint64 getUserID(quint64 _default=0);
quint64 getActorID(quint64 _default=0);
QJsonObject getJWTPrivsObject();

QString getIP();
Expand Down
Loading

0 comments on commit 20945ae

Please sign in to comment.