Handling Authorization Rules

Handling Authorization Rules

Old forum URL: forums.lhotka.net/forums/t/7014.aspx


NickTaylor posted on Thursday, May 28, 2009

Hi,

I wonder if anyone can help me with this please...?

I have just started implementing Authorization rules within my windows forms application. Its very simple at present, and for the purposes of experimentation I want to prevent a user from changing the contents of a textbox.

Within my BO I have overriden the AddAuthorizationRules() method and make a call to AuthorizationRules.AllowWrite on the appropriate field. Within the property definition I have also added the call to CanWriteProperty(true) within the set accessor. I understand that this will throw an exception when any user interaction takes place on the specified field.

When I run the form, any user interaction with that field fires the exception and in turn the form then displays the appropriate warning icon via the error provider. In short, this does exactly what I want it to do.

However....

I then can't tab off the field and the form is in effect stuck. So my question is how do I handle this within the UI ?? I deally I just want to revert the data for this field back to its clean state.

Can anyone off some suggestions / best practice advice as to how I should handle this...?

Thanks,

Nick

RockfordLhotka replied on Thursday, May 28, 2009

This is Windows Forms?

You should consider changing the UI control to read-only, or disabling it, if the user is not allowed to change the value. This does two things: it avoids the issue you are seeing, and it gives the user a proactive visual cue that they can't change the value.

There's a control in Csla.Windows that helps you do this for all detail controls (not grids or lists) on your form - the ReadWriteAuthorization control.

If you have the Expert 2005 Business Objects book the control is covered in the Windows Forms chapter. If not, you should look at the PTWin project in the ProjectTracker reference app to see how it is used.

NickTaylor replied on Thursday, May 28, 2009

Hi Rocky,

Yes, its WinForms. And yes, I have your book, and very good it is too. :o)

So far I have found the CSLA framework excellent, but as I'm migrating an internal application from Visual Foxpro, the learning curve associated with the move to dotnet is steep!! Having said that I would not go back now as its far superior!!

Thanks for the advice, I will take a look at the ReadWriteAuthorization in more detail.

Cheers,

Nick

triplea replied on Thursday, May 28, 2009

Are you using Winforms or WPF? Ideally, you may want to disable fields that are not editable using the ReadWriteAuthorization control (Winforms) or equivalent (for WPF), otherwise you will have to handle the Validate events or play with the CanWriteProperty overloads (I noticed you skipped the property name - if this is the overload you use you should refactor). Setting the value back to the original one, well have a read here first: http://www.lhotka.net/weblog/WPF40DataBindingFix.aspx

Also you may want to check this out: http://forums.lhotka.net/forums/thread/24888.aspx I used that a couple of times when I had form elements the user should not be able to edit as enabled but it is a slight variation to the existing way of declaring properties...

Copyright (c) Marimer LLC