2017-08-06 22:46:42 +02:00
|
|
|
module calendarwebapp;
|
|
|
|
|
|
|
|
import event;
|
|
|
|
|
|
|
|
import std.datetime.date;
|
|
|
|
import std.typecons : Nullable;
|
|
|
|
|
|
|
|
import vibe.vibe;
|
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
import vibe.web.auth;
|
2017-08-06 22:46:42 +02:00
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
struct AuthInfo
|
|
|
|
{
|
|
|
|
string userName;
|
|
|
|
}
|
2017-08-06 22:46:42 +02:00
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
@requiresAuth class CalendarWebapp
|
|
|
|
{
|
|
|
|
@noRoute AuthInfo authenticate(scope HTTPServerRequest req, scope HTTPServerResponse res)
|
2017-08-06 22:46:42 +02:00
|
|
|
{
|
2017-08-07 00:54:47 +02:00
|
|
|
if (!req.session || !req.session.isKeySet("auth"))
|
2017-08-06 22:46:42 +02:00
|
|
|
{
|
|
|
|
redirect("/login");
|
2017-08-07 00:54:47 +02:00
|
|
|
throw new HTTPStatusException(HTTPStatus.forbidden, "Du musst dich erst einloggen");
|
|
|
|
}
|
|
|
|
return req.session.get!AuthInfo("auth");
|
2017-08-06 22:46:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2017-08-07 00:54:47 +02:00
|
|
|
@anyAuth @errorDisplay!getLogin void index()
|
2017-08-06 22:46:42 +02:00
|
|
|
{
|
|
|
|
auto entries = getEntriesFromFile(fileName);
|
|
|
|
render!("showevents.dt", entries);
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
@noAuth void getLogin(string _error = null)
|
2017-08-06 22:46:42 +02:00
|
|
|
{
|
2017-08-07 00:01:02 +02:00
|
|
|
render!("login.dt", _error);
|
2017-08-06 22:46:42 +02:00
|
|
|
}
|
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
@noAuth @errorDisplay!getLogin void postLogin(string username, string password)
|
2017-08-06 22:46:42 +02:00
|
|
|
{
|
2017-08-07 00:01:02 +02:00
|
|
|
enforce(username == "foo" && password == "bar", "Benutzername oder Passwort ungültig");
|
2017-08-07 00:54:47 +02:00
|
|
|
immutable AuthInfo authInfo = {username};
|
|
|
|
auth = authInfo;
|
2017-08-06 22:46:42 +02:00
|
|
|
redirect("/");
|
|
|
|
}
|
2017-08-07 00:01:02 +02:00
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
@anyAuth void getLogout()
|
2017-08-07 00:01:02 +02:00
|
|
|
{
|
|
|
|
terminateSession();
|
|
|
|
redirect("/");
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
@anyAuth void getCreate(ValidationErrorData _error = ValidationErrorData.init)
|
2017-08-07 00:01:02 +02:00
|
|
|
{
|
|
|
|
render!("create.dt", _error);
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
@anyAuth @errorDisplay!getCreate void postCreate(Date begin, Nullable!Date end,
|
|
|
|
string description, string name, EventType type, bool shout)
|
2017-08-07 00:01:02 +02:00
|
|
|
{
|
|
|
|
import std.array : split, replace;
|
|
|
|
|
|
|
|
if (!end.isNull)
|
|
|
|
enforce(end - begin >= 1.days,
|
|
|
|
"Mehrtägige Ereignisse müssen mindestens einen Tag dauern");
|
|
|
|
|
|
|
|
auto entry = Entry(begin, end, Event("", name,
|
|
|
|
description.replace("\r", "").split('\n'), type, shout));
|
|
|
|
|
|
|
|
auto entries = getEntriesFromFile(fileName) ~ entry;
|
|
|
|
fileName.writeFileUTF8(entries.serializeToPrettyJson);
|
|
|
|
render!("showevents.dt", entries);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ValidationErrorData
|
|
|
|
{
|
|
|
|
string msg;
|
|
|
|
string field;
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
private:
|
|
|
|
|
|
|
|
immutable fileName = Path("events.json");
|
|
|
|
|
|
|
|
SessionVar!(AuthInfo, "auth") auth;
|
|
|
|
|
|
|
|
Entry[] getEntriesFromFile(in Path fileName)
|
|
|
|
{
|
|
|
|
Entry[] entries;
|
|
|
|
if (fileName.existsFile)
|
|
|
|
{
|
|
|
|
deserializeJson(entries, fileName.readFileUTF8.parseJsonString);
|
|
|
|
}
|
|
|
|
return entries;
|
|
|
|
}
|
|
|
|
|
2017-08-06 22:46:42 +02:00
|
|
|
}
|