Value manilpulation using validation rules

Value manilpulation using validation rules

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


ajj3085 posted on Monday, December 22, 2008

Hi,

I had a BO that I was upgrading from the 1.0 version of Csla.  One of the the property setters for a Password property would hash the password before storing it in the field.

In upgrading it, I changed this manipulation to occur in a validation rule.  This worked fine, it generated a new salt value, then hashed the password, and set the property value to the generated hash... until..

I have other rules, string required, on other properties.  So I need to call ValidationRules.CheckRules just before DataPortal_Create / Fetch.  The fetch is the problem; since the value from the database is already hashed, and the rules are run, it would re-hash the value and update the salt.  Not what I wanted.

So, I added a flag to detect if the value was changed via the property setter, and the rule only operates if that flag is true.  The rule sets it back after it runs.  Of course, I now have code again in my property setter, and the whole point was to get rid of code there!  Just one line, but it's critical that it not be lost if I regenerate properties..

Any ideas?

Thanks
Andy

McManus replied on Monday, December 22, 2008

Hey Andy,

Isn't it possible to check the IsDirty flag of the business object in the validation rule. If it's false, you must not rehash the password, otherwise you should.

Cheers,
Herman

ajj3085 replied on Tuesday, December 23, 2008

Hmm, I hadn't thought of that.  I'll need to double check though when IsDirty is set to true.  I thought it was after the rules had been run, during the PropertyHasChanged, but I'm not sure.

rsbaker0 replied on Monday, December 22, 2008

Checking IsDirty might be helpful, but I'd think that somehow to make this foolproof you would need to know the original value that you last computed a hash for and not recompute it as long as it matches.

I have a similar use case and have two properties - the hash value is a separate property from what the user has keyed, which perhaps simplifies this.

Copyright (c) Marimer LLC