Register submissions with regions based on user rights

I have a TabControl as an ItemControl, let me call it ContentRegion. Several modules register at least one view in ContentRegion. But these registrations are performed during module initialization.

I want to prohibit the registration of multiple views depending on the current user. But the user logs in after initializing the module and can also change at runtime.

Is there a way to provide a callback when the prism can evaluate if registration is active? Or do I have the option to disable the registration of a region manager? Any other ideas?

+3
source share
2 answers

The answer is quite simple: follow the user region behavior. You just need to extract from the existing AutoPopulateRegionBehaviour:

public class SecurityEnabledAutoPopulateRegionBehaviour : AutoPopulateRegionBehavior
{
    IUnityContainer container;

    public SecurityEnabledAutoPopulateRegionBehaviour(IUnityContainer container, IRegionViewRegistry regionViewRegistry)
        :base(regionViewRegistry)
    {
        this.container = container;
    }

    protected override void AddViewIntoRegion(object viewToAdd)
    {
        IRequiredAccessRight viewPermission = viewToAdd as IRequiredAccessRight;
        if ( viewPermission != null )
        {
            ISessionManager sessionManager = container.Resolve<ISessionManager>( );
            if ( sessionManager.AccessRights.IsGranted( viewPermission.RequiredAccessRight ) )
            {
                this.Region.Add( viewToAdd );
            }
        }
        else
        {
            this.Region.Add( viewToAdd ); //The region does not require any permissions so we can proceed
        }
    }
}

The final step is to override all AutoPopulateRegionBehaviours or only in certain regions. How to do this is described in detail in Appendix E of the Prism documentation. What I did was bind the behavior only to a specific region and replace AutoPopulateRegionBehaviour:

public partial class MyView : UserControl
{
    public MainView( IUnityContainer container ) 
    {
        InitializeComponent( );

        ObservableObject<IRegion> observableRegion = RegionManager.GetObservableRegion( ControlHostingTheRegion );

        observableRegion.PropertyChanged += ( sender, args ) =>
        {
            IRegion region = ( (ObservableObject<IRegion>)sender ).Value;
            region.Behaviors.Add( AutoPopulateRegionBehavior.BehaviorKey,
                (SecurityEnabledAutoPopulateRegionBehaviour)container.Resolve( typeof( SecurityEnabledAutoPopulateRegionBehaviour ) ) );
        };
    }
}
+4
source

You can bind TabItem.Visibilityto a variable that indicates whether to show it or not. After you check the user rights, set this variable so that it hides unwanted tabs.

, .

IRegion detailsRegion = regionManager.Regions["DetailsRegion"];
detailsRegion.Add(view, viewName);
detailsRegion.Activate(view); // not sure if you need the Activate
0

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


All Articles