From e2ec2f21524a12cda7632f5e33c19631765e04e8 Mon Sep 17 00:00:00 2001 From: Kevin Whitaker Date: Fri, 24 Feb 2017 17:17:13 -0500 Subject: [PATCH] Worked changing password into admin. --- src/ui/AdminInterface.cpp | 47 +++++++++++++++++++++++++++++++++++++-- src/ui/AdminInterface.h | 5 +++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/ui/AdminInterface.cpp b/src/ui/AdminInterface.cpp index f639ac1..4c22d8f 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 be07b47..12b88c3 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 -- GitLab