您可以在运行时评估方法的属性吗?

发布于 2024-11-08 05:35:35 字数 1051 浏览 0 评论 0原文

我有一个松散地基于 MVVM 的 Winforms 项目。安全性是由域层使用PrincipalPermissionAttribute实现的,如下所示:

Public Class Order

     <PrincipalPermissionAttribute(SecurityAction.Demand, Role:="Managers")> _
    Public Sub ChangeBillingAddress(NewAddress as Address)

        Me.BillingAddress = NewAddress

    End Sub


End Class

我希望我的ViewModel能够根据域中的PrincipalPermissionAttribute指示视图启用/禁用哪些控件:

Public Class OrderViewModel

    Private _Order as Order
    Public Sub New(Order as Order)
        _Order = Order
    End Sub

    Public Readonly Property ChangeBillingAddressEnabled as Boolean
        Get
            'Here I want to take Thread.CurrentPrincipal and evaluate
            'it's Role against the PrincipalPermissionAttribute on 
            '_Order.ChangeBillingAddress.  If the user will succeed
            'in changing the billing address return True, else return False.
        End Get

    End Property
End Class

ViewModel是否有办法评估PrincipalPermissionAttribute并确定当前Thread.Principal是否会成功?

I have a Winforms project that is loosely based on MVVM. Security is implemented by the domain layer by using the PrincipalPermissionAttribute, like so:

Public Class Order

     <PrincipalPermissionAttribute(SecurityAction.Demand, Role:="Managers")> _
    Public Sub ChangeBillingAddress(NewAddress as Address)

        Me.BillingAddress = NewAddress

    End Sub


End Class

I would like for my ViewModel to be able to instruct the view on which controls to enable/disable based on the PrincipalPermissionAttribute in the domain:

Public Class OrderViewModel

    Private _Order as Order
    Public Sub New(Order as Order)
        _Order = Order
    End Sub

    Public Readonly Property ChangeBillingAddressEnabled as Boolean
        Get
            'Here I want to take Thread.CurrentPrincipal and evaluate
            'it's Role against the PrincipalPermissionAttribute on 
            '_Order.ChangeBillingAddress.  If the user will succeed
            'in changing the billing address return True, else return False.
        End Get

    End Property
End Class

Is there a way for the ViewModel to evaluate the PrincipalPermissionAttribute and determine if the current Thread.Principal will succeed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囍孤女 2024-11-15 05:35:35

是的,您绝对可以取回方法的属性并用它们做一些事情。

例如(抱歉,在 C# 中):

return _Order.GetType()
             .GetMethod("ChangeBillingAddress")
             .GetCustomAttributes(typeof(PrincipalPermissionAttribute), true)
             .Cast<PrincipalPermissionAttribute>()
             .All(r => IsPermittedAccess(r, Thread.CurrentPrincipal));

您可以在其中确定如何处理属性:

bool IsPermittedAccess(PrincipalPermissionAttribute rule, IPrincipal user)
{
    // return ?
    throw new NotImplementedException();
}

我将保留最后一个任务,即确定用户是否满足属性的要求。我对框架的那部分还不够熟悉。您还可以处理错误处理(例如,该名称的方法不存在)。

我还要补充一点,您可能希望缓存(也许在静态字段中?)方法反射的结果,因为它永远不会改变。您还需要确保当主体更改或主体的角色集合更改(即,如果它确实发生更改)时,您的视图模型会触发属性更改通知。

Yes, you most definitely can get back the attributes on the method and do stuff with them.

For example (sorry in C#):

return _Order.GetType()
             .GetMethod("ChangeBillingAddress")
             .GetCustomAttributes(typeof(PrincipalPermissionAttribute), true)
             .Cast<PrincipalPermissionAttribute>()
             .All(r => IsPermittedAccess(r, Thread.CurrentPrincipal));

Where you can work out what to do with the attribute:

bool IsPermittedAccess(PrincipalPermissionAttribute rule, IPrincipal user)
{
    // return ?
    throw new NotImplementedException();
}

I'll leave the last task of deciding whether the user meets the attribute's requirements or not. I'm not familiar enough with that part of the framework. Also you can deal with the error handling (e.g. method doesn't exist with that name).

I'll also add that you would probably want to cache (in a static field perhaps?) the results of the reflection on the method since it'll never change. You'll also want to be sure that your view model is firing property change notifications when the principal changes or principal's roles collection changes (that is, if it does in deed change).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文