I have a permissions attribute which takes a list of enum values for roles:
[CheckRoles(SystemRole.ID.Admin, SystemRole.ID.User)]
public class MyController : Controller
{
....
}
However, in other parts of the code I would like to check if the user is in any of these roles, so I can go to the correct page. So the code looks something like:
if (roles.IsInAnyRoles(user, SystemRole.ID.Admin, SystemRole.ID.User)
{
... Do something in MyController ...
}
You can see the repetition here. I really want to have the two roles in a variable or initialiser list or something, that I can pass to both the Attribute and the method. Creating a const array doesn't work. Is there a way of doing this? Can I store the array initialiser somehow?
Any help would be greatly appreciated!
UPDATE
I have made the roles enum int a Flags enum (and given the items appropriate values). This means I can or the values together to give a single, constant value. I can now use that constant value in the Attribute, and in methods. Thanks to Danny Chen below.