restructure

This commit is contained in:
Dario 2019-12-30 17:17:55 +01:00
parent f890cac00d
commit a8c4acd81d
17 changed files with 322 additions and 63 deletions

View File

@ -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})

View File

@ -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> 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<EmailVerificationCode>(result);
}

View File

@ -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<EmailVerificationCode> create(int user_id);
inline Poco::AutoPtr<table::EmailOptIn> getModel() { return _getModel<table::EmailOptIn>(); }
inline Poco::AutoPtr<model::table::EmailOptIn> getModel() { return _getModel<model::table::EmailOptIn>(); }
protected:
EmailVerificationCode(table::EmailOptIn* dbModel);
EmailVerificationCode(model::table::EmailOptIn* dbModel);
static Poco::UInt64 createEmailVerificationCode();
//table::EmailOptIn* mDBModel;

View File

@ -2,7 +2,7 @@
#include <assert.h>
namespace model {
namespace controller {
TableControllerBase::TableControllerBase()
: mReferenceCount(1)

View File

@ -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 <table::ModelBase> mDBModel;
Poco::AutoPtr <model::table::ModelBase> mDBModel;
};
// ****** template function declarations ***************

View File

@ -0,0 +1,24 @@
#include "User.h"
namespace controller {
User::User(model::table::User* dbModel)
{
mDBModel = dbModel;
}
User::~User()
{
}
Poco::AutoPtr<User> User::create()
{
/*auto code = createEmailVerificationCode();
auto db = new model::table::EmailOptIn(code, user_id);
auto result = new EmailVerificationCode(db);
return Poco::AutoPtr<EmailVerificationCode>(result);
*/
}
}

25
src/cpp/controller/User.h Normal file
View File

@ -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<User> create();
inline Poco::AutoPtr<model::table::User> getModel() { return _getModel<model::table::User>(); }
protected:
User(model::table::User* dbModel);
};
}
#endif //GRADIDO_LOGIN_SERVER_CONTROLLER_USER_INCLUDE

View File

@ -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");

View File

@ -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<User> 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<User> 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;

View File

@ -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);

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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();

View File

@ -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<class T> size_t updateIntoDB(const std::string& fieldName, T fieldValue );
template<class T> 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<class T>
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<class T>
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:

View File

@ -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<Poco::Data::BLOB>();
}
else {
mPrivateKey = Poco::Nullable<Poco::Data::BLOB>(Poco::Data::BLOB(*privateKey, privateKey->size()));
}
}
void User::setPublicKey(const unsigned char* publicKey)
{
if (!publicKey) {
mPublicKey = Poco::Nullable<Poco::Data::BLOB>();
}
else {
mPrivateKey = Poco::Nullable<Poco::Data::BLOB>(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;
}
}
}

View File

@ -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<unsigned char>& 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<Poco::Data::BLOB> mPublicKey;
Poco::Nullable<Poco::Data::BLOB> mPrivateKey;
// created: Mysql DateTime
bool mEmailChecked;
std::string mLanguageKey;
};
}
}