views:

36

answers:

1

I think I may have made a mess of my controllers. I have a fairly simple site that allows users of type "Staff" or "Client" to view Projects. Staff have access to all projects, can add and delete them, add and delete users, assign clients permission to projects, etc.

As it stands this means I have a UserController and ProjectController, littered with Authentication attributes (ie. so a Client can change his email but nothing else like permissions, and Clients can't add/remove/edit projects but can browse their contents).

And also, based on the user type/role, I switch out menu components. Should I be splitting up my controllers to have narrower focus, or is this a candidate for Areas? I "feel" like to much of my code is checking whether user X can do action Y based on both his role AND explicit per-project permissions.

PS. I rewrote the subject several times trying to make it shorter :\

+2  A: 

Have you considerd splitting your controllers like so:

  • ClientUserController

  • ClientProjectController

  • StaffUserController

  • StaffProjectController

Splitting like this would allow you to easily have specialised controllers and views for each user type. Use a single Authentication attribute at the top of each "Staff" controller to secure access.

By setting up approriate routes, you could mainatin friendly URLs to each controler type: eg

  • /User/Edit (Shows all User detail, but only allows edit of Email field)
  • Staff/User/Edit (Allows edit of all User fields)
JcMalta
Yeah that might be the way to go. I did start off with a ie. UserAdminController, but I ended up with too much duplication. I'll see if I can clean it up a bit.
George R
You can always share common functionality between controllers ... remember that Controllers are really nothing more than classes - it is only the naming conventions (to enable to framework to call the correct method) that make them seem "magical".Also, never underestimate the power of using different ViewModels for acheiving different functionality .. your main controller code could remain the same, but the data presented to the View (including Display-type attributes etc) could vary according to which view you decide to present and the data (both real and meta-data) that you pass.
JcMalta