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 "handlerpageedit.h"
|
|
|
|
#include "../database/exceptions.h"
|
|
|
|
#include "../request.h"
|
|
|
|
|
|
|
|
#include "../parser.h"
|
2022-08-20 12:30:28 +02:00
|
|
|
#include "../revisionrenderer.h"
|
2023-07-28 15:04:58 +02:00
|
|
|
|
2021-10-25 17:56:37 +02:00
|
|
|
bool HandlerPageEdit::canAccess([[maybe_unused]] std::string page)
|
2018-11-03 17:12:20 +01:00
|
|
|
{
|
2021-10-25 18:07:23 +02:00
|
|
|
return effectivePermissions(page).canEdit();
|
2018-11-03 17:12:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HandlerPageEdit::pageMustExist()
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Response HandlerPageEdit::handleRequest(PageDao &pageDao, std::string pagename, const Request &r)
|
|
|
|
{
|
|
|
|
bool pageexists = pageDao.exists(pagename);
|
|
|
|
if(!pageexists && !this->userSession->user.permissions.canCreate())
|
|
|
|
{
|
|
|
|
return errorResponse("No permission", "You don't have permission to create new pages");
|
|
|
|
}
|
|
|
|
auto revisiondao = this->database->createRevisionDao();
|
2022-08-20 12:41:17 +02:00
|
|
|
auto revision = revisiondao->getCurrentForPage(pagename);
|
2018-11-03 17:12:20 +01:00
|
|
|
std::string body;
|
|
|
|
|
2018-11-04 19:35:24 +01:00
|
|
|
unsigned int current_revision = 0;
|
2018-11-03 17:12:20 +01:00
|
|
|
if(revision)
|
|
|
|
{
|
|
|
|
body = revision->content;
|
2018-11-04 19:35:24 +01:00
|
|
|
current_revision = revision->revision;
|
2018-11-03 17:12:20 +01:00
|
|
|
}
|
2022-08-20 12:41:17 +02:00
|
|
|
std::string from = r.get("frompage");
|
|
|
|
if(!from.empty())
|
|
|
|
{
|
|
|
|
if(!effectivePermissions(from).canRead())
|
|
|
|
{
|
2023-07-28 15:04:58 +02:00
|
|
|
return this->errorResponse("Permission denied",
|
|
|
|
"No access permissions, so you can't use this page as a template");
|
2022-08-20 12:41:17 +02:00
|
|
|
}
|
|
|
|
body = revisiondao->getCurrentForPage(from)->content;
|
|
|
|
}
|
2018-11-03 17:12:20 +01:00
|
|
|
if(r.getRequestMethod() == "POST")
|
|
|
|
{
|
|
|
|
if(r.post("do") == "submit")
|
|
|
|
{
|
|
|
|
std::string newContent = r.post("content");
|
|
|
|
std::string newComment = r.post("comment");
|
|
|
|
|
|
|
|
// TODO: must check, whether categories differ, and perhaps don't allow every user
|
|
|
|
// to set categories
|
|
|
|
Parser parser;
|
|
|
|
std::vector<std::string> cats = parser.extractCategories(newContent);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
this->database->beginTransaction();
|
2024-03-16 22:03:22 +01:00
|
|
|
|
2018-11-04 19:35:24 +01:00
|
|
|
std::string visiblecmd = parser.extractCommand("visible", newContent);
|
2024-03-16 22:03:22 +01:00
|
|
|
std::string listedcmd = parser.extractCommand("listed", newContent);
|
|
|
|
/* Backwarts compatibility */
|
|
|
|
if(listedcmd.empty())
|
|
|
|
{
|
|
|
|
listedcmd = visiblecmd;
|
|
|
|
}
|
|
|
|
std::string feedlistedcmd = parser.extractCommand("feedlisted", newContent);
|
|
|
|
|
2018-11-04 19:35:24 +01:00
|
|
|
std::string rename = parser.extractCommand("rename", newContent);
|
2022-03-27 09:23:35 +02:00
|
|
|
std::string customtitle = parser.extractCommand("pagetitle", newContent);
|
2023-11-23 18:25:57 +01:00
|
|
|
std::string parentpage = parser.extractCommand("parentpage", newContent);
|
2023-07-28 15:04:58 +02:00
|
|
|
std::vector<std::string> perms = parser.extractCommands("permissions", newContent);
|
2023-11-23 18:25:57 +01:00
|
|
|
|
|
|
|
if(parentpage != "" && !pageDao.find(parentpage))
|
|
|
|
{
|
|
|
|
return this->errorResponse("Invalid parent",
|
|
|
|
"Specified parent page " + parentpage + " does not exist");
|
|
|
|
}
|
|
|
|
|
2018-11-04 19:35:24 +01:00
|
|
|
Page page;
|
|
|
|
std::optional<Page> currentPage = pageDao.find(pagename);
|
|
|
|
if(currentPage)
|
|
|
|
{
|
|
|
|
page = *currentPage;
|
|
|
|
}
|
|
|
|
if(rename != "")
|
2018-11-03 17:12:20 +01:00
|
|
|
{
|
2018-11-04 19:35:24 +01:00
|
|
|
if(newComment == "")
|
|
|
|
{
|
|
|
|
newComment = "(renamed) from " + pagename + " to " + rename;
|
|
|
|
}
|
|
|
|
pagename = rename;
|
2018-11-03 17:12:20 +01:00
|
|
|
}
|
2023-07-28 15:04:58 +02:00
|
|
|
|
2023-08-11 09:22:04 +02:00
|
|
|
std::vector<std::pair<std::string, Permissions>> collectedPermissions;
|
|
|
|
|
|
|
|
auto permissionDao = this->database->createPermissionsDao();
|
2023-07-28 15:04:58 +02:00
|
|
|
for(const std::string &perm : perms)
|
|
|
|
{
|
|
|
|
auto splitted = utils::split(perm, '|');
|
|
|
|
if(splitted.size() != 2)
|
|
|
|
{
|
|
|
|
return this->errorResponse("Invalid command", "permissions command is misformated");
|
|
|
|
}
|
|
|
|
auto currentPermission = permissionDao->find(pagename, splitted[0]);
|
|
|
|
|
|
|
|
Permissions newPermissions = Permissions{splitted[1]};
|
|
|
|
if(!currentPermission || newPermissions != currentPermission.value())
|
|
|
|
{
|
2023-08-11 09:22:04 +02:00
|
|
|
if(!this->userSession->user.permissions.canSetPagePerms())
|
2023-07-28 15:04:58 +02:00
|
|
|
{
|
|
|
|
this->database->rollbackTransaction();
|
2023-08-11 09:22:04 +02:00
|
|
|
return errorResponse("Permission denied",
|
|
|
|
"You don't have permission to change permissions. Don't touch the "
|
|
|
|
"permission commands");
|
2023-07-28 15:04:58 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-11 09:22:04 +02:00
|
|
|
collectedPermissions.emplace_back(splitted[0], newPermissions);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(this->userSession->user.permissions.canSetPagePerms())
|
|
|
|
{
|
|
|
|
permissionDao->clearForPage(pagename);
|
|
|
|
for(auto &perms : collectedPermissions)
|
|
|
|
{
|
|
|
|
permissionDao->save(pagename, perms.first, perms.second);
|
|
|
|
}
|
2023-07-28 15:04:58 +02:00
|
|
|
}
|
|
|
|
|
2018-11-04 19:35:24 +01:00
|
|
|
page.current_revision = current_revision;
|
2024-03-16 22:03:22 +01:00
|
|
|
page.listed = !(listedcmd == "0");
|
|
|
|
page.feedlisted = !(feedlistedcmd == "0");
|
2018-11-04 19:35:24 +01:00
|
|
|
page.name = pagename;
|
2022-03-27 09:23:35 +02:00
|
|
|
page.title = customtitle;
|
2023-11-23 18:25:57 +01:00
|
|
|
page.parentpage = parentpage;
|
2022-03-27 09:23:35 +02:00
|
|
|
if(page.title.empty())
|
|
|
|
{
|
|
|
|
page.title = page.name;
|
|
|
|
}
|
2018-11-04 19:35:24 +01:00
|
|
|
pageDao.save(page);
|
|
|
|
|
|
|
|
Revision newRevision;
|
|
|
|
newRevision.author = this->userSession->user.login;
|
|
|
|
newRevision.comment = newComment;
|
|
|
|
newRevision.page = pagename;
|
|
|
|
newRevision.content = newContent;
|
|
|
|
|
2018-11-03 17:12:20 +01:00
|
|
|
revisiondao->save(newRevision);
|
|
|
|
pageDao.setCategories(pagename, cats);
|
|
|
|
this->database->commitTransaction();
|
|
|
|
this->cache->removePrefix("page:"); // TODO: overkill?
|
2022-03-27 21:22:00 +02:00
|
|
|
this->cache->removePrefix("feed:");
|
2018-11-03 17:12:20 +01:00
|
|
|
}
|
|
|
|
catch(const DatabaseException &e)
|
|
|
|
{
|
2023-07-26 20:21:44 +02:00
|
|
|
this->database->rollbackTransaction();
|
2018-11-03 17:12:20 +01:00
|
|
|
Logger::debug() << "Error saving revision: " << e.what();
|
|
|
|
return errorResponse("Database error", "A database error occured while trying to save this revision");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::redirectTemporarily(urlProvider->page(pagename));
|
|
|
|
}
|
|
|
|
if(r.post("do") == "preview")
|
|
|
|
{
|
|
|
|
std::string newContent = r.post("content");
|
|
|
|
Parser parser;
|
2022-08-20 12:30:28 +02:00
|
|
|
std::string title = parser.extractCommand("pagetitle", newContent);
|
2018-11-03 17:12:20 +01:00
|
|
|
TemplatePage templatePage = this->templ->getPage("page_creation_preview");
|
|
|
|
templatePage.setVar("actionurl", urlProvider->editPage(pagename));
|
2022-08-20 12:30:28 +02:00
|
|
|
|
2023-07-28 15:04:58 +02:00
|
|
|
RevisionRenderer revisionRenderer{*this->templ, *this->database, *this->urlProvider, *this->userSession};
|
2022-08-20 12:30:28 +02:00
|
|
|
|
|
|
|
templatePage.setVar("preview_content", revisionRenderer.renderContent(newContent));
|
2018-11-03 17:12:20 +01:00
|
|
|
templatePage.setVar("content", newContent);
|
|
|
|
setPageVars(templatePage, pagename);
|
2022-08-20 12:30:28 +02:00
|
|
|
templatePage.setVar("title", createPageTitle("Preview: " + title));
|
2021-03-08 11:37:45 +01:00
|
|
|
templatePage.setVar("comment", r.post("comment"));
|
2018-11-03 17:12:20 +01:00
|
|
|
Response response;
|
|
|
|
response.setBody(templatePage.render());
|
|
|
|
return response;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 00:11:58 +02:00
|
|
|
TemplatePage templatePage = this->templ->getPage("page_creation");
|
2018-11-03 17:12:20 +01:00
|
|
|
templatePage.setVar("actionurl", urlProvider->editPage(pagename));
|
|
|
|
templatePage.setVar("content", body);
|
|
|
|
setPageVars(templatePage, pagename);
|
2019-09-29 21:34:53 +02:00
|
|
|
templatePage.setVar("title", createPageTitle("Edit: " + pagename));
|
2018-11-03 17:12:20 +01:00
|
|
|
Response response;
|
|
|
|
response.setBody(templatePage.render());
|
|
|
|
return response;
|
|
|
|
}
|