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:
parent
464c224d72
commit
026b3f993e
1 changed files with 19 additions and 6 deletions
|
@ -54,13 +54,26 @@ struct AuthInfo
|
||||||
string passwordHash;
|
string passwordHash;
|
||||||
Role role;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue