There are currently 3 roles - Admin
, Manager
and Writer
.
There are some methods inside HomeController
and most of them are common for both Admin
and Manager
, few for only Admin
, and few for Writer
. There are around 30 methods. And 25 will be common for Admin and Manager, 1-2 for only Admnin. And 1-2 for all of 3 roles.
- Only 1 method named
ForWriterOnly()
should be available for everyone - Writer should not have access to any of other methods, but Admin and Manager should have access
- There are some methods which are only for Admin, and not for Manager and Writer
What is the best way to achieve the 3 cases mentioned above ? How can I exclude Writer for most of the other methods except ForWriterOnly()
? And how can I exclude Admin
for accessing some other methods ?
I know I could write [Authorize(Roles = "Admin, Manager")]
for those 25 common methods for Admin and Manager but I am looking for a simpler solution.
Please make modifications in my code so that it will be easier to understand.
[Authorize(Roles = "Admin, Manager, Writer")]
public class HomeController : Controller
{
public async Task<IActionResult> ForEveryone()
{
return View();
}
public async Task<IActionResult> ForAdminAndManager()
{
return View();
}
public async Task<IActionResult> ForWriterOnly()
{
return View();
}
public async Task<IActionResult> ForAdminOnly()
{
return View();
}
public async Task<IActionResult> ForAdminAndManager1()
{
return View();
}
public async Task<IActionResult> ForAdminAndManager2()
{
return View();
}
...........
// There are around 30 other methods which are common for both Admin and Manager
}