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 "handler.h"
|
|
|
|
|
|
|
|
void Handler::setGeneralVars(TemplatePage &page)
|
|
|
|
{
|
|
|
|
if(userSession->loggedIn)
|
|
|
|
{
|
|
|
|
page.setVar("loginstatus", "Logged in as " + userSession->user.login);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
page.setVar("loginstatus", "not logged in");
|
|
|
|
}
|
|
|
|
page.setVar("csrf_token", utils::toString(this->userSession->csrf_token));
|
|
|
|
}
|
|
|
|
Response Handler::errorResponse(std::string errortitle, std::string errormessage, int status)
|
|
|
|
{
|
|
|
|
TemplatePage &error = this->templ->getPage("error");
|
2019-09-29 21:34:53 +02:00
|
|
|
error.setVar("title", createPageTitle(errortitle));
|
2018-11-03 17:12:20 +01:00
|
|
|
error.setVar("errortitle", errortitle);
|
|
|
|
error.setVar("errormessage", errormessage);
|
|
|
|
// TODO: log?
|
|
|
|
setGeneralVars(error);
|
|
|
|
return {status, error.render()};
|
|
|
|
}
|
|
|
|
|
2019-09-29 21:34:53 +02:00
|
|
|
std::string Handler::createPageTitle(std::string append)
|
|
|
|
{
|
|
|
|
return this->handlersConfig->wikiname + " - " + append; // TODO: we might wanna make the format configurable
|
|
|
|
}
|
|
|
|
|
2018-11-03 17:12:20 +01:00
|
|
|
QueryOption Handler::queryOption(const Request &r) const
|
|
|
|
{
|
|
|
|
QueryOption result;
|
|
|
|
result.includeInvisible = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result.limit = utils::toUInt(r.get("limit"));
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
result.limit = 0;
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result.offset = utils::toUInt(r.get("offset"));
|
|
|
|
}
|
|
|
|
catch(std::exception &e)
|
|
|
|
{
|
|
|
|
result.offset = 0;
|
|
|
|
}
|
|
|
|
std::string order = r.get("sort");
|
|
|
|
if(order == "0")
|
|
|
|
{
|
|
|
|
result.order = ASCENDING;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result.order = DESCENDING;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2019-05-03 15:59:29 +02:00
|
|
|
|
|
|
|
Response Handler::handle(const Request &r)
|
|
|
|
{
|
|
|
|
if(!canAccess(this->userSession->user.permissions))
|
|
|
|
{
|
|
|
|
return errorResponse("Permission denied", accessErrorMessage());
|
|
|
|
}
|
|
|
|
return handleRequest(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
Permissions Handler::effectivePermissions(std::string page)
|
|
|
|
{
|
|
|
|
return this->database->createPermissionsDao()
|
|
|
|
->find(page, this->userSession->user.login)
|
|
|
|
.value_or(this->userSession->user.permissions);
|
|
|
|
}
|