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.

This commit is contained in:
Johannes Loher 2017-10-27 15:13:02 +02:00
parent 464c224d72
commit 026b3f993e

View file

@ -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;
}
}