Project

General

Profile

RE: WT3 to WT4 Port » Session.C

Sebastian Fett, 07/01/2019 09:23 AM

 
/**************************************************************
* FPMWebGui Session (Session.C) *
* -------------------------- *
* Holds authentication information of the user logged in with *
* a session, handles registration, *
**************************************************************/



// +------------+
// | Inclusions |
// +------------+

// - Standard
#include <string>
// - Wt-Library-Inclusions
// - - Database
#include <Wt/Dbo/Dbo.h> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Dbo.html
#include <Wt/Dbo/ptr.h> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1ptr.html
#include <Wt/Dbo/SqlConnection.h> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1SqlConnection.html
#include <Wt/Dbo/backend/MySQL.h> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1backend_1_1MySQL.html
#include <Wt/Dbo/Session.h> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Session.html
#include <Wt/Dbo/Exception.h> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Dbo_1_1Exception.html
// - - Authentication
#include <Wt/Auth/Login.h> // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Login.html
#include "Wt/Auth/AuthService.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1AuthService.html
#include "Wt/Auth/HashFunction.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1HashFunction.html
#include "Wt/Auth/PasswordService.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1PasswordService.html
#include "Wt/Auth/PasswordStrengthValidator.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1PasswordStrengthValidator.html
#include "Wt/Auth/PasswordVerifier.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1PasswordVerifier.html
#include "Wt/Auth/GoogleService.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1GoogleService.html // These are probably undesired.
//#include "Wt/Auth/FacebookService" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1FacebookService.html // However, at least one of them is necessary to not cause a bug in "user.modify()->name = (u.identity(Wt::Auth::Identity::LoginName)).toUTF8();".
#include "Wt/Auth/Dbo/AuthInfo.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Dbo_1_1AuthInfo.html
#include "Wt/Auth/Dbo/UserDatabase.h" // https://www.webtoolkit.eu/wt/doc/reference/html/classWt_1_1Auth_1_1Dbo_1_1UserDatabase.html
// - Custom Inclusions
#include "Session.h" // Prototype definition of class "Session" and its functions.
#include "User.h" //
#include "FillingModel.h" //
#include "SloshingModel.h" //
#include "FillingSimulation.h" //
#include "SloshingSimulation.h" //
#include "CustomModelTemplate.h" //
#include "TemplateParameter.h" //



// Abbreviations
namespace {
Wt::Auth::AuthService myAuthService;
Wt::Auth::PasswordService myPasswordService(myAuthService);
std::vector<std::unique_ptr<Wt::Auth::OAuthService>> myOAuth;
}



// +----------------------------------+
// | Default-Parameter-Initialization |
// +----------------------------------+

std::string * conparams = new std::string[6] {
"test", // Database-Name
"root", // Database-User-Name
"password", // Database-User-Password
"127.0.0.1", // IP
"3666", // Port
"/tmp/mysql.sock" // Socket
};



// +--------------------------------+
// | Class-Function-Implementations |
// +--------------------------------+

// Configures the Login-Screen.
void Session::configureAuth() {
myAuthService.setAuthTokenValidity(5 * 7 * 24 * 60); // Period of validity given in minutes. (5*7*24*60 = 5 weeks = 35 days = 840 hours = 50400 minutes)
myAuthService.setAuthTokensEnabled(true, "logincookie");
// myAuthService.setEmailVerificationEnabled(true);
// myAuthService.setEmailVerificationRequired(true);
std::unique_ptr<Wt::Auth::PasswordVerifier> verifier = std::make_unique<Wt::Auth::PasswordVerifier>();
verifier->addHashFunction(std::make_unique<Wt::Auth::BCryptHashFunction>(7));
myPasswordService.setVerifier(std::move(verifier));
myPasswordService.setAttemptThrottlingEnabled(true);
myPasswordService.setStrengthValidator(std::make_unique<Wt::Auth::PasswordStrengthValidator>());
}

//Session::Session(const std::string& sqliteDb) : connection_(sqliteDb)
Session::Session() : connection_() {
//connection_ = new dbo::backend::MySQL("test",
// "root",
// "password",
// "127.0.0.1",
// 3666,
// "/tmp/mysql.sock");
auto connection_ = std::make_unique <dbo::backend::MySQL>(conparams[0],
conparams[1],
conparams[2],
conparams[3],
std::stoi(conparams[4]),
conparams[5]);
connection_->setProperty("show-queries", "true");
setConnection(std::move(connection_));
mapClass<User>("user");
mapClass<AuthInfo>("auth_info");
mapClass<AuthInfo::AuthIdentityType>("auth_identity");
mapClass<AuthInfo::AuthTokenType>("auth_token");
mapClass<FillingModel>("filling_model");
mapClass<SloshingModel>("sloshing_model");
mapClass<FillingSimulation>("filling_simulation");
mapClass<SloshingSimulation>("sloshing_simulation");
mapClass<CustomModelTemplate>("custom_model_template");
mapClass<TemplateParameter>("template_parameter");
try {
createTables();
std::cerr << "Created database." << std::endl;
} catch (std::exception& e) {
std::cerr << e.what() << std::endl;
std::cerr << "Using existing database";
}
users_ = std::make_unique<UserDatabase>(*this);
}

//Session::~Session() { //shouldnt be needed with smart pointers wt4
// delete users_;
//}

Wt::Auth::AbstractUserDatabase& Session::users() {
return *users_;
}

dbo::ptr<User> Session::user() {
if (login_.loggedIn())
// dbo::ptr<AuthInfo> authInfo = users_->find(login_.user());
// return authInfo->user();
return user(login_.user());
else
return dbo::ptr<User>();
}

dbo::ptr<User> Session::user(const Wt::Auth::User& authUser) {
dbo::ptr<AuthInfo> authInfo = users_->find(authUser);
dbo::ptr<User> user = authInfo->user();
if (!user) {
//wt3: user = add (new User());
std::unique_ptr<User> user{new User()};
dbo::ptr<User> userPtr = this->add(std::move(user));
authInfo.modify()->setUser(userPtr);
}
return user;
}

const Wt::Auth::AuthService& Session::auth() {
return myAuthService;
}

const Wt::Auth::PasswordService& Session::passwordAuth() {
return myPasswordService;
}

//populating the User table with details of the newly created user
void Session::addNames() {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
// user.modify()->name = valueText(u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
user.modify()->name = (u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
}

//populating the Filling table with details of the newly created model
void Session::addDataFilling(std::map<std::string,std::string> mySQLTokenMap) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
// user.modify()->name = valueText(u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
// std::string user_name = (u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
std::unique_ptr<FillingModel> fmodel = std::make_unique<FillingModel>();
fmodel->user = user;
fmodel->shortModelDesc = mySQLTokenMap["shortModelDesc"];
fmodel->detailModelDesc = mySQLTokenMap["detailModelDesc"];
fmodel->fu = mySQLTokenMap["geometeryFile"];
fmodel->fu1 = mySQLTokenMap["geometeryFileTank"];
fmodel->startTime = mySQLTokenMap["startTime"];
fmodel->endTime = mySQLTokenMap["endTime"];
// fmodel->multiPhaseFlow = mySQLTokenMap["multiPhaseFlow"];
fmodel->pressureCurveOutflow = mySQLTokenMap["pressureCurveOutflow"];
fmodel->finenessPointCloud = mySQLTokenMap["finenessPointCloud"];
fmodel->finenessGrid = mySQLTokenMap["finenessGrid"];
fmodel->scaleGeometry = mySQLTokenMap["scaleGeometry"];
fmodel->inflowVolFlux = mySQLTokenMap["inflowVolFlux"];
fmodel->inflowRelativeFluctuation = mySQLTokenMap["inflowRelativeFluctuation"];
fmodel->nozzle = mySQLTokenMap["nozzleCB"];
fmodel->material = mySQLTokenMap["materialCB"];
fmodel->tiltAngle = mySQLTokenMap["tiltAngle"];
fmodel->edgeAngle = mySQLTokenMap["edgeAngle"];
fmodel->factorHSecondP = mySQLTokenMap["factorHSecondP"];
fmodel->factorHTank = mySQLTokenMap["factorHTank"];
fmodel->initialFillingLevel = mySQLTokenMap["initialFillingLevel"];
fmodel->useFoamModel = mySQLTokenMap["checkBox1"];
fmodel->useTurbulenceModel = mySQLTokenMap["checkBox2"];
fmodel->useMidline = mySQLTokenMap["checkBox3"];
fmodel->useTwoPhase = mySQLTokenMap["checkBox4"];
fmodel->createDate = mySQLTokenMap["date"];
fmodel->editDate = mySQLTokenMap["date"];
Wt::Dbo::ptr<FillingModel> fmodelPtr = this->add(std::move(fmodel));
}

//populating the Sloshing table with details of the newly created user
void Session::addDataSloshing(std::map<std::string,std::string> mySQLTokenMap) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
// user.modify()->name = valueText(u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
// std::string user_name = (u.identity(Wt::Auth::Identity::LoginName)).toUTF8();
std::unique_ptr<SloshingModel> smodel = std::make_unique<SloshingModel>();
smodel->user = user;
// std::string desc = "Test name";
smodel->shortModelDesc = mySQLTokenMap["shortModelDesc"];
smodel->detailModelDesc = mySQLTokenMap["detailModelDesc"];
smodel->fu = mySQLTokenMap["geometeryFile"];
smodel->startTime = mySQLTokenMap["startTime"];
smodel->endTime = mySQLTokenMap["endTime"];
// smodel->multiPhaseFlow = mySQLTokenMap["multiPhaseFlow"];
// smodel->pressureCurveOutflow = mySQLTokenMap["pressureCurveOutflow"];
smodel->finenessPointCloud = mySQLTokenMap["finenessPointCloud"];
smodel->finenessGrid = mySQLTokenMap["finenessGrid"];
smodel->scaleGeometry = mySQLTokenMap["scaleGeometry"];
smodel->absInitialVol = mySQLTokenMap["absInitialVol"];
smodel->material = mySQLTokenMap["materialCB"];
smodel->reductionFactor = mySQLTokenMap["reductionFactor"];
smodel->youngsModulus = mySQLTokenMap["youngsModulus"];
smodel->accBreak = mySQLTokenMap["accBreak"];
smodel->accCurve = mySQLTokenMap["accCurve"];
smodel->accVertical = mySQLTokenMap["accVertical"];
smodel->useFoamModel = mySQLTokenMap["checkBox1"];
smodel->useTurbulenceModel = mySQLTokenMap["checkBox2"];
smodel->elasticBaffleModelling = mySQLTokenMap["checkBox3"];
smodel->createDate = mySQLTokenMap["date"];
smodel->editDate = mySQLTokenMap["date"];
Wt::Dbo::ptr<SloshingModel> smodelPtr = this->add(std::move(smodel));
}

// creating the custom model entry in database table custom_model
Wt::Dbo::ptr<CustomModelTemplate> Session::createCustomTemplate(std::map<std::string,std::string> mySQLTokenMap) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);

std::unique_ptr<CustomModelTemplate> cmodelTemplate = std::make_unique<CustomModelTemplate>();
cmodelTemplate->user = user;
// std::string desc = "Test name";
cmodelTemplate->modelName = mySQLTokenMap["modelName"];
cmodelTemplate->createDate = mySQLTokenMap["date"];
cmodelTemplate->editDate = mySQLTokenMap["date"];
cmodelTemplate->modelCreated = "0";
Wt::Dbo::ptr<CustomModelTemplate> templatePtr = this->add(std::move(cmodelTemplate));
return templatePtr;
}

// adding custom model parameters
void Session::addCustomModelParams(Wt::Dbo::ptr<CustomModelTemplate> id, std::vector<std::string> data) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
// iteraring over the data vector and adding each parameter sequentially
for(auto const& line : data){
std::unique_ptr<TemplateParameter> templateParam = std::make_unique<TemplateParameter>();
templateParam->user = user;
templateParam->custom_model_template = id;
templateParam->parameterName = line;
this->add(std::move(templateParam));
}
}

// Updating specific model data in MySql
void Session::updateDataFilling(std::map<std::string,std::string> mySQLTokenMap, const Wt::Dbo::ptr<FillingModel> fm) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
// directly modifying the Dbo pointer here, changing the old method.
fm.modify()->shortModelDesc = mySQLTokenMap["shortModelDesc"];
fm.modify()->detailModelDesc = mySQLTokenMap["detailModelDesc"];
// fmodel->geometeryFile = mySQLTokenMap["geometeryFile"];
// fmodel->geometeryFileTank = mySQLTokenMap["geometeryFileTank"];
fm.modify()->fu=mySQLTokenMap["geometeryFile"];
fm.modify()->fu1 = mySQLTokenMap["geometeryFileTank"];
fm.modify()->startTime = mySQLTokenMap["startTime"];
fm.modify()->endTime = mySQLTokenMap["endTime"];
// fm.modify()->multiPhaseFlow = mySQLTokenMap["multiPhaseFlow"];
fm.modify()->pressureCurveOutflow = mySQLTokenMap["pressureCurveOutflow"];
fm.modify()->finenessPointCloud = mySQLTokenMap["finenessPointCloud"];
fm.modify()->finenessGrid = mySQLTokenMap["finenessGrid"];
fm.modify()->scaleGeometry = mySQLTokenMap["scaleGeometry"];
fm.modify()->inflowVolFlux = mySQLTokenMap["inflowVolFlux"];
fm.modify()->inflowRelativeFluctuation = mySQLTokenMap["inflowRelativeFluctuation"];
fm.modify()->nozzle = mySQLTokenMap["nozzleCB"];
fm.modify()->material = mySQLTokenMap["materialCB"];
fm.modify()->tiltAngle = mySQLTokenMap["tiltAngle"];
fm.modify()->edgeAngle = mySQLTokenMap["edgeAngle"];
fm.modify()->factorHSecondP = mySQLTokenMap["factorHSecondP"];
fm.modify()->factorHTank = mySQLTokenMap["factorHTank"];
fm.modify()->initialFillingLevel = mySQLTokenMap["initialFillingLevel"];
fm.modify()->useFoamModel = mySQLTokenMap["checkBox1"];
fm.modify()->useTurbulenceModel = mySQLTokenMap["checkBox2"];
fm.modify()->useMidline = mySQLTokenMap["checkBox3"];
fm.modify()->useTwoPhase = mySQLTokenMap["checkBox4"];
std::cout << "inside update data filling \n";
std::cout << mySQLTokenMap["checkBox1"] << " \n"<<mySQLTokenMap["checkBox2"] << " \n"<< mySQLTokenMap["checkBox3"] << " \n"<<mySQLTokenMap["checkBox4"] << " \n";
fm.modify()->editDate = mySQLTokenMap["date"];
std::cout<<"date value..."<<mySQLTokenMap["date"]<<std::endl;
}

void Session::updateDataSloshing(std::map<std::string,std::string> mySQLTokenMap, const Wt::Dbo::ptr<SloshingModel> smodel) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
//NEED to implement this query with smodel.id and NOT smodel.shortModelDesc
// Wt::Dbo::ptr<SloshingModel> smodel = (*this).find<SloshingModel>().where("id = ?").bind(sm.id());
smodel.modify()->shortModelDesc = mySQLTokenMap["shortModelDesc"];
smodel.modify()->detailModelDesc = mySQLTokenMap["detailModelDesc"];
smodel.modify()->fu=mySQLTokenMap["geometeryFile"];
smodel.modify()->startTime = mySQLTokenMap["startTime"];
smodel.modify()->endTime = mySQLTokenMap["endTime"];
// smodel.modify()->multiPhaseFlow = mySQLTokenMap["multiPhaseFlow"];
// smodel.modify()->pressureCurveOutflow = mySQLTokenMap["pressureCurveOutflow"];
smodel.modify()->finenessPointCloud = mySQLTokenMap["finenessPointCloud"];
smodel.modify()->finenessGrid = mySQLTokenMap["finenessGrid"];
smodel.modify()->scaleGeometry = mySQLTokenMap["scaleGeometry"];
smodel.modify()->absInitialVol = mySQLTokenMap["absInitialVol"];
smodel.modify()->material = mySQLTokenMap["materialCB"];
smodel.modify()->reductionFactor = mySQLTokenMap["reductionFactor"];
smodel.modify()->youngsModulus = mySQLTokenMap["youngsModulus"];
smodel.modify()->accBreak = mySQLTokenMap["accBreak"];
smodel.modify()->accCurve = mySQLTokenMap["accCurve"];
smodel.modify()->accVertical = mySQLTokenMap["accVertical"];
smodel.modify()->useFoamModel = mySQLTokenMap["checkBox1"];
smodel.modify()->useTurbulenceModel = mySQLTokenMap["checkBox2"];
smodel.modify()->elasticBaffleModelling = mySQLTokenMap["checkBox3"];
smodel.modify()->createDate = mySQLTokenMap["date"];
smodel.modify()->editDate = mySQLTokenMap["date"];
std::cout<<"date value..."<<mySQLTokenMap["date"]<<std::endl;
}

void Session::startFillingSimulation(const Wt::Dbo::ptr<FillingModel> fmodel, int status, std::string folderName) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
std::unique_ptr<FillingSimulation> fsimulation = std::make_unique<FillingSimulation>();
fsimulation->user = user;
fsimulation->filling_model = fmodel;
fsimulation->shortModelDesc = fmodel->shortModelDesc;
std::time_t result = std::time(NULL);
std::string time_string = std::asctime(std::localtime(&result));
// Wt::WDateTime date; //WDateTime liest aus dem browser und ist 2 stunden falsch, vllt variable übergeben für konsistenz
// std::string time_string = date.currentDateTime().toString().toUTF8();
fsimulation->startTime = time_string;
fsimulation->status = std::to_string(status);
fsimulation->safePath = folderName;
// Wt::Dbo::ptr<FillingSimulation> fsimulationPtr = (*this).add(fsimulation);
this->add(std::move(fsimulation));
}

void Session::startSloshingSimulation(const Wt::Dbo::ptr<SloshingModel> smodel, int status, std::string folderName) {
dbo::Transaction transaction(*this);
const Wt::Auth::User& u = this->login().user();
Wt::Dbo::ptr<User> user = this->user(u);
std::unique_ptr<SloshingSimulation> ssimulation = std::make_unique<SloshingSimulation>();
ssimulation->user = user;
ssimulation->sloshing_model = smodel;
ssimulation->shortModelDesc = smodel->shortModelDesc;
std::time_t result = std::time(NULL);
std::string time_string = std::asctime(std::localtime(&result));
// Wt::WDateTime date; //WDateTime liest aus dem browser und ist 2 stunden falsch
// std::string time_string = date.currentDateTime().toString().toUTF8();
ssimulation->startTime = time_string;
ssimulation->status = std::to_string(status);
ssimulation->safePath = folderName;
// Wt::Dbo::ptr<SloshingSimulation> ssimulationPtr = (*this).add(ssimulation);
this->add(std::move(ssimulation));
}

//const std::vector<const Wt::Auth::OAuthService *>& Session::oAuth()
//{
// std::vector<const Wt::Auth::OAuthService *> result;
// for (auto &auth : myOAuthServices) {
// result.push_back(auth.get());
// }
// return result;
//}

template <typename T>
void startSim(const T& arg) {
std::cout<<"dummy function body here\n";
}

//define session specific functions here, model and template saving and populating views etc.
(1-1/2)