Calling CanWriteProperty with string literal

Calling CanWriteProperty with string literal

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


mercule posted on Friday, March 16, 2007

I'm setting up a BusinessBase object and adding authorization to it.  If I use this code in a property:

CanWriteProperty(true);

the authorization works as expected.  But, if I use this:

CanWriteProperty("ResearchAnalystId");

my authorization always passes, whether or not I'm part of a group that has authorization.  For referrence, the line from AddAuthorizationRules() is:

AuthorizationRules.AllowWrite("ResearchAnalystId", "Administrator");

My main reason for wanting to call this method with a literal is to check authorization from the UI to control display of fields (label vs. dropdown).  What am I doing wrong?  Is there another way to do this?

Brian Criswell replied on Friday, March 16, 2007

The boolean tells whether you want to throw an exception on failed authorization.  You want to use CanWriteProperty("ResearchAnalystId", true);

CanWriteProperty("ResearchAnalystId"); will just tell you whether the current user can write to that property.

mercule replied on Friday, March 16, 2007

Thanks, Brian.  I was definitely missing that in the property code.  That works fine, now.

I'm still not getting the correct Boolean return from the CanWriteProperty method, though.  It always returns true, which is a problem from the UI.  I don't think using a try...catch block is probably the best way to toggle UI elements.

RockfordLhotka replied on Friday, March 16, 2007

So in the same scenario if you tell CanWriteProperty() to throw exceptions it throws, but if you tell it not to throw exceptions it returns true?

While there's always the possibility for bugs, I am a little skeptical here, because I use the ReadWriteAuthorization control all the time and it uses the boolean return technique to enable/disable the UI elements - it calls the same code you'd be calling.

That, by the way, is what I recommend, and what I show in Chapters 9 and 10 for the Windows and Web UI's - how to have the UI proactively find out what the user can and can't do so it can enable/disable the controls ahead of time.

Copyright (c) Marimer LLC