From 026b3f993ef3d26c987f8568c822f2eceb0ab179 Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
Date: Fri, 27 Oct 2017 15:13:02 +0200
Subject: [PATCH] Automatically generate methods for checking if a user has
 certain privileges. Once static foreach is supported by ldc, this can be done
 even more elegant without the use of a helper function.

---
 source/calendarwebapp/authenticator.d | 25 +++++++++++++++++++------
 1 file changed, 19 insertions(+), 6 deletions(-)

diff --git a/source/calendarwebapp/authenticator.d b/source/calendarwebapp/authenticator.d
index f56643b..6607f1f 100644
--- a/source/calendarwebapp/authenticator.d
+++ b/source/calendarwebapp/authenticator.d
@@ -54,13 +54,26 @@ struct AuthInfo
     string passwordHash;
     Role role;
 
-    bool isUser() const pure @safe nothrow
+    mixin(generateAuthMethods);
+
+private:
+    static string generateAuthMethods() pure @safe
     {
-        return role == Role.User;
+        import std.conv : to;
+        import std.format : format;
+        import std.traits : EnumMembers;
+
+        string ret;
+        foreach (member; EnumMembers!Role)
+        {
+            ret ~= q{
+                bool is%s() const pure @safe nothrow
+                {
+                    return role == Role.%s;
+                }
+            }.format(member.to!string, member.to!string);
+        }
+        return ret;
     }
 
-    bool isAdmin() const pure @safe nothrow
-    {
-        return role == Role.Admin;
-    }
 }