calendar-webapp/source/calendarwebapp.d

116 lines
2.9 KiB
D
Raw Normal View History

module calendarwebapp;
2017-08-07 01:33:17 +02:00
import core.time : days;
import event;
import std.datetime.date : Date;
2017-08-07 01:33:17 +02:00
import std.exception : enforce;
import std.typecons : Nullable;
2017-08-07 01:33:17 +02:00
import vibe.core.path : Path;
2017-08-07 03:31:46 +02:00
import vibe.data.bson : Bson;
import vibe.db.mongo.database : MongoDatabase;
import vibe.db.mongo.mongo : connectMongoDB;
2017-08-07 01:33:17 +02:00
import vibe.http.common : HTTPStatusException;
import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.http.status : HTTPStatus;
import vibe.web.auth;
2017-08-07 03:31:46 +02:00
import vibe.web.web : errorDisplay, noRoute, redirect, render, SessionVar,
terminateSession;
struct AuthInfo
{
string userName;
}
@requiresAuth class CalendarWebapp
{
@noRoute AuthInfo authenticate(scope HTTPServerRequest req, scope HTTPServerResponse res)
{
if (!req.session || !req.session.isKeySet("auth"))
{
redirect("/login");
throw new HTTPStatusException(HTTPStatus.forbidden, "Du musst dich erst einloggen");
}
return req.session.get!AuthInfo("auth");
}
public:
@anyAuth @errorDisplay!getLogin void index()
{
auto entries = getEntriesFromFile(fileName);
render!("showevents.dt", entries);
}
@noAuth void getLogin(string _error = null)
{
2017-08-07 00:01:02 +02:00
render!("login.dt", _error);
}
@noAuth @errorDisplay!getLogin void postLogin(string username, string password)
{
2017-08-07 03:31:46 +02:00
enforce(checkUser(username, password), "Benutzername oder Passwort ungültig");
immutable AuthInfo authInfo = {username};
auth = authInfo;
redirect("/");
}
2017-08-07 00:01:02 +02:00
@anyAuth void getLogout()
2017-08-07 00:01:02 +02:00
{
terminateSession();
redirect("/");
}
@anyAuth void getCreate(ValidationErrorData _error = ValidationErrorData.init)
2017-08-07 00:01:02 +02:00
{
render!("create.dt", _error);
}
@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;
entries.writeEntriesToFile(fileName);
2017-08-07 00:01:02 +02:00
render!("showevents.dt", entries);
}
struct ValidationErrorData
{
string msg;
string field;
}
2017-08-07 03:31:46 +02:00
this()
{
database = connectMongoDB("localhost").getDatabase("CalendarWebapp");
}
private:
immutable fileName = Path("events.json");
SessionVar!(AuthInfo, "auth") auth;
2017-08-07 03:31:46 +02:00
MongoDatabase database;
bool checkUser(string username, string password)
{
auto users = database["users"];
auto result = users.findOne(["_id" : username, "password" : password]);
return result != Bson(null);
}
}