123 line
3.9 KiB
C++
123 line
3.9 KiB
C++
|
/* 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"
|
||
|
bool HandlerPageEdit::canAccess(std::string page)
|
||
|
{
|
||
|
return this->userSession->user.permissions.canEdit();
|
||
|
}
|
||
|
|
||
|
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();
|
||
|
auto revision = this->database->createRevisionDao()->getCurrentForPage(pagename);
|
||
|
std::string body;
|
||
|
|
||
|
if(revision)
|
||
|
{
|
||
|
body = revision->content;
|
||
|
}
|
||
|
if(r.getRequestMethod() == "POST")
|
||
|
{
|
||
|
if(r.post("do") == "submit")
|
||
|
{
|
||
|
std::string newContent = r.post("content");
|
||
|
std::string newComment = r.post("comment");
|
||
|
|
||
|
Revision newRevision;
|
||
|
newRevision.author = this->userSession->user.login;
|
||
|
newRevision.comment = newComment;
|
||
|
newRevision.page = pagename;
|
||
|
newRevision.content = newContent;
|
||
|
|
||
|
|
||
|
//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();
|
||
|
if(!pageexists)
|
||
|
{
|
||
|
Page newPage;
|
||
|
newPage.current_revision = 0;
|
||
|
newPage.listed = true;
|
||
|
newPage.name = pagename;
|
||
|
pageDao.save(newPage);
|
||
|
|
||
|
}
|
||
|
revisiondao->save(newRevision);
|
||
|
pageDao.setCategories(pagename, cats);
|
||
|
this->database->commitTransaction();
|
||
|
this->cache->removePrefix("page:"); //TODO: overkill?
|
||
|
}
|
||
|
catch(const DatabaseException &e)
|
||
|
{
|
||
|
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;
|
||
|
TemplatePage templatePage = this->templ->getPage("page_creation_preview");
|
||
|
templatePage.setVar("actionurl", urlProvider->editPage(pagename));
|
||
|
templatePage.setVar("preview_content", parser.parse(pageDao, *this->urlProvider, newContent) );
|
||
|
templatePage.setVar("content", newContent);
|
||
|
setPageVars(templatePage, pagename);
|
||
|
|
||
|
Response response;
|
||
|
response.setBody(templatePage.render());
|
||
|
return response;
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
TemplatePage &templatePage = this->templ->getPage("page_creation");
|
||
|
templatePage.setVar("actionurl", urlProvider->editPage(pagename));
|
||
|
templatePage.setVar("content", body);
|
||
|
setPageVars(templatePage, pagename);
|
||
|
Response response;
|
||
|
response.setBody(templatePage.render());
|
||
|
return response;
|
||
|
|
||
|
}
|