在后面的代码中设置WPF UI权限 [英] Setting WPF UI Permissions in Code Behind

查看:397
本文介绍了在后面的代码中设置WPF UI权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找是否可以在WPF中执行以下操作:

I am looking to see if I can do the following in WPF:

用户打开WPF应用程序,并使用用户AD名称检查自定义数据库中的用户角色.这将类似于asp.net成员数据库.

A user opens the WPF application and using the users AD name I will check the users role(s) in a custom DB. This will be something similar to asp.net membership db.

根据用户角色,WPF将显示某些控件.

Depending on the user role the WPF will show certain controls.

例如,如果我是管理员,那么我将有权访问所有内容,但是如果我是只读用户,那么我将只能访问该应用程序.

So for example, if I was an Admin then I would have access to everything but if I was read-only user then I would have limited access to the application.

我知道我可以使用以下示例设置可见性:

I know I can set the visibility using the following example:

public class RoleToVisibilityConverter : MarkupExtension,IValueConverter
{
    public RoleToVisibilityConverter()
    {

    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo                                 culture)
    {
        var principal = value as GenericPrincipal;
        bool IsValidUser = false;
        if (principal != null)
        {
            foreach (String role in parameter.ToString().Split(';'))
            {
                if (principal.IsInRole(role))
                {
                    IsValidUser = true;
                    break;
                }  
            }
            return IsValidUser ? Visibility.Visible : Visibility.Collapsed;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        return this;
    }
}

在XAML中,我为可见性添加了以下绑定:

And in the XAML I add the following binding for Visibility:

Visibility="{Binding Source={x:Static systhread:Thread.CurrentPrincipal}, ConverterParameter=admin;editor;readonly, Converter={rv:RoleToVisibilityConverter}}

但是我要做的是不必在xaml中添加它.

But what I want to do is to not have to add this in xaml.

我希望能够获得用户角色及其所有权限,并取决于窗口加载的UI权限.

I would like to be able to get the users role and all of it's permissions and depending on the window load the UI permissions.

原因是,我希望能够更改只读角色可以查看/执行的操作,而不必更改xaml.

Reason is that I would like to be able to change what a read-only role can see/do without having to change the xaml.

有人知道这是否可行吗,这是最佳实践.

Does anyone know if this is possible and if so is it best practice.

谢谢.

Noel

推荐答案

如果使用的是MVVM,则视图模型应具有诸如public bool IsXXXAllowed { get; private set; }之类的属性.实例化视图模型后,应查询用户是否具有使用XXX功能所需的最低权限,并相应地设置属性的值.理想情况下,权限检查应该与角色无关,因为可以为用户创建角色以进行嵌套等.您可以使用fn_my_permissions直接查询表上的权限.

If you're using MVVM, then your view model should have properties such as public bool IsXXXAllowed { get; private set; }. When the view model is instantiated you should query whether the user has the minimum permissions required to use XXX feature and set the value of the property accordingly. The permission check should ideally be agnostic of roles since roles can be user created for nesting etc. You can directly query the permissions on a table using fn_my_permissions.

然后在视图中将相关控件的IsEnabled属性(或使用BooleanToVisibilityConverterVisibility属性)简单绑定到属性.

Then in the view simply bind the IsEnabled property (or Visibility property using BooleanToVisibilityConverter) of the relevant control (or a Grid enclosing the controls if there is more than one control) to the IsXXXAllowed property.

这篇关于在后面的代码中设置WPF UI权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆