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 "AdminInterface.h"
#include "../db/UserAction.h"
AdminInterface::AdminInterface(WebInterface* app)
{
this->app = app;
this->setMaximumSize(Wt::WLength::Auto,Wt::WLength(175,Wt::WLength::Pixel));
this->setOverflow(OverflowHidden);
pageLayout = new Wt::WVBoxLayout();
userLayout = new Wt::WHBoxLayout();
actionLayout = new Wt::WHBoxLayout();
usernameField = new Wt::WLineEdit();
passwordField = new Wt::WLineEdit();
addBtn = new Wt::WPushButton("");
changeBtn = new Wt::WPushButton("");
this->setLayout(pageLayout);
pageLayout->addLayout(userLayout);
pageLayout->addLayout(actionLayout);
userLayout->addWidget(usernameField);
userLayout->addWidget(passwordField);
actionLayout->addWidget(addBtn,0,Wt::AlignmentFlag::AlignLeft);
actionLayout->addWidget(changeBtn,0,Wt::AlignmentFlag::AlignLeft);
passwordField->setEchoMode(Wt::WLineEdit::EchoMode::Password);
addBtn->setTextFormat(Wt::XHTMLText);
addBtn->decorationStyle().font().setFamily(Wt::WFont::Default,"FontAwesome");
addBtn->clicked().connect(this, &AdminInterface::addClicked);
changeBtn->setTextFormat(Wt::XHTMLText);
changeBtn->decorationStyle().font().setFamily(Wt::WFont::Default,"FontAwesome");
changeBtn->clicked().connect(this, &AdminInterface::changeClicked);
AdminInterface::~AdminInterface()
{
}
void AdminInterface::addClicked()
{
Wt::Dbo::Session sqlSession;
sqlSession.setConnectionPool(*GroovePlayerMgr::getInstance()->connectionPool);
sqlSession.mapClass<User>("user");
sqlSession.mapClass<AudioTrack>("tracks");
sqlSession.mapClass<UserAction>("actions");
Wt::Dbo::Transaction addUserTransaction(sqlSession);
//Make sure user is valid to add
if(usernameField->text().empty() || passwordField->text().empty())
{
usernameField->setText("");
passwordField->setText("");
return;
}
int usernameMatching = sqlSession.query<int>("select count(username) from user").where("username = ?").bind(usernameField->text().toUTF8());
if(usernameMatching > 0)
{
usernameField->setText("already exists");
return;
}
//Add user
app->createUser(&sqlSession, usernameField->text().toUTF8(), passwordField->text().toUTF8(), false);
usernameField->setText("created user");
passwordField->setText("");
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
void AdminInterface::changeClicked()
{
Wt::Dbo::Session sqlSession;
sqlSession.setConnectionPool(*GroovePlayerMgr::getInstance()->connectionPool);
sqlSession.mapClass<User>("user");
sqlSession.mapClass<AudioTrack>("tracks");
sqlSession.mapClass<UserAction>("actions");
Wt::Dbo::Transaction changeUserTransaction(sqlSession);
//Make sure user is valid to add
if(usernameField->text().empty() || passwordField->text().empty())
{
usernameField->setText("");
passwordField->setText("");
return;
}
int usernameMatching = sqlSession.query<int>("select count(username) from user").where("username = ?").bind(usernameField->text().toUTF8());
if(usernameMatching > 0)
{
Wt::Dbo::ptr<User> user = sqlSession.find<User>().where("username = ?").bind(usernameField->text().toUTF8());
Wt::Auth::BCryptHashFunction hasher;
user.modify()->passwordHash = hasher.compute(passwordField->text().toUTF8(), user->passwordSalt);
usernameField->setText("User password changed.");
passwordField->setText("");
}
else
{
usernameField->setText("User doesn't exist");
passwordField->setText("");
}
changeUserTransaction.commit();
}