2017-08-06 22:46:42 +02:00
|
|
|
module calendarwebapp;
|
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
import authenticator : Authenticator, AuthInfo;
|
|
|
|
|
2017-08-07 01:33:17 +02:00
|
|
|
import core.time : days;
|
|
|
|
|
2017-08-06 22:46:42 +02:00
|
|
|
import event;
|
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
import poodinis;
|
|
|
|
|
2017-08-07 01:09:45 +02:00
|
|
|
import std.datetime.date : Date;
|
2017-08-07 01:33:17 +02:00
|
|
|
import std.exception : enforce;
|
2017-08-06 22:46:42 +02:00
|
|
|
import std.typecons : Nullable;
|
|
|
|
|
2017-08-07 01:33:17 +02:00
|
|
|
import vibe.http.common : HTTPStatusException;
|
|
|
|
import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
|
|
|
|
import vibe.http.status : HTTPStatus;
|
2017-08-07 00:54:47 +02:00
|
|
|
import vibe.web.auth;
|
2017-08-07 03:31:46 +02:00
|
|
|
import vibe.web.web : errorDisplay, noRoute, redirect, render, SessionVar,
|
|
|
|
terminateSession;
|
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-08 01:05:11 +02:00
|
|
|
@anyAuth void index()
|
2017-08-06 22:46:42 +02:00
|
|
|
{
|
2017-08-08 01:05:11 +02:00
|
|
|
auto events = eventStore.getAllEvents();
|
|
|
|
render!("showevents.dt", events);
|
2017-08-06 22:46:42 +02:00
|
|
|
}
|
|
|
|
|
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-08 01:05:11 +02:00
|
|
|
enforce(authenticator.checkUser(username, password), "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 01:09:45 +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
|
|
|
{
|
2017-08-08 01:05:11 +02:00
|
|
|
import std.array : replace, split;
|
|
|
|
import vibe.data.bson : BsonObjectID;
|
2017-08-07 00:01:02 +02:00
|
|
|
|
|
|
|
if (!end.isNull)
|
|
|
|
enforce(end - begin >= 1.days,
|
|
|
|
"Mehrtägige Ereignisse müssen mindestens einen Tag dauern");
|
2017-08-08 01:05:11 +02:00
|
|
|
auto event = Event(BsonObjectID.generate, begin, end, name,
|
|
|
|
description.replace("\r", "").split('\n'), type, shout);
|
2017-08-07 00:01:02 +02:00
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
eventStore.addEvent(event);
|
2017-08-07 00:01:02 +02:00
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
redirect("/");
|
2017-08-07 00:01:02 +02:00
|
|
|
}
|
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
private:
|
2017-08-07 00:01:02 +02:00
|
|
|
struct ValidationErrorData
|
|
|
|
{
|
|
|
|
string msg;
|
|
|
|
string field;
|
|
|
|
}
|
|
|
|
|
2017-08-07 00:54:47 +02:00
|
|
|
SessionVar!(AuthInfo, "auth") auth;
|
2017-08-07 03:31:46 +02:00
|
|
|
|
2017-08-08 01:05:11 +02:00
|
|
|
@Autowire EventStore eventStore;
|
|
|
|
@Autowire Authenticator authenticator;
|
2017-08-06 22:46:42 +02:00
|
|
|
}
|