开发者

MVVM and Role Based Security

I have a silverlight application (MVVM) with a view that will be used by multiple roles within the application, the accessibilty of certain ui开发者_C百科 controls in the view is dependant on the users role. How should this be managed within the view model? Are there any object patterns that I should be considering?

Any ideas/guidance would be greatly appreciated.


The first idea that comes to mind is to have properties in your ViewModel that correspond to whether or not the current user has the ability to perform certain operations. For example:

public bool CanChangeDisplayName {
    get {
        bool result = SomeMechanismToDetermineUsersAbilityToPerformAction();
        return result;
    }
}

Then you could bind the IsEnabled (or IsReadOnly or Visibility) property on the appropriate controls on your View to this property. Like:

<TextBox IsReadOnly="{Binding CanChangeDisplayName}" Text="{Binding DisplayName}"/>

I hope this helps!


The parts of the view that are role specific are likely in some sort of container (grid, stackpanel, tabitem, etc) and if that's the case then I would think your solution will hing on the binding you set for the Visibility property.

You would create an application scoped static resource that would facilitate a call to IsInRole for the current user. In your binding you'll have to reference a converter (to go from bool to visibility) and it's there that you could pass the name of role you're testing for.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜