如何在 WPF Catel 框架中控制两个以上角色的访问

本文关键字:两个 访问 角色 WPF Catel 框架 控制 | 更新日期: 2023-09-27 18:32:44

例如,我有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>

它不起作用请帮帮我

这是身份验证提供程序.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;
    }
}

如何在 WPF Catel 框架中控制两个以上角色的访问

也许你可以这样做:

<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 元素是可访问的,如果不是,则返回 false,因此隐藏 UI 元素。