This commit is contained in:
parent
ab2fe4f492
commit
6ab3e2b22b
@ -59,12 +59,13 @@ Application::Application(const Wt::WEnvironment &env, bool embedded) : Wt::WAppl
|
||||
LOG(info) << "relative resources url: " << relativeResourcesUrl();
|
||||
LOG(info) << "resources url: " << resourcesUrl();
|
||||
LOG(info) << "internal path: " << internalPath();
|
||||
internalPathChanged().connect(this, &Application::handlePathChange);
|
||||
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
|
||||
|
||||
auto app = Amass::Singleton<WebToolkit::Server>::instance();
|
||||
m_loginPage = std::make_unique<LoginPage>(app->authService(), m_session->users(), m_session->login());
|
||||
m_loginPage->processEnvironment();
|
||||
|
||||
handlePathChange(m_externalPath.empty() ? internalPath() : m_externalPath);
|
||||
internalPathChanged().connect(this, &Application::handlePathChange);
|
||||
}
|
||||
|
||||
Application::~Application() {
|
||||
@ -75,16 +76,36 @@ void Application::authEvent() {
|
||||
const Wt::Auth::User &u = m_session->login().user();
|
||||
LOG(info) << "User " << u.id() << " (" << u.identity(Wt::Auth::Identity::LoginName) << ")"
|
||||
<< " logged in.";
|
||||
if (m_loginPage) {
|
||||
m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage));
|
||||
} else if (m_loginPageRef != nullptr && m_loginPageRef->parent() == m_root) {
|
||||
m_loginPage = m_loginPageRef->parent()->removeWidget(m_loginPageRef);
|
||||
m_loginPageRef = m_navigationBar->addLoginItem(std::move(m_loginPage));
|
||||
}
|
||||
setInternalPath("/", true);
|
||||
} else {
|
||||
m_loginPage = m_navigationBar->removeLoginItem();
|
||||
LOG(info) << "User logged out.";
|
||||
}
|
||||
}
|
||||
|
||||
void Application::handlePathChange(const std::string &path) {
|
||||
LOG(info) << "handlePathChange: " << path;
|
||||
if (path.starts_with("/wt/login") || path.starts_with("/wt/register")) {
|
||||
if (path.starts_with("/wt/login")) {
|
||||
if (m_session->login().loggedIn()) {
|
||||
std::exit(0);
|
||||
} else {
|
||||
if (m_loginPage) {
|
||||
m_root->clear();
|
||||
// m_loginPageRef = m_root->addWidget(std::move(m_loginPage));
|
||||
m_loginPageRef = m_root->addWidget(std::move(m_loginPage));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (m_loginPageRef != nullptr && m_loginPageRef->parent() == m_root) {
|
||||
m_loginPage = m_root->removeWidget(m_loginPageRef);
|
||||
}
|
||||
|
||||
if (path.starts_with("/wt/register")) {
|
||||
} else if (path.starts_with("/wt/visitor/analysis")) {
|
||||
m_root->clear();
|
||||
m_root->addNew<VisitorRecordsPage>(*m_session);
|
||||
@ -92,6 +113,7 @@ void Application::handlePathChange(const std::string &path) {
|
||||
m_root->clear();
|
||||
m_root->addNew<HomePage>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Server::Server(uint16_t port, const std::string &applicationRoot, const std::string &documentRoot) {
|
||||
|
@ -1,4 +1,7 @@
|
||||
#include "NavigationBar.h"
|
||||
#include "LoginPage.h"
|
||||
#include <Wt/WApplication.h>
|
||||
#include <Wt/WPushButton.h>
|
||||
#include <format>
|
||||
|
||||
constexpr auto Burger = R"(
|
||||
@ -8,19 +11,17 @@ constexpr auto Burger = R"(
|
||||
<span aria-hidden="true"></span>
|
||||
)";
|
||||
|
||||
constexpr auto Login = R"(
|
||||
${register-button class="bulma-button bulma-is-primary"}
|
||||
${login-button class="bulma-button bulma-is-light"}
|
||||
)";
|
||||
|
||||
constexpr auto Menu = R"(
|
||||
<div class="bulma-navbar-start">
|
||||
<a class="bulma-navbar-item">Home</a>
|
||||
</div>
|
||||
<div class="bulma-navbar-end">
|
||||
<div class="bulma-navbar-item">
|
||||
<div class="bulma-buttons">
|
||||
<a class="bulma-button bulma-is-primary">
|
||||
<strong>注册</strong>
|
||||
</a>
|
||||
<a class="bulma-button bulma-is-light">登录</a>
|
||||
</div>
|
||||
</div>
|
||||
${login-item}
|
||||
</div>
|
||||
)";
|
||||
|
||||
@ -46,7 +47,35 @@ NavigationBar::NavigationBar() : Wt::WTemplate(Template) {
|
||||
setAttributeValue("aria-label", "main navigation");
|
||||
auto navbarBurger = bindWidget("navbar-burger", std::make_unique<Wt::WTemplate>(Burger));
|
||||
navbarBurger->setHtmlTagName("a");
|
||||
auto navbarMenu = bindWidget("navbar-menu", std::make_unique<Wt::WTemplate>(Menu));
|
||||
|
||||
navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), navbarMenu->id()));
|
||||
m_menu = bindWidget("navbar-menu", std::make_unique<Wt::WTemplate>(Menu));
|
||||
createLoginButtons();
|
||||
|
||||
navbarBurger->clicked().connect(std::format(JS, navbarBurger->id(), m_menu->id()));
|
||||
}
|
||||
|
||||
NavigationBar::~NavigationBar() {
|
||||
}
|
||||
|
||||
LoginPage *NavigationBar::addLoginItem(std::unique_ptr<LoginPage> item) {
|
||||
auto ret = m_menu->bindWidget("login-item", std::move(item));
|
||||
ret->addStyleClass("bulma-navbar-item");
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::unique_ptr<LoginPage> NavigationBar::removeLoginItem() {
|
||||
auto ret = m_menu->removeWidget("login-item");
|
||||
createLoginButtons();
|
||||
return std::unique_ptr<LoginPage>(dynamic_cast<LoginPage *>(ret.release()));
|
||||
}
|
||||
|
||||
void NavigationBar::createLoginButtons() {
|
||||
auto loginItem = m_menu->bindWidget("login-item", std::make_unique<Wt::WTemplate>(Login));
|
||||
loginItem->addStyleClass("bulma-buttons");
|
||||
auto loginButton = loginItem->bindWidget("login-button", std::make_unique<Wt::WPushButton>("登录"));
|
||||
loginButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/login", true); });
|
||||
|
||||
auto registerButton = loginItem->bindWidget("register-button", std::make_unique<Wt::WPushButton>("<strong>注册</strong>"));
|
||||
registerButton->setTextFormat(Wt::TextFormat::XHTML);
|
||||
registerButton->clicked().connect([]() { Wt::WApplication::instance()->setInternalPath("/wt/register", true); });
|
||||
}
|
||||
|
@ -3,9 +3,20 @@
|
||||
|
||||
#include <Wt/WTemplate.h>
|
||||
|
||||
class NavigationBar: public Wt::WTemplate {
|
||||
class LoginPage;
|
||||
|
||||
class NavigationBar : public Wt::WTemplate {
|
||||
public:
|
||||
NavigationBar();
|
||||
~NavigationBar();
|
||||
LoginPage *addLoginItem(std::unique_ptr<LoginPage> item);
|
||||
std::unique_ptr<LoginPage> removeLoginItem();
|
||||
|
||||
protected:
|
||||
void createLoginButtons();
|
||||
|
||||
private:
|
||||
Wt::WTemplate *m_menu = nullptr;
|
||||
};
|
||||
|
||||
#endif // __NAVIGATIONBAR_H__
|
@ -218,9 +218,8 @@
|
||||
</message>
|
||||
|
||||
<message id="Wt.Auth.template.logged-in">
|
||||
<div class="Wt-auth-logged-in">
|
||||
<b>${user-name}</b> ${logout}
|
||||
</div>
|
||||
<span class="is-size-5 has-text-justified">${user-name}</span>
|
||||
${logout}
|
||||
</message>
|
||||
|
||||
<message id="Wt.Auth.template.totp">
|
||||
|
Loading…
Reference in New Issue
Block a user