摘要
若窗体(页面)具有「执行、新增、修改、删除」等操作权限控管,可以使用列举来描述使用者在该窗体的权限,详细的作法可以参考下面的「Enum 的设计与应用 - 简易权限设计」这篇文章。在此我们将利用这种列举的方式来描述窗体权限,并由 BasePage 来处理窗体权限的控管。
程序说明及实作
首先定义 EFormActions 列举,来描述窗体操作权限。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**/''' <summary>
''' 窗体操作权限列举。
''' </summary>
<Flags()> _
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Enum EFormActionsEnum EFormActions
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 无。
''' </summary>
None = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 执行。
''' </summary>
Execute = 1
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 新增。
''' </summary>
Add = 2
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 修改。
''' </summary>
Update = 4
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 删除。
''' </summary>
Delete = 8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 全部。
''' </summary>
All = Execute Or Add Or Update Or Delete
End Enum
新增一个 TBActionPage 类别,继承至 System.Web.UI.Page 或自订的底层 BaseForm,做为权限控管的 BaseForm。首先新增一个 UserActions 属性,来描述使用者的窗体操作权限,在 OnInitComplete 方法中,第一次加载此窗体时取得使用者的窗体权限,即设定 UserActions 属性,此属性会储存至 ViewState 中,使下次页面 PostBack 时无需再重新取得窗体权限。另外新增 CheckUserActions 方法,供检查使用者是否具有指定的窗体操作权限。
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Public Class TBActionPageClass TBActionPage
Inherits System.Web.UI.Page
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 使用者权限。
''' </summary>
Private FUserActions As EFormActions = EFormActions.None
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
ViewState 状态管理#Region " ViewState 状态管理 "
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 由 ViewState 还原控件的状态。
''' </summary>
''' <param name="savedState">要还原的控件状态。</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overrides Sub LoadViewState()Sub LoadViewState(ByVal savedState As Object)
If Not (savedState Is Nothing) Then
' Load State from the array of objects that was saved at ;
' SavedViewState.
Dim myState As Object() = CType(savedState, Object())
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
If Not (myState(0) Is Nothing) Then
MyBase.LoadViewState(myState(0))
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
If Not (myState(1) Is Nothing) Then
FUserActions = CType(myState(1), EFormActions)
End If
End If
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 控件的状态储存至 ViewState。
''' </summary>
''' <returns>含有控件之目前检视状态的对象。</returns>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overrides Function SaveViewState()Function SaveViewState() As Object
Dim baseState As Object = MyBase.SaveViewState()
Dim myState(1) As Object
myState(0) = baseState
myState(1) = FUserActions
Return myState
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#End Region
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 覆写。引发 InitComplete 事件。
''' </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Overrides Sub OnInitComplete()Sub OnInitComplete(ByVal e As System.EventArgs)
If Not Me.IsPostBack Then
'取得使用者的窗体权限
'测试用:设定使用者具「执行/新增/修改」权限,实际应呼叫中间层取得使用者的此窗体权限
Me.UserActions = EFormActions.Execute Or EFormActions.Add Or EFormActions.Update
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
'第一次加载页面时检查执行权限
If Me.UserActions = EFormActions.None Then
Throw New Exception("没有此窗体执行权限")
End If
End If
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MyBase.OnInitComplete(e)
End Sub
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 使用者的窗体操作权限。
''' </summary>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Property UserActions()Property UserActions() As EFormActions
Get
Return FUserActions
End Get
Set(ByVal value As EFormActions)
FUserActions = value
End Set
End Property
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/''' <summary>
''' 检查使用者是否具有指定的窗体操作权限。
''' </summary>
''' <param name="Actions">窗体操作权限</param>
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Public Function CheckUserActions()Function CheckUserActions(ByVal Actions As EFormActions) As Boolean
If (Me.UserActions And Actions) = Actions Then
Return True
Else
Return False
End If
End Function
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
End Class
测试程序
为测试使用,在上述的 TBActionPage 的 OnInitComplete 方法,直接设定具有「执行/新增/修改」权限;正确的作法应该是呼叫中间层传入该窗体的识别码来取得使用者在该窗体具有那些权限,并在此设定 UserActions 属性值。
Me.UserActions = EFormActions.Execute Or EFormActions.Add Or EFormActions.Update
新增一个测试页面,继承至 TBActionPage,当按下按钮时利用 CheckUserActions 使用者具那些权限并输出。
Imports Bee.Web.WebCommon
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
Partial Class _DefaultClass _Default
Inherits Bee.Web.TBActionPage
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Protected Sub Button1_Click()Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Me.Response.Write("執行 -> " & IIf(Me.CheckUserActions(EFormActions.Execute), "Y", "N"))
Me.Response.Write("< br/ >")
Me.Response.Write("新增 -> " & IIf(Me.CheckUserActions(EFormActions.Add), "Y", "N"))
Me.Response.Write("< br/ >")
Me.Response.Write("修改 -> " & IIf(Me.CheckUserActions(EFormActions.Update), "Y", "N"))
Me.Response.Write("< br/ >")
Me.Response.Write("刪除 -> " & IIf(Me.CheckUserActions(EFormActions.Delete), "Y", "N"))
End Sub
End Class
执行结果如下
![](http://files.dotblogs.com.tw/jeff377/0806/BasePage_13E30/image_thumb.png)