views:

163

answers:

4

I've been trimming the UI of our website by doing the following in the onload event of that control:

btnDelete.isVisible = user.IsInRole("can delete");

This has become very tedious because there are so many controls to check again and again. As soon as I get it all working, designers request to change the UI and then it starts all over.

Any suggestions?

+3  A: 

One simple suggestion would be to group controls into panels based on access rights

Christian Hagelid
+2  A: 

Something I have done before has been to create a custom page class (Actually, I do this part on every project) that each ASP.NET Page inherits.

This page class contains an IsAdmin property.

I then subclass the commonly used controls that may or may not be visible between modes into custom controls, and add code to check the Pages IsAdmin property.

All this is maybe an hour of work, but if you build pages using these controls, they manage their mode automatically.

Another fun timesaving tip is if you need to flip the page in and out of readonly mode. I added a property to the main base class, and then added a custom control that renders a textbox in one mode, and a label in the other.

Again, a little bit of time on the components, but then you can create a readonly version of the page in 2 lines of code...Very worth it.

FlySwat
+2  A: 

You may be thinking of the situation in the wrong way. Instead of thinking of individual controls, think of it in terms of business roles and what they have the ability to do. This goes along with grouping controls into panels for access rights. For example, maybe only managers have the ability to delete and do other things, and you have a role for managers that you check. This way if there are changes, you can just move users into different roles. Business rules should not change drastically. There will always be tweaking as new positions gain more responsibility, but thinking of it in this way should minimize the number of changes to be made.

Chris Westbrook
A: 

A quick and dirty option is using the asp:loginview controls, which can be wired up to user roles.

Not as elegant as the custom page class option suggested by Jonathan, and can be a bit of a performance hit if they are all over the page.

seanb