qswiki/handlers/handlerpage.cpp

105 satır
3.2 KiB
C++
Ham Normal Görünüm Geçmiş

2018-11-03 17:12:20 +01:00
/* Copyright (c) 2018 Albert S.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <optional>
2018-11-03 17:12:20 +01:00
#include "handlerpage.h"
Response HandlerPage::handle(const Request &r)
{
std::string pagename = r.get("page");
auto pageDao = this->database->createPageDao();
if(pagename.empty())
{
2022-03-29 22:34:22 +02:00
std::string title = r.get("title");
if(title.empty())
{
return errorResponse("No page given", "No page given to request");
}
title = utils::strreplace(title, "-", " ");
auto page = pageDao->findByTitle(title);
if(!page)
{
return errorResponse("No page by such title", "No page with such title exists");
}
pagename = page->name;
2018-11-03 17:12:20 +01:00
}
if(pageMustExist() && !pageDao->exists(pagename))
{
std::string createlink = this->urlProvider->editPage(pagename);
return errorResponse(
"Page not found",
"The requested page was not found. Do you want to <a href=\"" + createlink + "\">create</a> it?", 404);
}
if(!canAccess(pagename))
{
return errorResponse("Permission denied", accessErrorMessage());
}
return this->handleRequest(*pageDao, pagename, r);
}
std::string HandlerPage::accessErrorMessage()
{
return "You don't have permission to access this page";
}
bool HandlerPage::pageMustExist()
{
return true;
}
void HandlerPage::setPageVars(TemplatePage &page, std::string pagename)
{
setGeneralVars(page);
if(!pagename.empty())
{
std::string headerlinks;
TemplatePage headerlink = this->templ->getPage("_headerlink");
auto addHeaderLink = [&headerlinks, &headerlink](std::string href, std::string value)
{
2018-11-03 17:12:20 +01:00
headerlink.setVar("href", href);
headerlink.setVar("value", value);
headerlinks += headerlink.render();
};
Permissions &perms = this->userSession->user.permissions;
if(perms.canEdit())
{
addHeaderLink(this->urlProvider->editPage(pagename), "Edit");
addHeaderLink(this->urlProvider->pageSettings(pagename), "Page settings");
}
if(perms.canDelete())
{
addHeaderLink(this->urlProvider->pageDelete(pagename), "Delete");
}
if(perms.canSeePageHistory())
{
addHeaderLink(this->urlProvider->pageHistory(pagename), "Show history");
}
page.setVar("headerlinks", headerlinks);
page.setVar("page", pagename);
2019-09-29 21:34:53 +02:00
page.setVar("title", createPageTitle(pagename));
2018-11-03 17:12:20 +01:00
}
}