From a8c4acd81dfded71669ac38ffedcefbe39ae9cd6 Mon Sep 17 00:00:00 2001 From: Dario Date: Mon, 30 Dec 2019 17:17:55 +0100 Subject: [PATCH] restructure --- CMakeLists.txt | 5 +- .../EmailVerificationCode.cpp | 6 +- .../EmailVerificationCode.h | 8 +-- .../TableControllerBase.cpp | 2 +- .../TableControllerBase.h | 6 +- src/cpp/controller/User.cpp | 24 +++++++ src/cpp/controller/User.h | 25 +++++++ src/cpp/model/Session.cpp | 19 +++++- src/cpp/model/Session.h | 24 +++++-- src/cpp/model/table/ElopageBuy.cpp | 9 +-- src/cpp/model/table/ElopageBuy.h | 13 ++-- src/cpp/model/table/EmailOptIn.cpp | 9 +-- src/cpp/model/table/EmailOptIn.h | 9 +-- src/cpp/model/table/ModelBase.cpp | 29 +++++--- src/cpp/model/table/ModelBase.h | 64 +++++++++++++++-- src/cpp/model/table/User.cpp | 65 ++++++++++++++++++ src/cpp/model/table/User.h | 68 ++++++++++++++++++- 17 files changed, 322 insertions(+), 63 deletions(-) rename src/cpp/{model => controller}/EmailVerificationCode.cpp (85%) rename src/cpp/{model => controller}/EmailVerificationCode.h (68%) rename src/cpp/{model => controller}/TableControllerBase.cpp (96%) rename src/cpp/{model => controller}/TableControllerBase.h (89%) create mode 100644 src/cpp/controller/User.cpp create mode 100644 src/cpp/controller/User.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 013990906..61f2530ed 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ include_directories( ) - +FILE(GLOB CONTROLLER "src/cpp/controller/*.cpp" "src/cpp/controller/*.h") FILE(GLOB TINF "dependencies/tinf/src/*.c" "dependencies/tinf/src/*.h") FILE(GLOB HTTPInterface "src/cpp/HTTPInterface/*.h" "src/cpp/HTTPInterface/*.cpp") FILE(GLOB JSONInterface "src/cpp/JSONInterface/*.h" "src/cpp/JSONInterface/*.cpp") @@ -34,7 +34,7 @@ FILE(GLOB PROTO_GRADIDO "src/cpp/proto/gradido/*.cc" "src/cpp/proto/gradido/*.h" FILE(GLOB TEST "src/cpp/test/*.cpp" "src/cpp/test/*.h") SET(LOCAL_SRCS - ${TINF} ${MAIN} ${HTTPInterface} + ${CONTROLLER} ${TINF} ${MAIN} ${HTTPInterface} ${JSONInterface} ${CRYPTO} ${MODEL} ${MODEL_TABLE} ${SINGLETON_MANAGER} ${LIB_SRC} ${MYSQL} ${TASKS} ${PROTO_GRADIDO} @@ -43,6 +43,7 @@ aux_source_directory("src/cpp" LOCAL_SRCS) if(MSVC) # src +source_group("controller" FILES ${CONTROLLER}) source_group("proto" FILES ${PROTO_GRADIDO}) source_group("tinf" FILES ${TINF}) source_group("crypto" FILES ${CRYPTO}) diff --git a/src/cpp/model/EmailVerificationCode.cpp b/src/cpp/controller/EmailVerificationCode.cpp similarity index 85% rename from src/cpp/model/EmailVerificationCode.cpp rename to src/cpp/controller/EmailVerificationCode.cpp index a5f6d0a1b..a5d159fa1 100644 --- a/src/cpp/model/EmailVerificationCode.cpp +++ b/src/cpp/controller/EmailVerificationCode.cpp @@ -2,9 +2,9 @@ #include "sodium.h" -namespace model { +namespace controller { - EmailVerificationCode::EmailVerificationCode(table::EmailOptIn* dbModel) + EmailVerificationCode::EmailVerificationCode(model::table::EmailOptIn* dbModel) { mDBModel = dbModel; } @@ -29,7 +29,7 @@ namespace model { Poco::AutoPtr EmailVerificationCode::create(int user_id) { auto code = createEmailVerificationCode(); - auto db = new table::EmailOptIn(code, user_id); + auto db = new model::table::EmailOptIn(code, user_id); auto result = new EmailVerificationCode(db); return Poco::AutoPtr(result); } diff --git a/src/cpp/model/EmailVerificationCode.h b/src/cpp/controller/EmailVerificationCode.h similarity index 68% rename from src/cpp/model/EmailVerificationCode.h rename to src/cpp/controller/EmailVerificationCode.h index 1b78fc9a9..cf2308ab2 100644 --- a/src/cpp/model/EmailVerificationCode.h +++ b/src/cpp/controller/EmailVerificationCode.h @@ -1,11 +1,11 @@ #ifndef GRADIDO_LOGIN_SERVER_MODEL_EMAIL_VERIFICATION_CODE_INCLUDE #define GRADIDO_LOGIN_SERVER_MODEL_EMAIL_VERIFICATION_CODE_INCLUDE -#include "table/EmailOptIn.h" +#include "../model/table/EmailOptIn.h" #include "TableControllerBase.h" -namespace model { +namespace controller { class EmailVerificationCode : public TableControllerBase { public: @@ -13,10 +13,10 @@ namespace model { ~EmailVerificationCode(); static Poco::AutoPtr create(int user_id); - inline Poco::AutoPtr getModel() { return _getModel(); } + inline Poco::AutoPtr getModel() { return _getModel(); } protected: - EmailVerificationCode(table::EmailOptIn* dbModel); + EmailVerificationCode(model::table::EmailOptIn* dbModel); static Poco::UInt64 createEmailVerificationCode(); //table::EmailOptIn* mDBModel; diff --git a/src/cpp/model/TableControllerBase.cpp b/src/cpp/controller/TableControllerBase.cpp similarity index 96% rename from src/cpp/model/TableControllerBase.cpp rename to src/cpp/controller/TableControllerBase.cpp index 09eab6798..8f0827643 100644 --- a/src/cpp/model/TableControllerBase.cpp +++ b/src/cpp/controller/TableControllerBase.cpp @@ -2,7 +2,7 @@ #include -namespace model { +namespace controller { TableControllerBase::TableControllerBase() : mReferenceCount(1) diff --git a/src/cpp/model/TableControllerBase.h b/src/cpp/controller/TableControllerBase.h similarity index 89% rename from src/cpp/model/TableControllerBase.h rename to src/cpp/controller/TableControllerBase.h index ebd18fa55..e680e1dad 100644 --- a/src/cpp/model/TableControllerBase.h +++ b/src/cpp/controller/TableControllerBase.h @@ -3,9 +3,9 @@ #include "../lib/MultithreadContainer.h" #include "Poco/AutoPtr.h" -#include "table/ModelBase.h" +#include "../model/table/ModelBase.h" -namespace model { +namespace controller { class TableControllerBase : protected UniLib::lib::MultithreadContainer { public: @@ -26,7 +26,7 @@ namespace model { int mReferenceCount; // - Poco::AutoPtr mDBModel; + Poco::AutoPtr mDBModel; }; // ****** template function declarations *************** diff --git a/src/cpp/controller/User.cpp b/src/cpp/controller/User.cpp new file mode 100644 index 000000000..1b3da1ce9 --- /dev/null +++ b/src/cpp/controller/User.cpp @@ -0,0 +1,24 @@ +#include "User.h" + +namespace controller { + User::User(model::table::User* dbModel) + { + mDBModel = dbModel; + } + + User::~User() + { + + } + + + Poco::AutoPtr User::create() + { + /*auto code = createEmailVerificationCode(); + auto db = new model::table::EmailOptIn(code, user_id); + auto result = new EmailVerificationCode(db); + return Poco::AutoPtr(result); + */ + + } +} \ No newline at end of file diff --git a/src/cpp/controller/User.h b/src/cpp/controller/User.h new file mode 100644 index 000000000..ed45827ae --- /dev/null +++ b/src/cpp/controller/User.h @@ -0,0 +1,25 @@ +#ifndef GRADIDO_LOGIN_SERVER_CONTROLLER_USER_INCLUDE +#define GRADIDO_LOGIN_SERVER_CONTROLLER_USER_INCLUDE + +#include "../model/table/User.h" + +#include "TableControllerBase.h" + +namespace controller { + class User : public TableControllerBase + { + public: + + ~User(); + + static Poco::AutoPtr create(); + + inline Poco::AutoPtr getModel() { return _getModel(); } + + protected: + User(model::table::User* dbModel); + + }; +} + +#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_USER_INCLUDE \ No newline at end of file diff --git a/src/cpp/model/Session.cpp b/src/cpp/model/Session.cpp index 440138f7e..ce953b652 100644 --- a/src/cpp/model/Session.cpp +++ b/src/cpp/model/Session.cpp @@ -84,7 +84,7 @@ int WritePassphraseIntoDB::run() // -------------------------------------------------------------------------------------------------------------- Session::Session(int handle) - : mHandleId(handle), mSessionUser(nullptr), mEmailVerificationCode(0), mState(SESSION_STATE_EMPTY), mActive(false) + : mHandleId(handle), mSessionUser(nullptr), mEmailVerificationCode(0), mEmailVerificationCodeObject(nullptr), mState(SESSION_STATE_EMPTY), mActive(false) { } @@ -106,6 +106,10 @@ void Session::reset() lock("Session::reset"); mSessionUser = nullptr; + if (mEmailVerificationCodeObject) { + delete mEmailVerificationCodeObject; + mEmailVerificationCodeObject = nullptr; + } // watch out //updateTimeout(); @@ -127,6 +131,14 @@ void Session::updateTimeout() unlock(); } +controller::EmailVerificationCode* Session::getEmailVerificationCodeObject() +{ + lock("Session::getEmailVerificationCodeObject"); + auto ret = mEmailVerificationCodeObject; + unlock(); + return ret; +} + bool Session::createUser(const std::string& first_name, const std::string& last_name, const std::string& email, const std::string& password) { Profiler usedTime; @@ -362,6 +374,11 @@ int Session::updateEmailVerification(Poco::UInt64 emailVerificationCode) return 0; } +bool Session::createNewEmailVerificationCode() +{ + return false; +} + bool Session::startProcessingTransaction(const std::string& proto_message_base64) { lock("Session::startProcessingTransaction"); diff --git a/src/cpp/model/Session.h b/src/cpp/model/Session.h index 452d8859f..73a904cfc 100644 --- a/src/cpp/model/Session.h +++ b/src/cpp/model/Session.h @@ -18,6 +18,8 @@ #include "../SingletonManager/LanguageManager.h" +#include "../controller/EmailVerificationCode.h" + #include "Poco/Thread.h" #include "Poco/Types.h" #include "Poco/DateTime.h" @@ -54,6 +56,10 @@ public: Session(int handle); ~Session(); + // get new model objects + controller::EmailVerificationCode* getEmailVerificationCodeObject(); + + // ---------------- User functions ---------------------------- // TODO: automatic redirect after some time, median profiled time for register // TODO: register state: written into db, mails sended, update state only if new state is higher as old state bool createUser(const std::string& first_name, const std::string& last_name, const std::string& email, const std::string& password); @@ -65,6 +71,12 @@ public: bool deleteUser(); + Poco::AutoPtr getUser() { + return mSessionUser; + } + + // ------------------------- Email Verification Code functions ------------------------------- + bool loadFromEmailVerificationCode(Poco::UInt64 emailVerificationCode); //! \return 1 = konto already exist @@ -73,16 +85,14 @@ public: //! 0 = ok int updateEmailVerification(Poco::UInt64 emailVerificationCode); - - + bool createNewEmailVerificationCode(); Poco::Net::HTTPCookie getLoginCookie(); - Poco::AutoPtr getUser() { - return mSessionUser; - } - + inline int getHandle() { return mHandleId; } + + // ------------------------ Passphrase functions ---------------------------- inline void setPassphrase(const std::string& passphrase) { mPassphrase = passphrase; } inline const std::string& getPassphrase() { return mPassphrase; } @@ -138,6 +148,8 @@ private: Poco::DateTime mLastActivity; Poco::Net::IPAddress mClientLoginIP; Poco::UInt64 mEmailVerificationCode; + controller::EmailVerificationCode* mEmailVerificationCodeObject; + SessionStates mState; diff --git a/src/cpp/model/table/ElopageBuy.cpp b/src/cpp/model/table/ElopageBuy.cpp index 80bbad3ca..2db6e9574 100644 --- a/src/cpp/model/table/ElopageBuy.cpp +++ b/src/cpp/model/table/ElopageBuy.cpp @@ -51,7 +51,7 @@ namespace model { ELOPAGE_BUY_PRODUCT_PRICE */ - Poco::Data::Statement ElopageBuy::insertIntoDB(Poco::Data::Session session) + Poco::Data::Statement ElopageBuy::_insertIntoDB(Poco::Data::Session session) { Poco::Data::Statement insert(session); @@ -66,11 +66,8 @@ namespace model { return insert; } - Poco::Data::Statement ElopageBuy::updateIntoDB(Poco::Data::Session session) - { - throw Poco::Exception("ElopageBuy::updateIntoDB not implemented"); - } - Poco::Data::Statement ElopageBuy::loadFromDB(Poco::Data::Session session, std::string& fieldName) + + Poco::Data::Statement ElopageBuy::_loadFromDB(Poco::Data::Session session, std::string& fieldName) { // Poco::Data::Statement select(session); diff --git a/src/cpp/model/table/ElopageBuy.h b/src/cpp/model/table/ElopageBuy.h index f70dfa096..b9c58baf6 100644 --- a/src/cpp/model/table/ElopageBuy.h +++ b/src/cpp/model/table/ElopageBuy.h @@ -16,7 +16,7 @@ namespace model { namespace table { - enum ElopageBuyId { + enum ElopageBuyIDFields { ELOPAGE_BUY_ID, ELOPAGE_BUY_AFFILIATE_PROGRAM_ID, ELOPAGE_BUY_PUBLISHER_ID, @@ -35,13 +35,12 @@ namespace model { // generic db operations const char* getTableName() { return "elopage_buys"; } - Poco::Data::Statement insertIntoDB(Poco::Data::Session session); - Poco::Data::Statement updateIntoDB(Poco::Data::Session session); - Poco::Data::Statement loadFromDB(Poco::Data::Session session, std::string& fieldName); - - - + + protected: + Poco::Data::Statement _loadFromDB(Poco::Data::Session session, std::string& fieldName); + Poco::Data::Statement _insertIntoDB(Poco::Data::Session session); + Poco::Int32 mIDs[ELOPAGE_BUY_MAX]; std::string mPayerEmail; std::string mPublisherEmail; diff --git a/src/cpp/model/table/EmailOptIn.cpp b/src/cpp/model/table/EmailOptIn.cpp index 8d09bc054..c8a35af59 100644 --- a/src/cpp/model/table/EmailOptIn.cpp +++ b/src/cpp/model/table/EmailOptIn.cpp @@ -24,7 +24,7 @@ namespace model { } - Poco::Data::Statement EmailOptIn::insertIntoDB(Poco::Data::Session session) + Poco::Data::Statement EmailOptIn::_insertIntoDB(Poco::Data::Session session) { Poco::Data::Statement insert(session); @@ -36,13 +36,8 @@ namespace model { return insert; } - Poco::Data::Statement EmailOptIn::updateIntoDB(Poco::Data::Session session) - { - throw Poco::Exception("EmailVerificationCode::updateIntoDB not implemented"); - } - - Poco::Data::Statement EmailOptIn::loadFromDB(Poco::Data::Session session, std::string& fieldName) + Poco::Data::Statement EmailOptIn::_loadFromDB(Poco::Data::Session session, std::string& fieldName) { Poco::Data::Statement select(session); diff --git a/src/cpp/model/table/EmailOptIn.h b/src/cpp/model/table/EmailOptIn.h index fc2a9d79c..45ed21ead 100644 --- a/src/cpp/model/table/EmailOptIn.h +++ b/src/cpp/model/table/EmailOptIn.h @@ -17,14 +17,11 @@ namespace model { // generic db operations const char* getTableName() { return "email_opt_in"; } - Poco::Data::Statement insertIntoDB(Poco::Data::Session session); - Poco::Data::Statement updateIntoDB(Poco::Data::Session session); - Poco::Data::Statement loadFromDB(Poco::Data::Session session, std::string& fieldName); - - + inline Poco::UInt64 getCode() const { return mEmailVerificationCode; } protected: - + Poco::Data::Statement _loadFromDB(Poco::Data::Session session, std::string& fieldName); + Poco::Data::Statement _insertIntoDB(Poco::Data::Session session); // data type must be a multiple of 4 Poco::UInt64 mEmailVerificationCode; diff --git a/src/cpp/model/table/ModelBase.cpp b/src/cpp/model/table/ModelBase.cpp index 6b0676ac7..95c6288e8 100644 --- a/src/cpp/model/table/ModelBase.cpp +++ b/src/cpp/model/table/ModelBase.cpp @@ -21,17 +21,7 @@ namespace model { int ModelInsertTask::run() { - auto session = ConnectionManager::getInstance()->getConnection(CONNECTION_MYSQL_LOGIN_SERVER); - auto insert = mModel->insertIntoDB(session); - - try { - insert.execute(); - } - catch (Poco::Exception& ex) { - mModel->lock(); - mModel->addError(new ParamError(mModel->getTableName(), "mysql error by inserting", ex.displayText().data())); - mModel->unlock(); - } + mModel->insertIntoDB(); return 0; } @@ -44,6 +34,23 @@ namespace model { unlock(); } + bool ModelBase::insertIntoDB() + { + auto cm = ConnectionManager::getInstance(); + Poco::Data::Statement insert = _insertIntoDB(cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER)); + + size_t resultCount = 0; + try { + return insert.execute() == 1; + } + catch (Poco::Exception& ex) { + lock(); + addError(new ParamError(getTableName(), "mysql error by insert", ex.displayText().data())); + unlock(); + } + return false; + } + void ModelBase::duplicate() { lock(); diff --git a/src/cpp/model/table/ModelBase.h b/src/cpp/model/table/ModelBase.h index 40881e8df..202fb7b0c 100644 --- a/src/cpp/model/table/ModelBase.h +++ b/src/cpp/model/table/ModelBase.h @@ -3,6 +3,7 @@ #include "Poco/Data/Session.h" +#include "../../SingletonManager/ConnectionManager.h" #include "../../lib/MultithreadContainer.h" #include "../../tasks/CPUTask.h" @@ -19,11 +20,10 @@ namespace model { virtual ~ModelBase(); virtual const char* getTableName() = 0; - virtual Poco::Data::Statement insertIntoDB(Poco::Data::Session session) = 0; - virtual Poco::Data::Statement updateIntoDB(Poco::Data::Session session) = 0; - virtual Poco::Data::Statement loadFromDB(Poco::Data::Session session, std::string& fieldName) = 0; - virtual bool executeLoadFromDB(Poco::Data::Statement select) { return select.execute() == 1; }; - + + template size_t updateIntoDB(const std::string& fieldName, T fieldValue ); + template size_t loadFromDB(const std::string& fieldName, T fieldValue); + bool insertIntoDB(); inline void setID(int id) { lock(); mID = id; unlock(); } inline int getID() { lock(); int id = mID; unlock(); return id; } @@ -34,6 +34,10 @@ namespace model { void duplicate(); void release(); protected: + + virtual Poco::Data::Statement _loadFromDB(Poco::Data::Session session, std::string& fieldName) = 0; + virtual Poco::Data::Statement _insertIntoDB(Poco::Data::Session session) = 0; + int mID; // for poco auto ptr @@ -41,6 +45,56 @@ namespace model { }; + template + size_t ModelBase::loadFromDB(const std::string& fieldName, T fieldValue) + { + auto cm = ConnectionManager::getInstance(); + Poco::Data::Statement select = _loadFromDB(cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER), fieldName); + select, use(fieldValue); + + size_t resultCount = 0; + try { + resultCount = select.execute(); + } + catch (Poco::Exception& ex) { + lock(); + addError(new ParamError(getTableName(), "mysql error by selecting", ex.displayText().data())); + addError(new ParamError(getTableName(), "field name for select: ", fieldName.data())); + unlock(); + } + return resultCount; + } + + template + size_t ModelBase::updateIntoDB(const std::string& fieldName, T fieldValue) + { + auto cm = ConnectionManager::getInstance(); + Poco::Data::Statement update(cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER)); + + if (mID == 0) { + addError(new Error("ModelBase::updateIntoDB", "id is zero")); + return 0; + } + + update << "UPDATE " << getTableName() << " SET " << fieldName << " = ? where id = ?", + use(fieldValue), use(mID); + + size_t resultCount = 0; + try { + resultCount = select.execute(); + } + catch (Poco::Exception& ex) { + lock(); + addError(new ParamError(getTableName(), "mysql error by update", ex.displayText().data())); + addError(new ParamError(getTableName(), "field name for update: ", fieldName.data())); + unlock(); + } + return resultCount; + + } + + // ******************** Generic Tasks ************************************ + class ModelInsertTask : public UniLib::controller::CPUTask { public: diff --git a/src/cpp/model/table/User.cpp b/src/cpp/model/table/User.cpp index e8ccfeb1b..eb101f0f9 100644 --- a/src/cpp/model/table/User.cpp +++ b/src/cpp/model/table/User.cpp @@ -1,10 +1,75 @@ #include "User.h" +#include "Poco/Data/Binding.h" + +using namespace Poco::Data::Keywords; + namespace model { namespace table { + User::User() + : mPasswordHashed(0), mEmailChecked(false), mLanguageKey("de") + { + } + + User::User(const std::string& email, const std::string& first_name, const std::string& last_name, Poco::UInt64 passwordHashed/* = 0*/, std::string languageKey/* = "de"*/) + : mEmail(email), mFirstName(first_name), mLastName(last_name), mPasswordHashed(passwordHashed), mEmailChecked(false), mLanguageKey(languageKey) { } + + User::~User() + { + + } + + void User::setPrivateKey(const MemoryBin* privateKey) + { + if (!privateKey) { + mPrivateKey = Poco::Nullable(); + } + else { + mPrivateKey = Poco::Nullable(Poco::Data::BLOB(*privateKey, privateKey->size())); + } + + } + + void User::setPublicKey(const unsigned char* publicKey) + { + if (!publicKey) { + mPublicKey = Poco::Nullable(); + } + else { + mPrivateKey = Poco::Nullable(Poco::Data::BLOB(publicKey, 32)); + } + } + + Poco::Data::Statement User::_insertIntoDB(Poco::Data::Session session) + { + Poco::Data::Statement insert(session); + + + if (mPasswordHashed) { + insert << "INSERT INTO users (email, first_name, last_name, password, language) VALUES(?,?,?,?,?);", + use(mEmail), use(mFirstName), use(mLastName), bind(mPasswordHashed), use(mLanguageKey); + } + else { + insert << "INSERT INTO users (email, first_name, last_name, language) VALUES(?,?,?,?);", + use(mEmail), use(mFirstName), use(mLastName), use(mLanguageKey); + } + + return insert; + } + + Poco::Data::Statement User::_loadFromDB(Poco::Data::Session session, std::string& fieldName) + { + + Poco::Data::Statement select(session); + int email_checked = 0; + select << "SELECT id, email, first_name, last_name, password, pubkey, privkey, email_checked, language from " << getTableName() << " where " << fieldName << " = ?", + into(mID), into(mEmail), into(mFirstName), into(mLastName), into(mPasswordHashed), into(mPublicKey), into(mPrivateKey), into(email_checked), into(mLanguageKey); + + return select; + } } } \ No newline at end of file diff --git a/src/cpp/model/table/User.h b/src/cpp/model/table/User.h index c83fef84c..a85524987 100644 --- a/src/cpp/model/table/User.h +++ b/src/cpp/model/table/User.h @@ -2,15 +2,81 @@ #define GRADIDO_LOGIN_SERVER_MODEL_TABLE_USER_INCLUDE #include "ModelBase.h" +#include "../../SingletonManager/MemoryManager.h" + +//#include "Poco/Nullable.h" +//#include "Poco/Data/LOB.h" namespace model { namespace table { - class User : ModelBase + enum UserFields + { + USER_FIELDS_ID, + USER_FIELD_EMAIL, + USER_FIELDS_FIRST_NAME, + USER_FIELDS_LAST_NAME, + USER_FIELDS_PASSWORD, + USER_FIELDS_PUBLIC_KEY, + USER_FIELDS_PRIVATE_KEY, + USER_FIELDS_EMAIL_CHECKED, + USER_FIELDS_LANGUAGE + }; + + class User : public ModelBase { public: User(); + User(const std::string& email, const std::string& first_name, const std::string& last_name, Poco::UInt64 passwordHashed = 0, std::string languageKey = "de"); + ~User(); + + // generic db operations + const char* getTableName() { return "users"; } + + + + // default getter unlocked + inline const std::string& getEmail() const { return mEmail; } + inline const std::string& getFirstName() const { return mFirstName; } + inline const std::string& getLastName() const { return mLastName; } + inline const Poco::UInt64& getPasswordHashed() const { return mPasswordHashed; } + inline const unsigned char* getPublicKey() const { if (mPublicKey.isNull()) return nullptr; return mPublicKey.value().content().data(); } + inline bool existPrivateKeyCrypted() const { return !mPrivateKey.isNull(); } + inline const std::vector& getPrivateKeyCrypted() const { return mPrivateKey.value().content(); } + inline bool isEmailChecked() const { return mEmailChecked; } + inline const std::string& getLanguageKey() const { return mLanguageKey; } + + // default setter unlocked + inline void setEmail(const std::string& email) { mEmail = email; } + inline void setFirstName(const std::string& first_name) { mFirstName = first_name; } + inline void setLastName(const std::string& last_name) { mLastName = last_name; } + inline void setPasswordHashed(const Poco::UInt64& passwordHashed) { mPasswordHashed = passwordHashed; } + void setPublicKey(const unsigned char* publicKey); + // copy data, didn't move memory bin + void setPrivateKey(const MemoryBin* privateKey); + inline void setEmailChecked(bool emailChecked) { mEmailChecked = emailChecked; } + inline void setLanguageKey(const std::string& languageKey) { mLanguageKey = languageKey; } + + + protected: + Poco::Data::Statement _loadFromDB(Poco::Data::Session session, std::string& fieldName); + // insert only with email, first_name, last_name, password if exist and language + Poco::Data::Statement _insertIntoDB(Poco::Data::Session session); + + std::string mEmail; + std::string mFirstName; + std::string mLastName; + + Poco::UInt64 mPasswordHashed; + + Poco::Nullable mPublicKey; + Poco::Nullable mPrivateKey; + // created: Mysql DateTime + + bool mEmailChecked; + std::string mLanguageKey; + }; } }