Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How far to 'drill down' roles in a .NET MVC application?

I've written a few complex MVC applications which are all role based and use .NET Membership. On my first project I used roles with structure similar to this:

  • Admin
  • Manager
  • Approver

I quickly discovered that wasn't very scalable, for example a customer would say "I want specific user x to have all manager privileges but not delete". I would then have to put a hack in the controller for that user.

Therefore, my second implementation led to this role structure:

  • CanCreate
  • CanDelete
  • CanEditAll
  • CanEditOwn

This approach then led to literally dozens of roles based on whether they could edit particular items globally or just their own etc. It also leads to a lot more controller actions and considerably more code - though maybe thats just the case in a complex application!

My question is, am I approaching this in the correct way, and are there any good online resources on the "correct" way to approach complex applications with loads of roles. Am I doing this correctly?

like image 445
Chris Avatar asked Jun 29 '11 10:06

Chris


People also ask

What is the correct order for the lifecycle of an ASP.NET MVC page?

MVC actually defined in two life cycles, the application life cycle, and the request life cycle. The Starting point for every MVC application begins with routing. After that, the received request figures out and finds how it should be handled with the help of the URL Routing Module.

What's the entry point of an application in the MVC app architecture?

The view is the entry point to the Application. One to many relationships between Controller & View.


2 Answers

Indeed it's very interesting topic and I found myself struggling with the same problems as you do.

I read Derick Baileys interesting blog about that "Don’t Do Role-Based Authorization Checks; Do Activity-Based Checks" : http://lostechies.com/derickbailey/2011/05/24/dont-do-role-based-authorization-checks-do-activity-based-checks/

but had not time to experminet it myself.

like image 89
Tomasz Jaskuλa Avatar answered Oct 11 '22 16:10

Tomasz Jaskuλa


A year on from this question I handle things a different way across projects. I'm now sticking to the classic roles of:

  • View
  • Edit
  • Delete
  • Add

BUT the action methods themselves return data like this:

var order = or.MyVisibleOrders().FirstOrDefault(x => x.Id == Id);

The logic for what is viewable and what is not is then handled by roles in the repository. The database will essentially never be queried for the restricted items in the first place.

Basic stuff but felt I should follow up on myself.

like image 32
Chris Avatar answered Oct 11 '22 17:10

Chris