tags:

views:

49

answers:

3

I am a Silverlight newbie...

I want a page to display elements (button, grid, images) based on the current user security role. Basically, I would like to attach to an element the required security role and have my code being called to decide whether the element will show or not.

Something like (adding an attribute RequiredRole)

<Button RequiredRole="Administrator" x:Name="PushBtn" Content="PushMe"/>

In this case my code will set the Button.visibility to hidden if the user does not have enough security rights.

I could not find anything in the documentation (except of maybe DependentProperties) that could help me achieve something like this.

How can this be done? (If this is at all the right way to think about this problem...)

Thanks.

A: 

One approach would be to bind the button to a command object. On the "CanExecute" of the command object, you would perform your security check. This, of course, would only work for buttons.

Probably a more interesting approach is to create an object that manages your security and provides the security values via an indexer. You can create a behavior for the element visibility that interfaces with the object. Then, you'd do something like:

The security behavior would take in the button on attached, query the security system, and collapse the visibility if the user is not authorized.

Jeremy Likness
A: 

Recursively enumerate all of the controls on a given form and set their visibility accordingly. You could put this logic in a helper or base class.

public partial class MainPage : UserControl
{
    public MainPage()
    {
        InitializeComponent();

        foreach (var control in LayoutRoot.GetVisuals().OfType<Control>())
            control.Visibility = Visibility.Collapsed;
    }
}

public static class ExtensionMethods
{
    public static IEnumerable<DependencyObject> GetVisuals(this DependencyObject root)
    {
        int count = VisualTreeHelper.GetChildrenCount(root);

        for (int i = 0; i < count; i++)
        {
            var child = VisualTreeHelper.GetChild(root, i);
            yield return child;

            foreach (var descendants in child.GetVisuals())
                yield return descendants;
        }
    }
}
David
A: 

You have logic to determine the user's role and looking to toggle visibility accordingly? Sounds like you're already on the right track. How about an attached property? You can include a PropertyChangedCallback when defining the property metadata. That PropertyChangedCallback method can handle toggling visibility.

Here's the basic idea...

public static class RequiredRoleSupport
{
    public static readonly DependencyProperty RequiredRoleProperty = DependencyProperty.RegisterAttached("RequiredRole", typeof(string), typeof(RequiredRoleSupport), new PropertyMetadata(ValidateRole));

    public static string GetRequiredRole(DependencyObject obj)
    {
        return (string)obj.GetValue(RequiredRoleProperty);
    }

    public static void SetRequiredRole(DependencyObject obj, string value)
    {
        obj.SetValue(RequiredRoleProperty, value);
    }

    private static void ValidateRole(object sender, DependencyPropertyChangedEventArgs e)
    {
        var control = sender as Control;
        var roleValue = (string)e.NewValue;

        // Logic to check roleValue against current user role and modify control accordingly
    }
}

XAML

<UserControl xmlns:roleSupport=...

<Button roleSupport:RequiredRoleSupport.RequiredRole="Administrator" x:Name="PushBtn" Content="PushMe"/>
Brandon Copeland
Thanks -- this is the type of idea I had in mind but I was lacking the details. I will try this.
RenR