Refactoring long if statement that checks for user roles

Hi, I'm trying to find a cleaner way of refactoring this if code block that checks for each user role. The if statement works but looks really messy, and I was wondering if it's possible to do something like how EF Core would do, just check if it contains something using .Contains().
if (roleName != "Admin" && (User.IsInRole("Admin.Global") || User.IsInRole("Department.A") || User.IsInRole("Department.B") || User.IsInRole("Department.C") || User.IsInRole("Department.D")))
if (roleName != "Admin" && (User.IsInRole("Admin.Global") || User.IsInRole("Department.A") || User.IsInRole("Department.B") || User.IsInRole("Department.C") || User.IsInRole("Department.D")))
12 Replies
Angius
Angius2y ago
I'd probably create an extension method on User Or create some authorization service
Anton
Anton2y ago
new[]{"Role1",...}.Any(User.IsInRole) you can move the roles into a static readonly field to make sure it's not allocated multiple times
CoreVisional
CoreVisionalOP2y ago
Hmm, extension method?
Angius
Angius2y ago
Yeah In case you didn't know what extension methods are...
MODiX
MODiX2y ago
Angius#1586
sharplab.io (click here)
Console.WriteLine(78.DoubleIt());
static class IntExtensions {
public static int DoubleIt(this int val) {
return val * 2;
}
}
Console.WriteLine(78.DoubleIt());
static class IntExtensions {
public static int DoubleIt(this int val) {
return val * 2;
}
}
React with ❌ to remove this embed.
CoreVisional
CoreVisionalOP2y ago
I'm still confused as in how does this relate to checking for the roles.
Angius
Angius2y ago
You asked what extension methods are... well, this is what they are You can declare one on your User that has all those checks inside So that you can just do if (User.CustomChecks())
ero
ero2y ago
Or just a computed property on the user
Angius
Angius2y ago
User in this instance is of, I believe, IClaimsPrincipal, type It's not an IdentityUser Yeah, the naming is a bit confusing, but it is what it is
CoreVisional
CoreVisionalOP2y ago
Ahh okay, I went and try to create the custom extension check in one razor view.... so i was stuck trying to figure out how to do it Wait, so it's something like public static bool (this IPrincipal user) and not public static bool (this User user)?
Angius
Angius2y ago
ye
CoreVisional
CoreVisionalOP2y ago
Does IsInRole checks for the entire role name or part of it? Like, if I just specify "Department", does it verify all "Department.A", "Department.B", etc? how to check for true : false here btw? I'm looking to return false if the second variable is true for example.
public static bool IsAdmin(this ClaimsPrincipal principal)
{
var roles = new[] { "Admin", "Department" };

var userRoles = principal.Claims.Where(x => roles.Contains(ClaimTypes.Role));
var excludeRole = principal.Claims.Where(x => ClaimTypes.Role.Contains("Candidate"));
}
public static bool IsAdmin(this ClaimsPrincipal principal)
{
var roles = new[] { "Admin", "Department" };

var userRoles = principal.Claims.Where(x => roles.Contains(ClaimTypes.Role));
var excludeRole = principal.Claims.Where(x => ClaimTypes.Role.Contains("Candidate"));
}
Want results from more Discord servers?
Add your server