#include "EditUsers.h" #include #include #include #include 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(); auto goLimit = std::make_unique(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::IdType id) { Wt::WApplication::instance()->setInternalPath(m_basePath + "edituser/" + std::to_string(id), true); } void EditUsers::limitList() { auto listPtr = std::make_unique(); auto list = listPtr.get(); bindWidget("user-list", std::move(listPtr)); typedef Wt::Dbo ::collection> UserList; Wt::Dbo ::Transaction t(m_session); UserList users = m_session.find().where("name like ?").bind("%" + m_limitEdit->text() + "%").orderBy("name"); for (auto user : users) { Wt::WText *t = list->addWidget(std::make_unique(user->name)); t->setStyleClass("link"); list->addWidget(std::make_unique()); t->clicked().connect(std::bind(&EditUsers::onUserClicked, this, user.id())); } if (!users.size()) list->addWidget(std::make_unique(tr("no-users-found"))); } EditUser::EditUser(Wt::Dbo::Session &aSession) : WTemplate(tr("edit-user")), session_(aSession) { auto roleButton = std::make_unique(); roleButton_ = bindWidget("role-button", std::move(roleButton)); roleButton_->clicked().connect(this, &EditUser::switchRole); } void EditUser::switchUser(Wt::Dbo::ptr 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(); }