diff --git a/Makefile.am b/Makefile.am index 32d9c93b..c94131c4 100644 --- a/Makefile.am +++ b/Makefile.am @@ -65,8 +65,9 @@ lms_SOURCES = \ $(top_srcdir)/ui/video/VideoDatabaseWidget.cpp \ $(top_srcdir)/ui/video/VideoMediaPlayerWidget.cpp \ $(top_srcdir)/ui/video/VideoParametersDialog.cpp \ - $(top_srcdir)/ui/settings/Settings.cpp \ - $(top_srcdir)/ui/settings/SettingsDatabaseFormView.cpp + $(top_srcdir)/ui/settings/Settings.cpp \ + $(top_srcdir)/ui/settings/SettingsDatabaseFormView.cpp \ + $(top_srcdir)/ui/settings/SettingsUsers.cpp lms_CXXFLAGS=-std=c++11 -Wall -I$(top_srcdir)/boost/ -I$(top_srcdir)/ui -I$(top_srcdir)/remote diff --git a/TODO b/TODO index 7f82397c..34c3a47c 100644 --- a/TODO +++ b/TODO @@ -17,6 +17,7 @@ - Add path to Audio/Video files - Do not make the update start time active when update perdiod is "Never" - Do not restart the db update service if user applied no changes + - Uncheck the "Request immediate scan" once setins are applied - [user/transcoding] - Prefered bitrate and codecs for audio/video - [user/Personal] diff --git a/database/User.cpp b/database/User.cpp index e08a2566..7df00512 100644 --- a/database/User.cpp +++ b/database/User.cpp @@ -14,6 +14,12 @@ _videoBitrate(defaultVideoBitrate) } +std::vector +User::getAll(Wt::Dbo::Session& session) +{ + Wt::Dbo::collection res = session.find(); + return std::vector(res.begin(), res.end()); +} diff --git a/database/User.hpp b/database/User.hpp index d075cdf6..9288ff46 100644 --- a/database/User.hpp +++ b/database/User.hpp @@ -17,6 +17,9 @@ class User { typedef Wt::Dbo::ptr pointer; + // accessors + static std::vector getAll(Wt::Dbo::Session& session); + bool isAdmin() const {return _isAdmin;} template diff --git a/ui/LmsApplication.cpp b/ui/LmsApplication.cpp index fbcd1fd0..75b380ad 100644 --- a/ui/LmsApplication.cpp +++ b/ui/LmsApplication.cpp @@ -57,7 +57,6 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env, boost::filesystem::p void LmsApplication::handleAuthEvent(void) { - _sessionData.getDatabaseHandler().getLogin().changed().connect(this, &LmsApplication::handleAuthEvent); if (_sessionData.getDatabaseHandler().getLogin().loggedIn()) { if (_home == nullptr) { diff --git a/ui/settings/Settings.cpp b/ui/settings/Settings.cpp index 1b762d71..fece783a 100644 --- a/ui/settings/Settings.cpp +++ b/ui/settings/Settings.cpp @@ -3,6 +3,7 @@ #include #include "SettingsDatabaseFormView.hpp" +#include "SettingsUsers.hpp" #include "Settings.hpp" @@ -31,7 +32,7 @@ _sessionData(sessionData) { // TODO Special admin settings menu->addItem("Database", new DatabaseFormView(sessionData)); - menu->addItem("Users", new Wt::WTextArea("Users here!")); + menu->addItem("Users", new Users(sessionData)); } // User specifics settings diff --git a/ui/settings/SettingsUsers.cpp b/ui/settings/SettingsUsers.cpp new file mode 100644 index 00000000..6d4b9ab1 --- /dev/null +++ b/ui/settings/SettingsUsers.cpp @@ -0,0 +1,122 @@ + +#include +#include +#include +#include + +#include + +#include "SettingsUsers.hpp" + +namespace UserInterface { +namespace Settings { + +Users::Users(SessionData& sessionData, Wt::WContainerWidget *parent) +: Wt::WContainerWidget(parent), +_sessionData(sessionData) +{ + + Wt::WGroupBox *container = new Wt::WGroupBox("Users", this); + + _table = new Wt::WTable(container); + + _table->addStyleClass("table form-inline"); + + _table->toggleStyleClass("table-hover", true); + _table->toggleStyleClass("table-striped", true); + + _table->setHeaderCount(1); + + _table->elementAt(0, 0)->addWidget(new Wt::WText("#")); + _table->elementAt(0, 1)->addWidget(new Wt::WText("Name")); + _table->elementAt(0, 2)->addWidget(new Wt::WText("e-Mail")); + _table->elementAt(0, 3)->addWidget(new Wt::WText("Admin")); + + Database::Handler& db = sessionData.getDatabaseHandler(); + + Wt::Dbo::Transaction transaction(db.getSession()); + + std::vector users = Database::User::getAll(db.getSession()); + + for (std::size_t i = 0; i < users.size(); ++i) + { + + std::string userId; + { + std::ostringstream oss; oss << users[i].id(); + userId = oss.str(); + } + + Wt::Auth::User authUser = db.getUserDatabase().findWithId( userId ); + + _table->elementAt(i + 1, 0)->addWidget(new Wt::WText( Wt::WString::fromUTF8("{1}").arg(i+1))); + _table->elementAt(i + 1, 1)->addWidget(new Wt::WText( authUser.identity(Wt::Auth::Identity::LoginName)) ); + + Wt::WText* email = new Wt::WText(); + if (!authUser.email().empty()) { + email->setText(authUser.email()); + } + else { + email->setStyleClass("alert-danger"); + email->setText(authUser.unverifiedEmail()); + } + + _table->elementAt(i + 1, 2)->addWidget( email ) ; + + _table->elementAt(i + 1, 3)->addWidget(new Wt::WText( users[i]->isAdmin() ? "Yes" : "No" )); + + Wt::WPushButton* editBtn = new Wt::WPushButton("Edit"); + _table->elementAt(i + 1, 4)->addWidget(editBtn); + + Wt::WPushButton* delBtn = new Wt::WPushButton("Delete"); + delBtn->setStyleClass("btn-danger"); + delBtn->setMargin(5, Wt::Left); + + delBtn->clicked().connect(boost::bind( &Users::handleDelUser, this, authUser.identity(Wt::Auth::Identity::LoginName), userId)); + + _table->elementAt(i + 1, 4)->addWidget(delBtn); + } + + Wt::WPushButton* addBtn = new Wt::WPushButton("Add User"); + addBtn->setStyleClass("btn-success"); + + addWidget( addBtn ); +} + +void +Users::handleDelUser(Wt::WString loginNameIdentity, std::string id) +{ + Wt::WMessageBox *messageBox = new Wt::WMessageBox + ("Delete User", + Wt::WString( "Deleting user '{1}'?").arg(loginNameIdentity), + Wt::Question, Wt::Yes | Wt::No); + + messageBox->setModal(false); + + messageBox->buttonClicked().connect(std::bind([=] () { + if (messageBox->buttonResult() == Wt::Yes) + { + Database::Handler& db = _sessionData.getDatabaseHandler(); + + // Delete the user + Wt::Auth::User authUser = db.getUserDatabase().findWithId( id ); + + db.getUserDatabase().deleteUser( authUser ); + + // TODO remove Database::User too? + + // TODO Update the view + } + + delete messageBox; + })); + + messageBox->show(); + + +} + +} // namespace UserInterface +} // namespace Settings + + diff --git a/ui/settings/SettingsUsers.hpp b/ui/settings/SettingsUsers.hpp new file mode 100644 index 00000000..c06adf96 --- /dev/null +++ b/ui/settings/SettingsUsers.hpp @@ -0,0 +1,30 @@ +#ifndef UI_SETTINGS_USERS_HPP +#define UI_SETTINGS_USERS_HPP + +#include +#include + +#include "common/SessionData.hpp" + +namespace UserInterface { +namespace Settings { + +class Users : public Wt::WContainerWidget +{ + public: + Users(SessionData& sessioNData, Wt::WContainerWidget *parent = 0); + + private: + + void handleDelUser(Wt::WString loginNameIdentity, std::string id); + + SessionData& _sessionData; + + Wt::WTable* _table; +}; + +} // namespace UserInterface +} // namespace Settings + +#endif +