First version of the Users view (read only)

This commit is contained in:
emeric
2018-03-29 14:00:49 +02:00
parent 3aa006c921
commit 05fd8a47d8
14 changed files with 225 additions and 63 deletions
+55 -32
View File
@@ -37,8 +37,9 @@
#include "PlayQueueView.hpp"
#include "SettingsView.hpp"
#include "admin/InitWizardView.hpp"
#include "admin/DatabaseSettingsView.hpp"
#include "admin/AdminWizardView.hpp"
#include "admin/UsersView.hpp"
#include "resource/ImageResource.hpp"
#include "resource/TranscodeResource.hpp"
@@ -86,6 +87,7 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env, Wt::Dbo::SqlConnecti
// Add a resource bundle
messageResourceBundle().use(appRoot() + "admin-database");
messageResourceBundle().use(appRoot() + "admin-users");
messageResourceBundle().use(appRoot() + "admin-wizard");
messageResourceBundle().use(appRoot() + "artist");
messageResourceBundle().use(appRoot() + "artists");
@@ -115,7 +117,7 @@ LmsApplication::LmsApplication(const Wt::WEnvironment& env, Wt::Dbo::SqlConnecti
if (firstConnection)
{
root()->addWidget(new AdminWizardView());
root()->addWidget(new InitWizardView());
}
else
{
@@ -208,31 +210,41 @@ enum IdxRoot
IdxPlayQueue,
IdxSettings,
IdxAdminDatabase,
IdxAdminUsers,
};
static void
handlePathChange(Wt::WStackedWidget* stack)
handlePathChange(Wt::WStackedWidget* stack, bool isAdmin)
{
static const std::map<std::string, int> indexes =
static const struct
{
{ "/home", IdxHome },
{ "/artists", IdxExplore },
{ "/artist", IdxExplore },
{ "/releases", IdxExplore },
{ "/release", IdxExplore },
{ "/tracks", IdxExplore },
{ "/playqueue", IdxPlayQueue },
{ "/settings", IdxSettings },
{ "/admin/database", IdxAdminDatabase },
std::string path;
int index;
bool admin;
} views[] =
{
{ "/home", IdxHome, false },
{ "/artists", IdxExplore, false },
{ "/artist", IdxExplore, false },
{ "/releases", IdxExplore, false },
{ "/release", IdxExplore, false },
{ "/tracks", IdxExplore, false },
{ "/playqueue", IdxPlayQueue, false },
{ "/settings", IdxSettings, false },
{ "/admin/database", IdxAdminDatabase, true },
{ "/admin/users", IdxAdminUsers, true },
};
LMS_LOG(UI, DEBUG) << "Internal path changed to '" << wApp->internalPath() << "'";
for (auto index : indexes)
for (auto& view : views)
{
if (wApp->internalPathMatches(index.first))
if (wApp->internalPathMatches(view.path))
{
stack->setCurrentIndex(index.second);
if (view.admin && !isAdmin)
break;
stack->setCurrentIndex(view.index);
return;
}
}
@@ -253,6 +265,11 @@ LmsApplication::handleAuthEvent(void)
LMS_LOG(UI, INFO) << "User '" << CurrentAuthUser().identity(Wt::Auth::Identity::LoginName) << "' logged in from '" << Wt::WApplication::instance()->environment().clientAddress() << "', user agent = " << Wt::WApplication::instance()->environment().agent() << ", session = " << Wt::WApplication::instance()->sessionId();
{
Wt::Dbo::Transaction transaction (DboSession());
_isAdmin = CurrentUser()->isAdmin();
}
require("/js/mediaplayer.js");
_imageResource = new ImageResource(_db, root());
@@ -295,21 +312,21 @@ LmsApplication::handleAuthEvent(void)
auto rightMenu = new Wt::WMenu();
std::size_t itemCounter = 0;
if (_isAdmin)
{
Wt::Dbo::Transaction transaction (DboSession());
auto menuItem = rightMenu->insertItem(itemCounter++, Wt::WString::tr("Lms.administration"));
menuItem->setSelectable(false);
if (CurrentUser()->isAdmin())
{
auto menuItem = rightMenu->insertItem(itemCounter++, Wt::WString::tr("Lms.administration"));
menuItem->setSelectable(false);
Wt::WPopupMenu *admin = new Wt::WPopupMenu();
auto dbSettings = admin->insertItem(0, Wt::WString::tr("Lms.Admin.Database.database"));
dbSettings->setLink(Wt::WLink(Wt::WLink::InternalPath, "/admin/database"));
dbSettings->setSelectable(false);
Wt::WPopupMenu *admin = new Wt::WPopupMenu();
auto dbSettings = admin->insertItem(0, Wt::WString::tr("Lms.Admin.Database.database"));
dbSettings->setLink(Wt::WLink(Wt::WLink::InternalPath, "/admin/database"));
dbSettings->setSelectable(false);
auto usersSettings = admin->insertItem(1, Wt::WString::tr("Lms.Admin.Users.users"));
usersSettings->setLink(Wt::WLink(Wt::WLink::InternalPath, "/admin/users"));
usersSettings->setSelectable(false);
menuItem->setMenu(admin);
}
menuItem->setMenu(admin);
}
{
@@ -344,8 +361,14 @@ LmsApplication::handleAuthEvent(void)
mainStack->addWidget(settings);
// Admin stuff
auto databaseSettings = new DatabaseSettingsView();
mainStack->addWidget(databaseSettings);
if (_isAdmin)
{
auto databaseSettings = new DatabaseSettingsView();
mainStack->addWidget(databaseSettings);
auto users = new UsersView();
mainStack->addWidget(users);
}
explore->tracksAdd.connect(std::bind([=] (std::vector<Database::Track::pointer> tracks)
{
@@ -380,7 +403,7 @@ LmsApplication::handleAuthEvent(void)
playqueue->playbackStop.connect(player, &MediaPlayer::stop);
// Events from MediaScanner
if (CurrentUser()->isAdmin())
if (_isAdmin)
{
enableUpdates(true);
std::string sessionId = this->sessionId();
@@ -401,10 +424,10 @@ LmsApplication::handleAuthEvent(void)
internalPathChanged().connect(std::bind([=]
{
handlePathChange(mainStack);
handlePathChange(mainStack, _isAdmin);
}));
handlePathChange(mainStack);
handlePathChange(mainStack, _isAdmin);
}
void
+1
View File
@@ -65,6 +65,7 @@ class LmsApplication : public Wt::WApplication
Scanner::MediaScanner& _scanner;
ImageResource* _imageResource;
TranscodeResource* _transcodeResource;
bool _isAdmin = false;
};
// Helpers to get session data
@@ -27,11 +27,11 @@
#include "common/Validators.hpp"
#include "LmsApplication.hpp"
#include "AdminWizardView.hpp"
#include "InitWizardView.hpp"
namespace UserInterface {
class AdminWizardModel : public Wt::WFormModel
class InitWizardModel : public Wt::WFormModel
{
public:
@@ -40,7 +40,7 @@ class AdminWizardModel : public Wt::WFormModel
static const Field PasswordField;
static const Field PasswordConfirmField;
AdminWizardModel(Wt::WObject *parent = 0)
InitWizardModel(Wt::WObject *parent = 0)
: Wt::WFormModel(parent)
{
addField(AdminLoginField);
@@ -112,31 +112,31 @@ class AdminWizardModel : public Wt::WFormModel
};
const Wt::WFormModel::Field AdminWizardModel::AdminLoginField = "admin-login";
const Wt::WFormModel::Field AdminWizardModel::PasswordField = "password";
const Wt::WFormModel::Field AdminWizardModel::PasswordConfirmField = "password-confirm";
const Wt::WFormModel::Field InitWizardModel::AdminLoginField = "admin-login";
const Wt::WFormModel::Field InitWizardModel::PasswordField = "password";
const Wt::WFormModel::Field InitWizardModel::PasswordConfirmField = "password-confirm";
AdminWizardView::AdminWizardView(Wt::WContainerWidget *parent)
InitWizardView::InitWizardView(Wt::WContainerWidget *parent)
: Wt::WTemplateFormView(parent)
{
auto model = new AdminWizardModel(this);
auto model = new InitWizardModel(this);
setTemplateText(Wt::WString::tr("Lms.AdminWizard.template"));
setTemplateText(Wt::WString::tr("Lms.Admin.InitWizard.template"));
addFunction("tr", &WTemplate::Functions::tr);
addFunction("id", &WTemplate::Functions::id);
// AdminLogin
Wt::WLineEdit* accountEdit = new Wt::WLineEdit();
setFormWidget(AdminWizardModel::AdminLoginField, accountEdit);
setFormWidget(InitWizardModel::AdminLoginField, accountEdit);
// Password
Wt::WLineEdit* passwordEdit = new Wt::WLineEdit();
setFormWidget(AdminWizardModel::PasswordField, passwordEdit );
setFormWidget(InitWizardModel::PasswordField, passwordEdit );
passwordEdit->setEchoMode(Wt::WLineEdit::Password);
// Password confirmation
Wt::WLineEdit* passwordConfirmEdit = new Wt::WLineEdit();
setFormWidget(AdminWizardModel::PasswordConfirmField, passwordConfirmEdit);
setFormWidget(InitWizardModel::PasswordConfirmField, passwordConfirmEdit);
passwordConfirmEdit->setEchoMode(Wt::WLineEdit::Password);
auto saveButton = new Wt::WPushButton(Wt::WString::tr("Lms.create"));
@@ -148,7 +148,7 @@ AdminWizardView::AdminWizardView(Wt::WContainerWidget *parent)
if (model->validate())
{
model->saveData();
LmsApp->notify(Wt::WString::tr("Lms.AdminWizard.done"));
LmsApp->notify(Wt::WString::tr("Lms.Admin.InitWizard.done"));
saveButton->setEnabled(false);
}
@@ -24,10 +24,10 @@
namespace UserInterface {
class AdminWizardView : public Wt::WTemplateFormView
class InitWizardView : public Wt::WTemplateFormView
{
public:
AdminWizardView(Wt::WContainerWidget *parent = 0);
InitWizardView(Wt::WContainerWidget *parent = 0);
};
+83
View File
@@ -0,0 +1,83 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Wt/WTemplate>
#include <Wt/WPushButton>
#include "database/Types.hpp"
#include "utils/Logger.hpp"
#include "LmsApplication.hpp"
#include "UsersView.hpp"
namespace UserInterface {
UsersView::UsersView(Wt::WContainerWidget *parent)
: Wt::WContainerWidget(parent)
{
LMS_LOG(UI, DEBUG) << "TEST";
auto t = new Wt::WTemplate(Wt::WString::tr("Lms.Admin.Users.template"), this);
t->addFunction("tr", &Wt::WTemplate::Functions::tr);
_container = new Wt::WContainerWidget();
t->bindWidget("users", _container);
auto addBtn = new Wt::WPushButton(Wt::WString::tr("Lms.Admin.Users.add"));
t->bindWidget("add-btn", addBtn);
refresh();
}
void
UsersView::refresh()
{
_container->clear();
Wt::Dbo::Transaction transaction(DboSession());
auto users = Database::User::getAll(DboSession());
for (auto user : users)
{
auto entry = new Wt::WTemplate(Wt::WString::tr("Lms.Admin.Users.template.entry"), _container);
Wt::Auth::User authUser = DbHandler().getUserDatabase().findWithId(std::to_string(user.id()));
if (!authUser.isValid()) {
LMS_LOG(UI, ERROR) << "Skipping invalid userId = " << user.id();
continue;
}
entry->bindString("name", authUser.identity(Wt::Auth::Identity::LoginName), Wt::PlainText);
// Don't edit ourself this way
if (CurrentUser() != user)
{
entry->setCondition("if-edit", true);
auto editBtn = new Wt::WPushButton(Wt::WString::tr("Lms.Admin.Users.edit"));
entry->bindWidget("edit-btn", editBtn);
auto delBtn = new Wt::WPushButton(Wt::WString::tr("Lms.Admin.Users.del"));
entry->bindWidget("del-btn", delBtn);
}
}
}
} // namespace UserInterface
+39
View File
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS 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 3 of the License, or
* (at your option) any later version.
*
* LMS 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 LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <Wt/WContainerWidget>
namespace UserInterface {
class UsersView : public Wt::WContainerWidget
{
public:
UsersView(Wt::WContainerWidget *parent = 0);
private:
void refresh();
Wt::WContainerWidget* _container;
};
} // namespace UserInterface
+1 -1
View File
@@ -87,7 +87,7 @@ Artists::addSome()
for (auto artist : artists)
{
Wt::WTemplate* entry = new Wt::WTemplate(Wt::WString::tr("Lms.Explore.Artists.template.entry"), _artistsContainer);
auto entry = new Wt::WTemplate(Wt::WString::tr("Lms.Explore.Artists.template.entry"), _artistsContainer);
entry->bindInt("nb-release", artist->getReleases(clusterIds).size());