View/Edit Mode and Authorization Rules

View/Edit Mode and Authorization Rules

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


knute posted on Friday, June 30, 2006

I have a requirement for a form to support view/edit behavior.  In other words, the form is initially in "view" mode and the user must click a button to enter "edit" mode.  Once in "edit" mode, the user can "save" or "cancel" their changes to return to "view"mode etc.

There are a couple of ways to do this using panels and such but I was wondering if anybody has tried to work this at a control level, factoring in the CSLA authorization behavior, without battling for who is managing each control's Enabled and ReadOnly properties.  Maybe I should forgo using CSLA authorization at the control level (no ReadWriteAuthorization extender)?

I am new to CSLA and learning its structure so I may be missing an obvious place/way to plug/build this functionality in.

Q Johnson replied on Friday, June 30, 2006

My ISP Mail Server is "having issues" and is apparently dropping random e-mails to me
during the week of 6/26 - 6/30.

If you don't receive a timely reply during this period, PLEASE try again OR BETTER YET
pick up the phone and call me at 512 255-7566!

The ISP is replacing the server on 6/30, so the problem should be gone by next week.

FYI, if you are receiving this notice it should mean that I _HAVE_ received the mail
message to which this is auto-responding. But your last few or your next few
might be (or might have been) victims.

Sorry for the inconvenience.

Q

ajj3085 replied on Friday, June 30, 2006

This could be way out there, but would it be possible to override CanWriteProperty and have it fail / return false if the EditLevel is not 0?  Whether or not this works will depend on when databinding calls BeginEdit on your object.

knute replied on Friday, June 30, 2006

Hmmm... I see that CanWriteProperty is virtual and I believe the book talked about overriding this to suit your needs so that makes a lot of sense.  I'll try it out.  Thanks for the suggestion ajj3085.

ajj3085 replied on Friday, June 30, 2006

Let us know how this works too; there could be consequences I didn't think about.

jwooley replied on Friday, June 30, 2006

I implemented a scheme in 1.x before the current authorization roles scheme came out whereby I have a function on my business object called FieldEditable where I pass the field name in and find out if it is editable or not. In my forms, I create a collection which helps to manage the databindings. I can then call a method to set visible based on the object's editable status and it loops through the control collection setting the enabled property on a per-field basis depending on the FieldEditable value for that field. This was necessary for 2 purposes: As my objects change values (dates) the state changes which changes which fields are then editable, also I had a business rule which allowed for certain users to be able to override values regardless of state.

It is a rather flexible model. I didn't go to the level of checking for FieldEditable on each property set, but that would probably be a bit more robust. I have yet to ponder how it would fit with the new scheme in 2.0 as I'm still stuck in 1.x world having made customizations to the framework and not having the time to retrofit them into 2.x at this point.

Jim Wooley
http://devauthority.com/blogs/jwooley/default.aspx

knute replied on Friday, June 30, 2006

That sounds very interesting Jim.  I'm not familiar with 1.x but from my quick experiments based on ajj3085 comments, I think your scheme would fit nicely right along side the 2.0 authorization roles w/o having to change the frameworks.  I don't require the flexiblity your solution provides (yet!) in regards to editability but it is giving me a few ideas to solve some other hurdles I have coming up.

knute replied on Friday, June 30, 2006

The good news is that overriding the CanWriteProperty (in my BO) does do the trick in the UI.  However, I'm not sure how you were proposing to access the EditLevel property.  I just used an "editing" property in the BO to test overriding CanWriteProperty, not using EditLevel itself.  Were you suggesting a change to the framework itself?

xal replied on Friday, June 30, 2006

EditLevel is accessible inside your business object... It just doesn't appear in intellisense because it's marked with <EditorBrowsable(EditorBrowsableState.Never)>.

Andrés

knute replied on Friday, June 30, 2006

Thanks Xal.  Putting it all together, this seems to work pretty good.  Here is what I did.  I put this in my BO:

  public override bool CanWriteProperty(string propertyName)
  {
   if (this.EditLevel > 1)
    return base.CanWriteProperty(propertyName);
   else
    return false;
  }

Add a ReadWriteAuthorization control to the form and set the applicable data entry controls to ApplyAuthorization.  Then when I want to enter "Edit" mode, I do a BeginEdit() followed by a call to:

readWriteAuthorization.ResetControlAuthorization();

to refresh/enable the data entry controls.  After I ApplyEdit() and save or CancelEdit(), I call ResetControlAuthorization() again to refresh/disabled the controls.

Although I am not using Authorization roles right now, they should still take effect so proper authorization should remain intact.  I think I will consider moving the CanWriteProperty() code above to an abstract class off BusinessBase for this application so similiar BOs can share this behavior.

Thanks to everyone who chimed in!

ajj3085 replied on Friday, June 30, 2006

The button could call BeginEdit, and then you can try calling the bindingsource's ResetBindings or ResetCurrentItem method..

As I said, there may be problems with the approach I've suggested, I'm not sure others have done this.  I would think that the resetting of the bindings would then re-evaluate the ability to read / write properties.

knute replied on Friday, June 30, 2006

ajj3085, I actually am using ResetControlAuthorization() of the ReadWriteAuthorization control to re-evaluate.  Does that make sense?

Thanks for your help.

ajj3085 replied on Monday, July 03, 2006

Yes, makes sense to me.. I just read the part of the book on that control and it sounds like that's exactly the way to go.

Andy

Copyright (c) Marimer LLC