I would suggest a complete different approach, when working with fixed user roles.
Using an Enumeration you can achieve same and much more:
public abstract class UserRoleType : Enumeration<UserRoleType>
{
protected UserRoleType(int value, string displayName)
: base(value, displayName)
{}
public static readonly UserRoleType Unknown = new UnknownRoleType();
public static readonly UserRoleType Administrator = new AdministratorRoleType();
public static readonly UserRoleType System = new SystemRoleType();
public static readonly UserRoleType Moderator = new ModeratorRoleType();
public virtual bool CanCreateUser => false;
public virtual bool CanBlockUser => false;
public virtual bool CanResetUserPassword => false;
}
public sealed class UnknownRoleType : UserRoleType
{
public UnknownRoleType()
: base(0, "Unknown")
{ }
}
public sealed class AdministratorRoleType : UserRoleType
{
public AdministratorRoleType()
: base(10, "Administrator")
{}
public override bool CanCreateUser => true;
public override bool CanBlockUser => true;
public override bool CanResetUserPassword => true;
}
public sealed class SystemRoleType : UserRoleType
{
public SystemRoleType()
: base(20, "System")
{ }
public override bool CanBlockUser => true;
public override bool CanResetUserPassword => true;
}
public sealed class ModeratorRoleType : UserRoleType
{
public ModeratorRoleType()
: base(40, "Moderator")
{ }
public override bool CanBlockUser => true;
}
By setting abstract/virtual properties on the abstract UserRoleType, you system only have operate on the abstract class.
When your user context is being initialized (on login), you simply find the user role by
var roleTypeValueFromDatabase = 10;
var roleType = UserRoleType.FromValueOrDefault(roleTypeValueFromDatabase, UserRoleType.Unknown);
if (roleType.CanCreateUser)
{
// create user..
}
// Find roles with specific rights
var rolesThatCanResetPassword = UserRoleType.GetAll().Where(urt => urt.CanResetUserPassword);
About the Enumeration class, there are several implementation of them on github/nuget.
Mine is for .Net core v2 - https://github.com/microknights/Collections
with Nuget: Install-Package MicroKnights.Collections
Headspring - https://github.com/HeadspringLabs/Enumeration
source files only.
Guid
value is not valid for theIsDefined()
method. Why did you think that would work? What are you actually trying to do? Please fix your question so it includes a good minimal reproducible example that shows clearly the context and a good explanation of what specifically you can't figure out. – Courage2ED3164-BB48-499B-86C4-A2B1114BF1
not a validGUID
, you are missing3
chars,1
at the first 8 and2
at the last portion. – BataanGuid
isn't one of those. What you did was tagging integral enum members with guids but that doesn't make the guids part of the enum. learn.microsoft.com/en-us/dotnet/csharp/language-reference/… – LudditeGuid
-> enum members using reflection and then use the map to look up enum values. – LudditeEnum
withFlag
attribute. You then do bit operations to determine if user has permission to do functions. See blog.typps.com/2007/10/one-bit-masks-for-access-control.html for more info, same concept for roles. – Bataan