Commit c5e979e9 authored by Kevin Whitaker's avatar Kevin Whitaker

Make sql connection public to share same connection pool. Layout calls for...

Make sql connection public to share same connection pool. Layout calls for user info from DB. Implement login path.
parent 12791439
......@@ -66,11 +66,11 @@ public:
};
std::list<AudioTrack> requestQueue;
std::list<PlayerEvent> 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;
......
......@@ -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>("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/WText>
#include "../WebInterface.h"
#include <Wt/Dbo/Session>
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
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment