您可以在运行时评估方法的属性吗?
我有一个松散地基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您绝对可以取回方法的属性并用它们做一些事情。
例如(抱歉,在 C# 中):
您可以在其中确定如何处理属性:
我将保留最后一个任务,即确定用户是否满足属性的要求。我对框架的那部分还不够熟悉。您还可以处理错误处理(例如,该名称的方法不存在)。
我还要补充一点,您可能希望缓存(也许在静态字段中?)方法反射的结果,因为它永远不会改变。您还需要确保当主体更改或主体的角色集合更改(即,如果它确实发生更改)时,您的视图模型会触发属性更改通知。
Yes, you most definitely can get back the attributes on the method and do stuff with them.
For example (sorry in C#):
Where you can work out what to do with the attribute:
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).