Make sql connection public to share same connection pool. Layout calls for user info from DB. Implement login path.
This commit is contained in:
@@ -66,11 +66,11 @@ public:
|
|||||||
};
|
};
|
||||||
std::list<AudioTrack> requestQueue;
|
std::list<AudioTrack> requestQueue;
|
||||||
std::list<PlayerEvent> lastInternalEvents;
|
std::list<PlayerEvent> lastInternalEvents;
|
||||||
|
Wt::Dbo::backend::Sqlite3* sqliteConnection;
|
||||||
|
Wt::Dbo::FixedSqlConnectionPool* connectionPool;
|
||||||
private:
|
private:
|
||||||
GroovePlayerMgr (std::string dbFile);
|
GroovePlayerMgr (std::string dbFile);
|
||||||
~GroovePlayerMgr();
|
~GroovePlayerMgr();
|
||||||
Wt::Dbo::backend::Sqlite3* sqliteConnection;
|
|
||||||
Wt::Dbo::FixedSqlConnectionPool* connectionPool;
|
|
||||||
bool continueEventLoop = true;
|
bool continueEventLoop = true;
|
||||||
|
|
||||||
std::thread* grooveEvents;
|
std::thread* grooveEvents;
|
||||||
|
|||||||
@@ -18,12 +18,15 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "LoginInterface.h"
|
#include "LoginInterface.h"
|
||||||
|
#include "../db/UserAction.h"
|
||||||
|
|
||||||
LoginInterface::LoginInterface(WebInterface* app)
|
LoginInterface::LoginInterface(WebInterface* app)
|
||||||
{
|
{
|
||||||
this->app = 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();
|
loginContainer = new Wt::WContainerWidget();
|
||||||
loginLayout = new Wt::WVBoxLayout();
|
loginLayout = new Wt::WVBoxLayout();
|
||||||
|
loginMessage = new Wt::WText("Please Login to access.");
|
||||||
loginContainer->setLayout(loginLayout);
|
loginContainer->setLayout(loginLayout);
|
||||||
usernameArea = new Wt::WHBoxLayout();
|
usernameArea = new Wt::WHBoxLayout();
|
||||||
passwordArea = new Wt::WHBoxLayout();
|
passwordArea = new Wt::WHBoxLayout();
|
||||||
@@ -38,13 +41,78 @@ LoginInterface::LoginInterface(WebInterface* app)
|
|||||||
usernameArea->addWidget(usernameField);
|
usernameArea->addWidget(usernameField);
|
||||||
passwordArea->addWidget(new Wt::WText("Password:"));
|
passwordArea->addWidget(new Wt::WText("Password:"));
|
||||||
passwordArea->addWidget(passwordField);
|
passwordArea->addWidget(passwordField);
|
||||||
loginLayout->addWidget(new Wt::WText("Please Login to access."));
|
loginLayout->addWidget(loginMessage);
|
||||||
loginLayout->addWidget(usernameContainer);
|
loginLayout->addWidget(usernameContainer);
|
||||||
loginLayout->addWidget(passwordContainer);
|
loginLayout->addWidget(passwordContainer);
|
||||||
loginButton = new Wt::WPushButton("Login");
|
loginButton = new Wt::WPushButton("Login");
|
||||||
loginButton->clicked().connect(app, &WebInterface::loginCompleted);
|
loginButton->clicked().connect(this, &LoginInterface::loginCheck);
|
||||||
setContentAlignment(Wt::AlignmentFlag::AlignCenter);
|
setContentAlignment(Wt::AlignmentFlag::AlignCenter);
|
||||||
loginLayout->addWidget(loginButton);
|
loginLayout->addWidget(loginButton);
|
||||||
addChild(loginContainer);
|
addChild(loginContainer);
|
||||||
loginContainer->setWidth(Wt::WLength(50, Wt::WLength::Percentage));
|
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>("user");
|
||||||
|
sqlSession.mapClass<AudioTrack>("tracks");
|
||||||
|
sqlSession.mapClass<UserAction>("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
|
||||||
|
}
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include <Wt/WLineEdit>
|
#include <Wt/WLineEdit>
|
||||||
#include <Wt/WText>
|
#include <Wt/WText>
|
||||||
#include "../WebInterface.h"
|
#include "../WebInterface.h"
|
||||||
|
#include <Wt/Dbo/Session>
|
||||||
|
|
||||||
class LoginInterface : public Wt::WContainerWidget
|
class LoginInterface : public Wt::WContainerWidget
|
||||||
{
|
{
|
||||||
@@ -42,7 +43,14 @@ private:
|
|||||||
Wt::WContainerWidget* passwordContainer;
|
Wt::WContainerWidget* passwordContainer;
|
||||||
Wt::WLineEdit* usernameField;
|
Wt::WLineEdit* usernameField;
|
||||||
Wt::WLineEdit* passwordField;
|
Wt::WLineEdit* passwordField;
|
||||||
|
Wt::WText* loginMessage;
|
||||||
WebInterface* app;
|
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
|
#endif // LOGININTERFACE_H
|
||||||
|
|||||||
Reference in New Issue
Block a user