如何控制 WPF Catel Framework 中两个以上角色的访问权限?

How can I control access for more than two roles in WPF Catel Framework?

例如我有 3 个角色:管理员、开发人员、项目经理。 如果我想为除以下用户之外的所有用户禁用控件 管理员,我会写:

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

效果不错

但是如果我想为管理员和项目经理(2 个或更多角色)启用控制并为其他用户禁用,我会这样写:

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

没用 请帮助我

这里是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;
    }
}

也许你可以这样做:

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

并且在 IAuthenticationProvider 实现中:

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;
    }
}

因此,您提供的角色可以将特定 UI 元素查看为分号分隔的列表。如果列表包含当前 Role,那么 UI 元素是可访问的,如果不是,那么我们 return false,因此隐藏 UI 元素。