diff --git a/src/GroovePlayer.h b/src/GroovePlayer.h index c683e863d901e28975edf2d661cbae2d1c800fdc..92e4886dfa2392e012a124ba15c03325a8566305 100644 --- a/src/GroovePlayer.h +++ b/src/GroovePlayer.h @@ -66,11 +66,11 @@ public: }; std::list requestQueue; std::list lastInternalEvents; + Wt::Dbo::backend::Sqlite3* sqliteConnection; + Wt::Dbo::FixedSqlConnectionPool* connectionPool; private: GroovePlayerMgr (std::string dbFile); ~GroovePlayerMgr(); - Wt::Dbo::backend::Sqlite3* sqliteConnection; - Wt::Dbo::FixedSqlConnectionPool* connectionPool; bool continueEventLoop = true; std::thread* grooveEvents; diff --git a/src/ui/LoginInterface.cpp b/src/ui/LoginInterface.cpp index 9ee626e11fd0bfc8b2e675586edeeb00c7a67799..804a72aef0d5689023a4d9713ce370c746e9ae31 100644 --- a/src/ui/LoginInterface.cpp +++ b/src/ui/LoginInterface.cpp @@ -18,12 +18,15 @@ */ #include "LoginInterface.h" +#include "../db/UserAction.h" LoginInterface::LoginInterface(WebInterface* app) { this->app = app; + //TODO: First check if cookie is set and if is a valid user. Skip login if valid. loginContainer = new Wt::WContainerWidget(); loginLayout = new Wt::WVBoxLayout(); + loginMessage = new Wt::WText("Please Login to access."); loginContainer->setLayout(loginLayout); usernameArea = new Wt::WHBoxLayout(); passwordArea = new Wt::WHBoxLayout(); @@ -38,13 +41,78 @@ LoginInterface::LoginInterface(WebInterface* app) usernameArea->addWidget(usernameField); passwordArea->addWidget(new Wt::WText("Password:")); passwordArea->addWidget(passwordField); - loginLayout->addWidget(new Wt::WText("Please Login to access.")); + loginLayout->addWidget(loginMessage); loginLayout->addWidget(usernameContainer); loginLayout->addWidget(passwordContainer); loginButton = new Wt::WPushButton("Login"); - loginButton->clicked().connect(app, &WebInterface::loginCompleted); + loginButton->clicked().connect(this, &LoginInterface::loginCheck); setContentAlignment(Wt::AlignmentFlag::AlignCenter); loginLayout->addWidget(loginButton); addChild(loginContainer); loginContainer->setWidth(Wt::WLength(50, Wt::WLength::Percentage)); } + +void LoginInterface::loginCheck() +{ + //Reset message in case of success. + loginMessage->setText("Please Login to access."); + loginMessage->decorationStyle().setForegroundColor(Wt::WColor("black")); + + //First, check if username and password are filled. + if(usernameField->text().empty() || passwordField->text().empty()) + { + loginMessage->setText("Please fill out username and password."); + loginMessage->decorationStyle().setForegroundColor(Wt::WColor("red")); + return; + } + + Wt::Dbo::Session sqlSession; + sqlSession.setConnectionPool(*GroovePlayerMgr::getInstance()->connectionPool); + sqlSession.mapClass("user"); + sqlSession.mapClass("tracks"); + sqlSession.mapClass("actions"); + + //Check if there are any users. If not, use the username/password as new admin user. + if(getUserCount(&sqlSession) < 1) + { + createUser(&sqlSession, usernameField->text().toUTF8(),passwordField->text().toUTF8(), true); + } + //Check if the credentials match anything in the DB. If so, store cookie to skip login. + if(getUserForLoginAuth(&sqlSession, usernameField->text().toUTF8(),passwordField->text().toUTF8()) != nullptr) + { + app->currentUser = *getUserForLoginAuth(&sqlSession, usernameField->text().toUTF8(),passwordField->text().toUTF8()); + setLocalCookieForUser(app->currentUser); + app->loginCompleted(); + } + //If credentials don't match, reject user with message saying crednetials are wrong and they can ask an admin to make them an account. + else + { + loginMessage->setText("Username or password is incorrect. Do you have an account?"); + loginMessage->decorationStyle().setForegroundColor(Wt::WColor("red")); + } +} + +void LoginInterface::createUser(Wt::Dbo::Session* session, std::string username, std::string rawPassword, bool isAdmin) +{ + //TODO +} + +int LoginInterface::getUserCount(Wt::Dbo::Session* session) +{ + //TODO +} + +User* LoginInterface::getUserForLoginCookie(Wt::Dbo::Session* session, std::string cookie) +{ + //TODO +} + +User* LoginInterface::getUserForLoginAuth(Wt::Dbo::Session* session, std::string username, std::string rawPassword) +{ + //TODO +} + +void LoginInterface::setLocalCookieForUser(User user) +{ + //TODO +} diff --git a/src/ui/LoginInterface.h b/src/ui/LoginInterface.h index aaca2b460af0da91996476099d72112c72045b8f..f9d8aca0616645e7c7de258ccb0b872b01f90d63 100644 --- a/src/ui/LoginInterface.h +++ b/src/ui/LoginInterface.h @@ -27,6 +27,7 @@ #include #include #include "../WebInterface.h" +#include class LoginInterface : public Wt::WContainerWidget { @@ -42,7 +43,14 @@ private: Wt::WContainerWidget* passwordContainer; Wt::WLineEdit* usernameField; Wt::WLineEdit* passwordField; + Wt::WText* loginMessage; WebInterface* app; + void loginCheck(); + void createUser(Wt::Dbo::Session* session, std::string username, std::string rawPassword, bool isAdmin); + int getUserCount(Wt::Dbo::Session* session); + User* getUserForLoginCookie(Wt::Dbo::Session* session, std::string cookie); + User* getUserForLoginAuth(Wt::Dbo::Session* session, std::string username, std::string rawPassword); + void setLocalCookieForUser(User user); }; #endif // LOGININTERFACE_H