diff --git a/src/ui/AdminInterface.cpp b/src/ui/AdminInterface.cpp index f639ac1812028287b35adad0b14969d335c70f60..4c22d8f919993ca5ba9906dac745ab432e2e3f37 100644 --- a/src/ui/AdminInterface.cpp +++ b/src/ui/AdminInterface.cpp @@ -19,25 +19,35 @@ #include "AdminInterface.h" #include "../db/UserAction.h" +#include 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(userLayout); + this->setLayout(pageLayout); + pageLayout->addLayout(userLayout); + pageLayout->addLayout(actionLayout); userLayout->addWidget(usernameField); userLayout->addWidget(passwordField); - userLayout->addWidget(addBtn); + 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() @@ -76,3 +86,36 @@ void AdminInterface::addClicked() usernameField->setText("created user"); passwordField->setText(""); } + +void AdminInterface::changeClicked() +{ + Wt::Dbo::Session sqlSession; + sqlSession.setConnectionPool(*GroovePlayerMgr::getInstance()->connectionPool); + sqlSession.mapClass("user"); + sqlSession.mapClass("tracks"); + sqlSession.mapClass("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("select count(username) from user").where("username = ?").bind(usernameField->text().toUTF8()); + if(usernameMatching > 0) + { + Wt::Dbo::ptr user = sqlSession.find().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(); +} diff --git a/src/ui/AdminInterface.h b/src/ui/AdminInterface.h index be07b4719d05b1d2a34070a0ed6fa353855df055..12b88c3af29ab321becb3f411dc15647c5c186a7 100644 --- a/src/ui/AdminInterface.h +++ b/src/ui/AdminInterface.h @@ -23,6 +23,7 @@ #include #include "../WebInterface.h" #include +#include #include #include @@ -33,12 +34,16 @@ public: ~AdminInterface(); WebInterface* app; + Wt::WVBoxLayout* pageLayout; Wt::WHBoxLayout* userLayout; + Wt::WHBoxLayout* actionLayout; Wt::WLineEdit* usernameField; Wt::WLineEdit* passwordField; Wt::WPushButton* addBtn; + Wt::WPushButton* changeBtn; void addClicked(); + void changeClicked(); }; #endif // ADMININTERFACE_H