MVVM and role-based security

I have a Silverlight application (MVVM) with a view that will be used by several roles within the application, accessibilty of certain ui controls in the view depends on the role of users. How should this be managed in a presentation model? Are there any object templates that I have to consider?

Any ideas / recommendations are welcome.

+4
source share
3 answers

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

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

You can then bind the IsEnabled property (either IsReadOnly or Visibility ) to the appropriate controls in your view for this property. How:

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

Hope this helps!

+3
source

The parts of the presentation specific to a particular role most likely exist in some kind of container (grid, stackpanel, tabitem, etc.), and if that happens, I will think that your decision will bind the binding set for the Visibility property.

You would create a static resource with a scope that would make it easier to call IsInRole for the current user. In your binding, you will have to refer to the converter (go from bool to visibility), and there you can pass the name of the role you are testing.

0
source

Source: https://habr.com/ru/post/1338546/


All Articles