Guidance on Role-based views

Is there any general rule of thumb / pattern people have/should use to implement Role-based content views? In our project we have current folder structure similar to the one below (all content views).

/Dashboard
/Dashboard/Report1.cshtml
/Dashboard/Report2.cshtml

We have two types of users. Regular users can access/manage the “campaigns” they own. Administrative users have access to ALL “campaigns” across all users.

For regular users, Report1,Report2,etc.are fine but for admin users we’d like to have a different view for each route:

Seems we could go about this different ways:

  • Inside of each razor view, check the role and only render applicable content / js – would get messy to maintain.
  • Break each view into separate “partial” views (Report1Partial.cshtml, AdminReport1Partial.cshtml) and check the role in the view and render applicable partial. Less messy.
  • Create new request dto / service implementations, move the views inside the “Views” folder and control the view rendered in the service.
  • Go back to MVC Controller/View(s)
  • Keep it simple and just provide different routes for content pages that checks for required roles: (Admin/Dashboard/Report1.cshtml, etc)

Suggestions?

Sounds like you’ve covered all your possible options, it’s really up to your preference on what you think is the simpler more maintainable option.

If you want to show different content in the view page your options are pretty much to check the role and render the applicable content which depending on the amount and reusability of the content will be either in-line or refactored out into partials.

If the Views are very different you can can just have your Service render different Razor Views based on roles.

Personally I think I’d just provide distinct routes for the admin pages which I’d assume to be more maintainable as more features are added, it would also makes the impl for each page cleaner and easier to rationale about.