Newer
Older
/*
* Copyright (C) 2017 Kevin Whitaker <eyecreate@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "LoginInterface.h"
Kevin Whitaker
committed
#include "../db/UserAction.h"
LoginInterface::LoginInterface(WebInterface* app)
{
Kevin Whitaker
committed
this->app = app;
Kevin Whitaker
committed
//TODO: First check if cookie is set and if is a valid user. Skip login if valid.
Kevin Whitaker
committed
loginContainer = new Wt::WContainerWidget();
loginLayout = new Wt::WVBoxLayout();
Kevin Whitaker
committed
loginMessage = new Wt::WText("Please Login to access.");
Kevin Whitaker
committed
loginContainer->setLayout(loginLayout);
usernameArea = new Wt::WHBoxLayout();
passwordArea = new Wt::WHBoxLayout();
usernameContainer = new Wt::WContainerWidget();
passwordContainer = new Wt::WContainerWidget();
usernameContainer->setLayout(usernameArea);
passwordContainer->setLayout(passwordArea);
usernameField = new Wt::WLineEdit();
passwordField = new Wt::WLineEdit();
passwordField->setEchoMode(Wt::WLineEdit::EchoMode::Password);
usernameArea->addWidget(new Wt::WText("Username:"));
usernameArea->addWidget(usernameField);
passwordArea->addWidget(new Wt::WText("Password:"));
passwordArea->addWidget(passwordField);
Kevin Whitaker
committed
loginLayout->addWidget(loginMessage);
Kevin Whitaker
committed
loginLayout->addWidget(usernameContainer);
loginLayout->addWidget(passwordContainer);
loginButton = new Wt::WPushButton("Login");
Kevin Whitaker
committed
loginButton->clicked().connect(this, &LoginInterface::loginCheck);
setContentAlignment(Wt::AlignmentFlag::AlignCenter);
Kevin Whitaker
committed
loginLayout->addWidget(loginButton);
addChild(loginContainer);
loginContainer->setWidth(Wt::WLength(50, Wt::WLength::Percentage));
Kevin Whitaker
committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
}