C/S应用程序权限设计(3)
窗体访问权限的控制
此类权限就比较好控制了,你有权限就可以打开窗体,没有权限就不能打开.由于系统中所有的窗体都是基于FormBase继承而来的,那就有了集中控制权限的基础.首先创建了一个属性类,只能作用于类上,代码如下:
''' -----------------------------------------------------------------------------
''' Project : Lily.ComponentFramework
''' Class : ComponentFramework.PermissionAttribute
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 类访问许可属性定义
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2007-03-27 Created
''' </history>
''' -----------------------------------------------------------------------------
<AttributeUsage(AttributeTargets.Class)> Public Class PermissionAttribute
Inherits System.Attribute
Private md_name As String = String.Empty
Private md_guid As String = String.Empty
Private md_description As String = String.Empty
Public Sub New(ByVal name As String, ByVal guid As String, ByVal description As String)
Me.md_name = name
Me.md_guid = guid
Me.md_description = description
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 名称.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-25 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property Name() As String
Get
Return Me.md_name
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 唯一标识.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-25 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property Guid() As String
Get
Return Me.md_guid
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回描述.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-25 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property Description() As String
Get
Return Me.md_description
End Get
End Property
End Class
''' Project : Lily.ComponentFramework
''' Class : ComponentFramework.PermissionAttribute
'''
''' -----------------------------------------------------------------------------
''' <summary>
''' 类访问许可属性定义
''' </summary>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2007-03-27 Created
''' </history>
''' -----------------------------------------------------------------------------
<AttributeUsage(AttributeTargets.Class)> Public Class PermissionAttribute
Inherits System.Attribute
Private md_name As String = String.Empty
Private md_guid As String = String.Empty
Private md_description As String = String.Empty
Public Sub New(ByVal name As String, ByVal guid As String, ByVal description As String)
Me.md_name = name
Me.md_guid = guid
Me.md_description = description
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 名称.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-25 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property Name() As String
Get
Return Me.md_name
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 唯一标识.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-25 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property Guid() As String
Get
Return Me.md_guid
End Get
End Property
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回描述.
''' </summary>
''' <value></value>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-25 Created
''' </history>
''' -----------------------------------------------------------------------------
Public ReadOnly Property Description() As String
Get
Return Me.md_description
End Get
End Property
End Class
其中最主要的就是guid,每一个窗体都对应一个guid,guid是可以唯一的,窗体的名称,确不可以.如果某一个窗体需要进行权限控制,只需把PermissionAttribute作用于窗体上即可!那什么时候,进行权限判断呢,在基类窗体FormBase有如下代码:
实例化时进行权限判断
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
If Not Me.IsLoad Then
GC.SuppressFinalize(Me)
Throw New PermissionException("当前用户没有进行此项操作的权限。")
End If
End Sub
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
If Not Me.IsLoad Then
GC.SuppressFinalize(Me)
Throw New PermissionException("当前用户没有进行此项操作的权限。")
End If
End Sub
''' -----------------------------------------------------------------------------
''' <summary>
''' 返回当前用户是否有权限加载.
''' </summary>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-24 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Overridable Function IsLoad() As Boolean Implements IFormManager.IsLoad
If Not Me.PermissionAttribute Is Nothing Then
Return Lily.ComponentFramework.ComponentManager.Permission.Has(Me.PermissionAttribute)
Else
Return True
End If
End Function
''' <summary>
''' 返回当前用户是否有权限加载.
''' </summary>
''' <returns></returns>
''' <remarks>
''' </remarks>
''' <history>
''' [zqonline] 2006-12-24 Created
''' </history>
''' -----------------------------------------------------------------------------
Public Overridable Function IsLoad() As Boolean Implements IFormManager.IsLoad
If Not Me.PermissionAttribute Is Nothing Then
Return Lily.ComponentFramework.ComponentManager.Permission.Has(Me.PermissionAttribute)
Else
Return True
End If
End Function
这个主要是判断当前窗体是否具有PermissionAttribute属性,如果没有,说明不需要进行权限控制
Private md_pa As PermissionAttribute
<Browsable(False)> Public ReadOnly Property PermissionAttribute() As PermissionAttribute
Get
If Me.md_pa Is Nothing Then
Dim objs As Object() = Me.GetType.GetCustomAttributes(GetType(Lily.ComponentFramework.PermissionAttribute), True)
If Not objs Is Nothing AndAlso objs.Length > 0 Then
Dim v As Lily.ComponentFramework.PermissionAttribute = CType(objs(0), Lily.ComponentFramework.PermissionAttribute)
If Not v.Name Is Nothing AndAlso v.Name.Length > 0 _
AndAlso Not v.Guid Is Nothing _
AndAlso v.Guid.Length > 0 Then
Me.md_pa = v
End If
End If
End If
Return Me.md_pa
End Get
End Property
<Browsable(False)> Public ReadOnly Property PermissionAttribute() As PermissionAttribute
Get
If Me.md_pa Is Nothing Then
Dim objs As Object() = Me.GetType.GetCustomAttributes(GetType(Lily.ComponentFramework.PermissionAttribute), True)
If Not objs Is Nothing AndAlso objs.Length > 0 Then
Dim v As Lily.ComponentFramework.PermissionAttribute = CType(objs(0), Lily.ComponentFramework.PermissionAttribute)
If Not v.Name Is Nothing AndAlso v.Name.Length > 0 _
AndAlso Not v.Guid Is Nothing _
AndAlso v.Guid.Length > 0 Then
Me.md_pa = v
End If
End If
End If
Return Me.md_pa
End Get
End Property
待续