I have been struggling with the arcitecture for my permission handling on an application I'm building. Basically it consists of permissions for each different view on the web page.
For example, permissions for the Service part of the website would look like the following:
public bool ServiceView { get; set; }
public bool ServiceEdit { get; set; }
public bool ServiceCreate { get; set; }
public bool ServiceDelete { get; set; }
Where ServiceView let's you view services, ServiceEdit let's you edit them, and so on.
To check if a user has permission my first thought was to build a nested switch-case, like so:
public bool IsAuthorized(string controller, string action)
{
switch (controller)
{
case "Customer":
switch (action)
{
case "Index":
case "Details": return CustomerView;
case "Edit": return CustomerEdit;
case "Create": return CustomerCreate;
case "Delete": return CustomerDelete;
}
case "Service":
switch (action)
{
case "Index":
case "Details": return ServiceView;
case "Edit": return ServiceEdit;
case "Create": return ServiceCreate;
case "Delete": return ServiceDelete;
}
default: return false;
}
}
This however seems really ugly to me, so I'm wondering if anyone has any tips for a simpler and probably more beautiful way of doing this?
I have been googling around a bit to get inspiration, and came across a reflection-based way where you try to match the string to the property. Might that be a better way?
Here are some related links, that doesn't really give me the answer I'm looking for:
1. This related thread, which doesn't really get into details.
2. Another related thread, that neither goes into details, and where the accepted answer is role-based.
3. The answer to this thread is another way it could be handled.
Any help is highly appreciated.
Cheers!