views:

497

answers:

2

Hi,

I have a button user control, on which I've created a property, UserRights, which I use to define the rights a user must have before the button is enabled. These rights are defined as public constants in a class called UserRight (I don't use Enum for some special code design reasons). So, what I would like to achive is this:

<hmk:Button Id="BtnSave" UserRights='<%#UserRight.Create, UserRight.Modify%>' ... runat="server" />

Right now, the UserRights property is implemented as:

public List<int> UserRights {get; set; }

And therefore, I need to do like this:

<hmk:Button Id="BtnSave" UserRights='<%#new List<int> {UserRight.Create, UserRight.Modify }%>' ... runat="server" />

Is it possible to achieve what I want as stated in the first example?

Best regards Henrik

A: 

(re the syntax proposed - no, not that I know of)

Even if they aren't enums - are they bitwise flags? You could have an int property and use UserRight.Create | UserRight.Modify, and have the property setter take the bits apart into the component flags.

But it would be a lot easier with a [Flags] enum. I'd be interested in what those reasons are... if you were using an object/string, then fine - but with an int... I wonder if there is a trick that might let you use it the "easy" way.

Marc Gravell
The reason why I'm not using Enum is because the applications consists of 4-5 sub-applications, so we would like to use inheritance in the user rights, which makes it possible for re-using "global" rights in the sub-applications. There can be quite a lot of rights, so using bitwise flags will be difficult to maintain, I think.
HMK
Would it be possible to do like this instead:<hmk:Button Id="BtnSave" UserRights="UserRight.Create, UserRight.Modify" ... runat="server" />and somehow parse the string and convert it using reflection or?
HMK
Sure, you could **manually** parse it as a string using reflection.... not easy, though.
Marc Gravell
A: 

I think you should handle your button state and overall action logic at the composite control level (which gets passed from a higher level such as the page the control is on, or the part of your website the page belongs to).

We would like the buttons to be reusable and self-contained, so we can reuse them without being dependent on the different pages / controls that contain them. Is that a bad design?
HMK
That would be a very smart button indeed. I've never thought about buttons as being smart :D It really depends on your requirements, but why would you present a form loaded with data and a disabled button to users?
Some forms will be displayed to different users with different roles and hence different rights. Some users have readonly rights and in that case, a save button should be disabled
HMK
That was my point when I said you should control it upper in the hierarchy. I doubt that the only difference for different users would only be the state of that Save button.