1

For example I have 3 roles: admin, developer, project manager. If i want to disable controls for all users except for admin, i'll write:

<i:Interaction.Behaviors>
      <catel:Authentication AuthenticationTag="admin" Action="Collapse" />
</i:Interaction.Behaviors>

It works good

But if i want enable control for admin and project manager (2 or more roles) and disabled for other users, i'll write this:

<i:Interaction.Behaviors>
      <catel:Authentication AuthenticationTag="admin" Action="Collapse" />
      <catel:Authentication AuthenticationTag="project manager" Action="Collapse" />
</i:Interaction.Behaviors>

It doesn't work Help me please

Here is AuthenticationProvider.cs

public class AuthenticationProvider : IAuthenticationProvider
{
    /// <summary>
    /// Gets or sets the role the user is currently in.
    /// </summary>
    /// <value>The role.</value>
    public string Role { get; set; }

    public bool CanCommandBeExecuted(ICatelCommand command, object commandParameter)
    {
        return true;
    }

    public bool HasAccessToUIElement(FrameworkElement element, object tag, object authenticationTag)
    {
        var authenticationTagAsString = authenticationTag as string;
        if (authenticationTagAsString != null)
        {
            if (string.Compare(authenticationTagAsString, Role, true) == 0)
            {
                return true;
            }
        }

        return false;
    }
}
Kapiroska
  • 11
  • 6

1 Answers1

1

Maybe you could do this:

<i:Interaction.Behaviors>
    <catel:Authentication AuthenticationTag="admin;project manager" Action="Collapse" />
</i:Interaction.Behaviors>

And in the IAuthenticationProvider implementation:

public class AuthenticationProvider : IAuthenticationProvider
{
    /// <summary>
    /// Gets or sets the role the user is currently in.
    /// </summary>
    /// <value>The role.</value>
    public string Role { get; set; }

    public bool CanCommandBeExecuted(ICatelCommand command, object commandParameter)
    {
        return true;
    }

    public bool HasAccessToUIElement(FrameworkElement element, object tag, object authenticationTag)
    {
        var authenticationTagAsString = authenticationTag as string;
        if (authenticationTagAsString != null)
        {
            if (authenticationTagAsString.Contains(Role))
            {
                return true;
            }
        }

        return false;
    }
}

So you provide the roles that can view the specific UI element as a semicolon separated list. If the list contains the current Role, then the UI element is accessible, if not then we return false, therefore hide the UI element.

Szabolcs Dézsi
  • 8,743
  • 21
  • 29