calendar-webapp/source/calendarwebapp/calendarwebapp.d

135 lines
3.8 KiB
D
Raw Normal View History

2017-09-17 17:52:41 +02:00
module calendarwebapp.calendarwebapp;
import calendarwebapp.authenticator;
2017-09-17 17:52:41 +02:00
import calendarwebapp.event;
import calendarwebapp.jsonexport;
import calendarwebapp.passhash : PasswordHasher;
2017-09-17 17:52:41 +02:00
import core.time : days;
import poodinis;
import std.datetime : Date;
import std.exception : enforce;
import std.typecons : Nullable;
import vibe.data.bson : BsonObjectID;
import vibe.http.common : HTTPStatusException;
import vibe.http.server : HTTPServerRequest, HTTPServerResponse;
import vibe.http.status : HTTPStatus;
import vibe.web.auth;
import vibe.web.web : errorDisplay, noRoute, redirect, render, SessionVar,
terminateSession;
@requiresAuth class CalendarWebapp
{
@noRoute AuthInfo authenticate(scope HTTPServerRequest, scope HTTPServerResponse) @safe
2017-09-17 17:52:41 +02:00
{
if (authInfo.value.isNone)
2017-09-17 17:52:41 +02:00
redirect("/login");
return authInfo.value;
2017-09-17 17:52:41 +02:00
}
public:
@auth(Role.user | Role.admin) void index()
2017-09-17 17:52:41 +02:00
{
auto events = eventStore.getAllEvents();
auto authInfo = this.authInfo.value;
render!("showevents.dt", events, authInfo);
2017-09-17 17:52:41 +02:00
}
@noAuth void getLogin(string _error = null)
{
auto authInfo = this.authInfo.value;
render!("login.dt", _error, authInfo);
2017-09-17 17:52:41 +02:00
}
@noAuth @errorDisplay!getLogin void postLogin(string username, string password) @safe
{
auto authInfo = authenticator.checkUser(username, password);
enforce(!authInfo.isNull, "Benutzername oder Passwort ungültig");
this.authInfo = authInfo.get;
2017-09-17 17:52:41 +02:00
redirect("/");
}
@auth(Role.user | Role.admin) void getLogout() @safe
2017-09-17 17:52:41 +02:00
{
terminateSession();
redirect("/");
}
@auth(Role.user | Role.admin) void getCreateevent(
ValidationErrorData _error = ValidationErrorData.init)
2017-09-17 17:52:41 +02:00
{
auto authInfo = this.authInfo.value;
render!("createevent.dt", _error, authInfo);
2017-09-17 17:52:41 +02:00
}
@auth(Role.user | Role.admin) @errorDisplay!getCreateevent void postCreateevent(Date begin,
2017-10-30 02:31:48 +01:00
Nullable!Date end, string description, string name, EventType type, bool shout)
2017-09-17 17:52:41 +02:00
{
import std.array : replace, split;
if (!end.isNull)
enforce(end - begin >= 1.days,
"Mehrtägige Ereignisse müssen mindestens einen Tag dauern");
2017-10-30 02:31:48 +01:00
auto event = Event("", begin, end, name, description.replace("\r", ""), type, shout);
2017-09-17 17:52:41 +02:00
eventStore.addEvent(event);
exporter.exportJSON;
2017-09-17 17:52:41 +02:00
redirect("/");
}
2017-11-09 02:24:15 +01:00
@auth(Role.user | Role.admin) void postRemoveevent(string id)
2017-09-17 17:52:41 +02:00
{
eventStore.removeEvent(id);
exporter.exportJSON;
2017-09-17 17:52:41 +02:00
redirect("/");
}
@auth(Role.admin) void getUsers()
{
auto users = authenticator.getAllUsers;
auto authInfo = this.authInfo.value;
render!("showusers.dt", users, authInfo);
}
2017-11-09 02:24:15 +01:00
@auth(Role.admin) void postRemoveuser(string id)
{
authenticator.removeUser(id);
redirect("/users");
}
@auth(Role.admin) void getCreateuser(ValidationErrorData _error = ValidationErrorData.init)
{
auto authInfo = this.authInfo.value;
render!("createuser.dt", _error, authInfo);
}
@auth(Role.admin) @errorDisplay!getCreateuser void postCreateuser(string username,
2017-11-09 02:24:15 +01:00
string password, Privilege role)
{
2017-11-23 21:40:48 +01:00
import vibe.core.concurrency : async;
2017-11-09 02:24:15 +01:00
authenticator.addUser(AuthInfo("", username,
2017-11-23 21:40:48 +01:00
async(() => passwordHasher.generateHash(password)).getResult, role));
redirect("/users");
}
2017-09-17 17:52:41 +02:00
private:
struct ValidationErrorData
{
string msg;
string field;
}
2017-11-23 21:40:48 +01:00
SessionVar!(AuthInfo, "authInfo") authInfo = AuthInfo("", string.init,
string.init, Privilege.None);
2017-09-17 17:52:41 +02:00
@Autowire EventStore eventStore;
@Autowire Authenticator authenticator;
@Autowire PasswordHasher passwordHasher;
@Autowire JSONExporter exporter;
2017-09-17 17:52:41 +02:00
}