From 8d85fca3a36639fa760101b8ca56ec5ea877b27c Mon Sep 17 00:00:00 2001 From: Wieczorek Bartosz Date: Sat, 18 Mar 2017 13:30:22 +0100 Subject: [PATCH] refactor --- src/app/CMakeLists.txt | 1 - src/app/DatabaseConnection.hpp | 17 ------------- src/app/main.cpp | 25 ++++++++++--------- src/eedb/EEDB.cpp | 22 ++++++++-------- src/eedb/EEDB.hpp | 9 ++++--- src/eedb/db/CMakeLists.txt | 1 + .../db/config.cpp} | 24 +++++++----------- src/eedb/db/config.hpp | 15 +++++++++++ src/eedb/db/connection.cpp | 5 ++-- src/eedb/db/connection.hpp | 4 +-- src/eedb/widgets/AuthWidget.cpp | 17 +++++++++++-- src/eedb/widgets/AuthWidget.hpp | 25 +++++++++++++++++-- src/eedb/widgets/HomePage.cpp | 24 ++++++++---------- src/eedb/widgets/HomePage.hpp | 10 ++++++-- 14 files changed, 114 insertions(+), 85 deletions(-) delete mode 100644 src/app/DatabaseConnection.hpp rename src/{app/DatabaseConnection.cpp => eedb/db/config.cpp} (53%) create mode 100644 src/eedb/db/config.hpp diff --git a/src/app/CMakeLists.txt b/src/app/CMakeLists.txt index 0c7daae..2ced217 100644 --- a/src/app/CMakeLists.txt +++ b/src/app/CMakeLists.txt @@ -1,5 +1,4 @@ set(SOURCES - DatabaseConnection.cpp main.cpp) diff --git a/src/app/DatabaseConnection.hpp b/src/app/DatabaseConnection.hpp deleted file mode 100644 index 898b1d6..0000000 --- a/src/app/DatabaseConnection.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#include - -namespace Wt { -class WEnvironment; -} - -namespace eedb::db { -class PgConnection; -} - -namespace eedb { - -class DbConnection { - public: - std::unique_ptr< db::PgConnection > create(const Wt::WEnvironment & env) const; -}; -} diff --git a/src/app/main.cpp b/src/app/main.cpp index 25f2746..bb3bf6f 100644 --- a/src/app/main.cpp +++ b/src/app/main.cpp @@ -1,11 +1,10 @@ -#include "DatabaseConnection.hpp" - #include #include #include #include +#include #include #include @@ -21,19 +20,21 @@ class WContainerWidget; Wt::WApplication * createApplication(const Wt::WEnvironment & env) { using std::move; - auto dbConnection = eedb::DbConnection{}.create(env); + using std::make_unique; - auto userDatabase = std::make_unique< eedb::auth::PgUserAuth >(*dbConnection, env); - auto services = eedb::auth::Services(); + auto dbConfig = make_unique< eedb::db::PgConfig >(env); + auto dbConnection = make_unique< eedb::db::PgConnection >(move(dbConfig)); + auto userDatabase = make_unique< eedb::auth::PgUserAuth >(*dbConnection, env); + auto session = make_unique< eedb::Session >(move(dbConnection), env, move(userDatabase)); + auto theme = make_unique< eedb::BootstrapTheme >(); - auto session = std::make_unique< eedb::Session >(move(dbConnection), env, move(userDatabase)); - auto theme = std::make_unique< eedb::BootstrapTheme >(); - - auto authPageFactory = [ services, _session = session.get() ](Wt::WContainerWidget * parent)->eedb::AuthPage * { - return new eedb::AuthPage(services, *_session, parent); + auto authPageFactory = [_session = session.get()]()->eedb::AuthPage * { + auto services = eedb::auth::Services(); + return new eedb::AuthPage(services, *_session); }; - auto homePageFactory = [_session = session.get()](Wt::WContainerWidget * parent)->eedb::HomePage * { - return nullptr; // new eedb::HomePage(); + + auto homePageFactory = [_session = session.get()]()->eedb::HomePage * { + return new eedb::Home(*_session); }; return new eedb::EEDB{std::move(session), std::move(theme), authPageFactory, homePageFactory}; diff --git a/src/eedb/EEDB.cpp b/src/eedb/EEDB.cpp index 8e08ed3..c40272b 100644 --- a/src/eedb/EEDB.cpp +++ b/src/eedb/EEDB.cpp @@ -5,7 +5,6 @@ #include #include -#include #include #include #include @@ -41,34 +40,33 @@ EEDB::EEDB(std::unique_ptr< eedb::Session > session, _authPage(nullptr), _homePageFactory(move(homePageFactory)), _homePage(nullptr) { - // _login.changed().connect(this, &EEDB::authEvent); - // _userDatabase = std::make_unique< eedb::auth::PgUserAuth >(*_dbConnection, env); - root()->addStyleClass("container"); useStyleSheet("/resources/style.css"); if(_theme) setTheme(_theme->create()); - _authPage = _authPageFactory(root()); - // _authWidget = new eedb::AuthPage(eedb::auth::Services{}, *_userDatabase, _login); - // root()->addWidget(_authWidget); + _authPage = _authPageFactory(); + _authPage->onUserWeakLogin().connect([this](auto...) { this->authEventLogin(LoginState::weak); }); + _authPage->onUserStrongLogin().connect([this](auto...) { this->authEventLogin(LoginState::strong); }); + _authPage->onUserLogout().connect([this](auto...) { this->authEventLogout(); }); + + root()->addWidget(_authPage); } -void EEDB::authEvent() { +void EEDB::authEventLogin(EEDB::LoginState state) { using namespace Wt; - // if(_login.loggedIn()) { // Wt::WContainerWidget * container = root(); // setInternalPath("/app"); // root()->removeStyleClass("container"); - // Wt::log("notice") << "Clearing root and creating widgets"; + Wt::log("notice") << "Clearing root and creating widgets"; // // new eedb::Home(container, &_authWidget->login()); +} - // } else { +void EEDB::authEventLogout() { // Wt::log("notice") << "User logged out."; // redirect(url()); // quit(); - // } } } diff --git a/src/eedb/EEDB.hpp b/src/eedb/EEDB.hpp index 71d4d93..a81ec6d 100644 --- a/src/eedb/EEDB.hpp +++ b/src/eedb/EEDB.hpp @@ -13,17 +13,20 @@ class HomePage; class Session; class Theme; -using AuthPageFactory = std::function< AuthPage *(Wt::WContainerWidget *) >; -using HomePageFactory = std::function< HomePage *(Wt::WContainerWidget *) >; +using AuthPageFactory = std::function< AuthPage *() >; +using HomePageFactory = std::function< HomePage *() >; class EEDB : public Wt::WApplication { public: + enum class LoginState { weak, strong }; EEDB(std::unique_ptr< eedb::Session > session, std::unique_ptr< Theme > theme, AuthPageFactory authPageFactory, HomePageFactory homePageFactory); - void authEvent(); + void authEventLogin(LoginState state); + + void authEventLogout(); private: std::unique_ptr< eedb::Session > _session; diff --git a/src/eedb/db/CMakeLists.txt b/src/eedb/db/CMakeLists.txt index e7f865f..d77660b 100644 --- a/src/eedb/db/CMakeLists.txt +++ b/src/eedb/db/CMakeLists.txt @@ -1,5 +1,6 @@ set(SOURCE connection.cpp + config.cpp ) find_package(Sqlpp11 REQUIRED) diff --git a/src/app/DatabaseConnection.cpp b/src/eedb/db/config.cpp similarity index 53% rename from src/app/DatabaseConnection.cpp rename to src/eedb/db/config.cpp index eff0417..bbfb185 100644 --- a/src/app/DatabaseConnection.cpp +++ b/src/eedb/db/config.cpp @@ -1,14 +1,10 @@ -#include "DatabaseConnection.hpp" +#include #include #include -#include - -namespace eedb { -std::unique_ptr< db::PgConnection > eedb::DbConnection::create(const Wt::WEnvironment & env) const { - auto config = std::make_shared< sqlpp::postgresql::connection_config >(); - +namespace eedb::db { +PgConfig::PgConfig(const Wt::WEnvironment & env) { auto readString = [&env](auto && name, auto & val, auto def) { if(!env.server()->readConfigurationProperty(name, val)) { val = def; @@ -33,13 +29,11 @@ std::unique_ptr< db::PgConnection > eedb::DbConnection::create(const Wt::WEnviro } }; - readString("db_host", config->host, "localhost"); - readString("db_user", config->user, "postgres"); - readString("db_password", config->password, "postgres"); - readString("db_name", config->dbname, "eedb"); - readInt("db_port", config->port, 5432); - readBool("db_debug", config->debug, true); - - return std::make_unique< db::PgConnection >(config); + readString("db_host", host, "localhost"); + readString("db_user", user, "postgres"); + readString("db_password", password, "postgres"); + readString("db_name", dbname, "eedb"); + readInt("db_port", port, 5432); + readBool("db_debug", debug, true); } } diff --git a/src/eedb/db/config.hpp b/src/eedb/db/config.hpp new file mode 100644 index 0000000..94453e6 --- /dev/null +++ b/src/eedb/db/config.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include + +namespace Wt { +class WEnvironment; +} + +namespace eedb::db { + +class PgConfig : public sqlpp::postgresql::connection_config { + public: + PgConfig(const Wt::WEnvironment & env); +}; +} diff --git a/src/eedb/db/connection.cpp b/src/eedb/db/connection.cpp index 8282b23..0b21c8d 100644 --- a/src/eedb/db/connection.cpp +++ b/src/eedb/db/connection.cpp @@ -1,8 +1,7 @@ +#include #include - namespace eedb::db { - - +PgConnection::PgConnection(std::shared_ptr config) : _conn(std::make_unique< sqlpp::postgresql::connection >(config)) {} } diff --git a/src/eedb/db/connection.hpp b/src/eedb/db/connection.hpp index 1ec9a02..16a4c8b 100644 --- a/src/eedb/db/connection.hpp +++ b/src/eedb/db/connection.hpp @@ -5,11 +5,11 @@ #include namespace eedb::db { +class PgConfig; class PgConnection { public: - PgConnection(std::shared_ptr< sqlpp::postgresql::connection_config > config) - : _conn(std::make_unique< sqlpp::postgresql::connection >(config)) {} + PgConnection(std::shared_ptr< eedb::db::PgConfig > config); sqlpp::postgresql::connection * native() { return _conn.get(); diff --git a/src/eedb/widgets/AuthWidget.cpp b/src/eedb/widgets/AuthWidget.cpp index d1648c7..03dd81d 100644 --- a/src/eedb/widgets/AuthWidget.cpp +++ b/src/eedb/widgets/AuthWidget.cpp @@ -3,11 +3,24 @@ #include namespace eedb { -AuthPage::AuthPage(const auth::Services & baseAuth, Session & session, Wt::WContainerWidget * parent) - : Wt::Auth::AuthWidget(*baseAuth.authService(), session.userDatabase(), session.login(), parent) { +AuthPage::AuthPage(const auth::Services & baseAuth, Session & session) + : Wt::Auth::AuthWidget(*baseAuth.authService(), session.userDatabase(), session.login(), nullptr) { this->model()->addPasswordAuth(eedb::auth::Services::passwordService()); this->model()->addOAuth(eedb::auth::Services::oAuthServices()); this->setRegistrationEnabled(true); + + login().changed().connect([this](auto...) { + if(this->login().state() == Wt::Auth::LoginState::StrongLogin) { + _onUserStrongLogin(); + } else if(this->login().state() == Wt::Auth::LoginState::WeakLogin) { + _onUserWeakLogin(); + } else if(this->login().state() == Wt::Auth::LoginState::DisabledLogin) { + _onNeedEmailVerification(); + } else { + _onUserLogout(); + } + }); + this->processEnvironment(); } } diff --git a/src/eedb/widgets/AuthWidget.hpp b/src/eedb/widgets/AuthWidget.hpp index eed3904..311f6d3 100644 --- a/src/eedb/widgets/AuthWidget.hpp +++ b/src/eedb/widgets/AuthWidget.hpp @@ -25,7 +25,28 @@ class AuthPage : public Wt::Auth::AuthWidget { using _base = Wt::Auth::AuthWidget; public: - AuthPage(const eedb::auth::Services & baseAuth, eedb::Session & session, Wt::WContainerWidget * parent = nullptr); - + AuthPage(const eedb::auth::Services & baseAuth, Session & session); + + Wt::Signal<> & needVerification() { + return _onNeedEmailVerification; + } + + Wt::Signal<> & onUserWeakLogin() { + return _onUserWeakLogin; + } + + Wt::Signal<> & onUserStrongLogin() { + return _onUserStrongLogin; + } + + Wt::Signal<> & onUserLogout() { + return _onUserLogout; + } + + private: + Wt::Signal<> _onNeedEmailVerification; + Wt::Signal<> _onUserWeakLogin; + Wt::Signal<> _onUserStrongLogin; + Wt::Signal<> _onUserLogout; }; } diff --git a/src/eedb/widgets/HomePage.cpp b/src/eedb/widgets/HomePage.cpp index ed68934..d319c6e 100644 --- a/src/eedb/widgets/HomePage.cpp +++ b/src/eedb/widgets/HomePage.cpp @@ -39,7 +39,9 @@ auto make_tree() { return tree; } -eedb::Home::Home(Wt::WContainerWidget * container, Wt::Auth::Login * login) : _login(login) { +eedb::Home::Home(eedb::Session & session) : _session(session) {} + +void Home::show(Wt::WContainerWidget * container) { // Create a navigation bar with a link to a web page. auto navigation = new Wt::WNavigationBar(); navigation->setTitle("eedb", "http://eedb.pl"); @@ -90,19 +92,13 @@ eedb::Home::Home(Wt::WContainerWidget * container, Wt::Auth::Login * login) : _l Wt::WStackedWidget * contents = new Wt::WStackedWidget(); _categoryTree = make_tree(); - - _categoryTree->clicked().connect([=](auto...) { - Wt::log("notice") << "Item selection changed"; - }); - - _categoryTree->mouseWentDown().connect([=](auto...) { - Wt::log("notice") << "Mouse went down"; - }); - - _categoryTree->mouseWentUp().connect([=](auto...) { - Wt::log("notice") << "Mouse went up"; - }); - + + _categoryTree->clicked().connect([=](auto...) { Wt::log("notice") << "Item selection changed"; }); + + _categoryTree->mouseWentDown().connect([=](auto...) { Wt::log("notice") << "Mouse went down"; }); + + _categoryTree->mouseWentUp().connect([=](auto...) { Wt::log("notice") << "Mouse went up"; }); + hbox->addWidget(_categoryTree, 1); hbox->addWidget(contents, 2); hbox->setResizable(0, true); diff --git a/src/eedb/widgets/HomePage.hpp b/src/eedb/widgets/HomePage.hpp index 243aa70..f4a1e2c 100644 --- a/src/eedb/widgets/HomePage.hpp +++ b/src/eedb/widgets/HomePage.hpp @@ -6,10 +6,15 @@ namespace Wt { class WContainerWidget; class WTreeView; } + namespace Wt::Auth { class Login; } +namespace eedb { +class Session; +} + namespace eedb { class HomePage { @@ -18,11 +23,12 @@ class HomePage { class Home final : public HomePage { public: - Home(Wt::WContainerWidget * container, Wt::Auth::Login * login); + Home(Session & session); - void show(Wt::WContainerWidget * parent) override {} + void show(Wt::WContainerWidget * parent) override; private: + Session & _session; const Wt::Auth::Login * _login; Wt::WTreeView * _categoryTree; };