David
David

Reputation: 3609

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 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.

Upvotes: 4

Views: 1799

Answers (2)

Mark Carpenter
Mark Carpenter

Reputation: 17775

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!

Upvotes: 3

Ralph Shillington
Ralph Shillington

Reputation: 21098

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.

Upvotes: 0

Related Questions