71 lines
2.5 KiB
C++
71 lines
2.5 KiB
C++
#include "EditUsers.h"
|
|
#include <Wt/WApplication.h>
|
|
#include <Wt/WBreak.h>
|
|
#include <Wt/WLineEdit.h>
|
|
#include <Wt/WPushButton.h>
|
|
|
|
EditUsers::EditUsers(Wt::Dbo::Session &aSession, const std::string &basePath)
|
|
: m_session(aSession), m_basePath(basePath) {
|
|
setStyleClass("user-editor");
|
|
setTemplateText(tr("edit-users-list"));
|
|
auto limitEdit = std::make_unique<Wt::WLineEdit>();
|
|
auto goLimit = std::make_unique<Wt::WPushButton>(tr("go-limit"));
|
|
goLimit->clicked().connect(this, &EditUsers::limitList);
|
|
|
|
m_limitEdit = bindWidget("limit-edit", std::move(limitEdit));
|
|
bindWidget("limit-button", std::move(goLimit));
|
|
limitList();
|
|
}
|
|
|
|
void EditUsers::onUserClicked(Wt::Dbo::dbo_traits<User>::IdType id) {
|
|
Wt::WApplication::instance()->setInternalPath(m_basePath + "edituser/" + std::to_string(id), true);
|
|
}
|
|
|
|
void EditUsers::limitList() {
|
|
auto listPtr = std::make_unique<Wt::WContainerWidget>();
|
|
auto list = listPtr.get();
|
|
bindWidget("user-list", std::move(listPtr));
|
|
|
|
typedef Wt::Dbo ::collection<Wt::Dbo ::ptr<User>> UserList;
|
|
Wt::Dbo ::Transaction t(m_session);
|
|
UserList users = m_session.find<User>().where("name like ?").bind("%" + m_limitEdit->text() + "%").orderBy("name");
|
|
|
|
for (auto user : users) {
|
|
Wt::WText *t = list->addWidget(std::make_unique<Wt::WText>(user->name));
|
|
t->setStyleClass("link");
|
|
list->addWidget(std::make_unique<Wt::WBreak>());
|
|
t->clicked().connect(std::bind(&EditUsers::onUserClicked, this, user.id()));
|
|
}
|
|
if (!users.size()) list->addWidget(std::make_unique<Wt::WText>(tr("no-users-found")));
|
|
}
|
|
|
|
EditUser::EditUser(Wt::Dbo::Session &aSession) : WTemplate(tr("edit-user")), session_(aSession) {
|
|
auto roleButton = std::make_unique<Wt::WPushButton>();
|
|
roleButton_ = bindWidget("role-button", std::move(roleButton));
|
|
roleButton_->clicked().connect(this, &EditUser::switchRole);
|
|
}
|
|
|
|
void EditUser::switchUser(Wt::Dbo::ptr<User> target) {
|
|
target_ = target;
|
|
bindTemplate();
|
|
}
|
|
|
|
void EditUser::bindTemplate() {
|
|
bindString("username", target_->name);
|
|
if (target_->role == User::Admin)
|
|
roleButton_->setText(tr("demote-admin"));
|
|
else
|
|
roleButton_->setText(tr("promote-user"));
|
|
}
|
|
|
|
void EditUser::switchRole() {
|
|
Wt::Dbo::Transaction t(session_);
|
|
target_.reread();
|
|
if (target_->role == User::Admin)
|
|
target_.modify()->role = User::Visitor;
|
|
else
|
|
target_.modify()->role = User::Admin;
|
|
t.commit();
|
|
bindTemplate();
|
|
}
|