Author:水如烟
界面:
在运行环境CAP目录下生成四个文件:
BillPermission.vb
BillPermissionAttribute.vb
BillPermissionFlags.vb
XMLUtil.vb(属公用)
内容分别如下:
BillPermission.vb
生成器下载,不含源代码:CPABuilder
界面:
在运行环境CAP目录下生成四个文件:
BillPermission.vb
BillPermissionAttribute.vb
BillPermissionFlags.vb
XMLUtil.vb(属公用)
内容分别如下:
BillPermission.vb
Imports System.Security
Imports System.Security.Permissions
Namespace LzmTW.uSystem.uSecurity
<Serializable()> _
Public NotInheritable Class BillPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
Private gFlags As BillPermissionFlags
Public Property Flags() As BillPermissionFlags
Get
Return Me.gFlags
End Get
Set(ByVal value As BillPermissionFlags)
VerifyFlags(value)
Me.gFlags = value
End Set
End Property
Sub New(ByVal state As PermissionState)
If state = PermissionState.Unrestricted Then
Me.SetUnrestricted(True)
Else
If (state <> PermissionState.None) Then
Throw New ArgumentException("state")
End If
Me.SetUnrestricted(False)
Me.Reset()
End If
End Sub
Sub New(ByVal flags As BillPermissionFlags)
VerifyFlags(flags)
Me.gFlags = flags
End Sub
Private Sub SetUnrestricted(ByVal unrestricted As Boolean)
If unrestricted Then
Me.gFlags = BillPermissionFlags.AllFlags
End If
End Sub
Private Sub Reset()
Me.gFlags = BillPermissionFlags.NoFlags
End Sub
Friend Function VerifyType(ByVal perm As CodeAccessPermission) As Boolean
If Not perm Is Nothing AndAlso perm.GetType Is MyBase.GetType Then
Return True
End If
Return False
End Function
Friend Shared Sub VerifyFlags(ByVal flags As BillPermissionFlags)
If ((flags And Not BillPermissionFlags.AllFlags) <> BillPermissionFlags.NoFlags) Then
Throw New ArgumentException("flags")
End If
End Sub
Public Function IsUnrestricted() As Boolean Implements IUnrestrictedPermission.IsUnrestricted
Return Me.gFlags = BillPermissionFlags.AllFlags
End Function
Public Overrides Function Copy() As IPermission
Return New BillPermission(Me.gFlags)
End Function
Public Overrides Sub FromXml(ByVal element As SecurityElement)
XMLUtil.ValidateElement(element)
If XMLUtil.IsUnrestricted(element) Then
Me.SetUnrestricted(True)
Else
Me.gFlags = BillPermissionFlags.NoFlags
Dim strFlags As String = element.Attribute("Flags")
If Not strFlags Is Nothing Then
Me.gFlags = DirectCast([Enum].Parse(GetType(BillPermissionFlags), strFlags), BillPermissionFlags)
End If
End If
End Sub
Public Overrides Function Intersect(ByVal target As IPermission) As IPermission
If target Is Nothing Then
Return Nothing
End If
If Not VerifyType(CType(target, CodeAccessPermission)) Then
Throw New ArgumentException("WrongType")
End If
Dim mPermission As BillPermission = DirectCast(target, BillPermission)
Dim mFlags As BillPermissionFlags = Me.gFlags And mPermission.Flags
If mFlags = BillPermissionFlags.NoFlags Then
Return Nothing
End If
Return New BillPermission(mFlags)
End Function
Public Overrides Function IsSubsetOf(ByVal target As IPermission) As Boolean
If target Is Nothing Then
Return Me.gFlags = BillPermissionFlags.NoFlags
End If
Dim mResult As Boolean = False
Try
Dim mPermission As BillPermission = DirectCast(target, BillPermission)
If mPermission.IsUnrestricted Then
Return True
End If
If Me.IsUnrestricted Then
Return False
End If
Dim mMaxInt As Integer = BillPermissionFlags.AllFlags + 1
Dim mNum As Integer = CInt(Math.Log(mMaxInt) / Math.Log(2))
Dim mValue As Integer
For i As Integer = 0 To mNum - 1
mValue = CInt(Math.Pow(2, i))
mResult = ((Me.gFlags And mValue) <= (mPermission.Flags And mValue))
If mResult = False Then Exit For
Next
Catch ex As InvalidCastException
Throw New ArgumentException("WrongType")
End Try
Return mResult
End Function
Public Overrides Function Union(ByVal target As IPermission) As IPermission
If target Is Nothing Then
Return Me.Copy
End If
If Not VerifyType(CType(target, CodeAccessPermission)) Then
Throw New ArgumentException("WrongType")
End If
Dim mPermission As BillPermission = DirectCast(target, BillPermission)
Return New BillPermission((Me.gFlags Or mPermission.Flags))
End Function
Public Overrides Function ToXml() As SecurityElement
Dim element As SecurityElement = XMLUtil.CreatePermissionElement(Me)
If Not Me.IsUnrestricted Then
If Me.gFlags <> BillPermissionFlags.NoFlags Then
element.AddAttribute("Flags", [Enum].GetName(GetType(BillPermissionFlags), Me.gFlags))
End If
Return element
End If
XMLUtil.AddUnrestrictedAttribute(element)
Return element
End Function
End Class
End Namespace
BillPermissionAttribute.vbImports System.Security.Permissions
Namespace LzmTW.uSystem.uSecurity
<Serializable()> _
Public NotInheritable Class BillPermission
Inherits CodeAccessPermission
Implements IUnrestrictedPermission
Private gFlags As BillPermissionFlags
Public Property Flags() As BillPermissionFlags
Get
Return Me.gFlags
End Get
Set(ByVal value As BillPermissionFlags)
VerifyFlags(value)
Me.gFlags = value
End Set
End Property
Sub New(ByVal state As PermissionState)
If state = PermissionState.Unrestricted Then
Me.SetUnrestricted(True)
Else
If (state <> PermissionState.None) Then
Throw New ArgumentException("state")
End If
Me.SetUnrestricted(False)
Me.Reset()
End If
End Sub
Sub New(ByVal flags As BillPermissionFlags)
VerifyFlags(flags)
Me.gFlags = flags
End Sub
Private Sub SetUnrestricted(ByVal unrestricted As Boolean)
If unrestricted Then
Me.gFlags = BillPermissionFlags.AllFlags
End If
End Sub
Private Sub Reset()
Me.gFlags = BillPermissionFlags.NoFlags
End Sub
Friend Function VerifyType(ByVal perm As CodeAccessPermission) As Boolean
If Not perm Is Nothing AndAlso perm.GetType Is MyBase.GetType Then
Return True
End If
Return False
End Function
Friend Shared Sub VerifyFlags(ByVal flags As BillPermissionFlags)
If ((flags And Not BillPermissionFlags.AllFlags) <> BillPermissionFlags.NoFlags) Then
Throw New ArgumentException("flags")
End If
End Sub
Public Function IsUnrestricted() As Boolean Implements IUnrestrictedPermission.IsUnrestricted
Return Me.gFlags = BillPermissionFlags.AllFlags
End Function
Public Overrides Function Copy() As IPermission
Return New BillPermission(Me.gFlags)
End Function
Public Overrides Sub FromXml(ByVal element As SecurityElement)
XMLUtil.ValidateElement(element)
If XMLUtil.IsUnrestricted(element) Then
Me.SetUnrestricted(True)
Else
Me.gFlags = BillPermissionFlags.NoFlags
Dim strFlags As String = element.Attribute("Flags")
If Not strFlags Is Nothing Then
Me.gFlags = DirectCast([Enum].Parse(GetType(BillPermissionFlags), strFlags), BillPermissionFlags)
End If
End If
End Sub
Public Overrides Function Intersect(ByVal target As IPermission) As IPermission
If target Is Nothing Then
Return Nothing
End If
If Not VerifyType(CType(target, CodeAccessPermission)) Then
Throw New ArgumentException("WrongType")
End If
Dim mPermission As BillPermission = DirectCast(target, BillPermission)
Dim mFlags As BillPermissionFlags = Me.gFlags And mPermission.Flags
If mFlags = BillPermissionFlags.NoFlags Then
Return Nothing
End If
Return New BillPermission(mFlags)
End Function
Public Overrides Function IsSubsetOf(ByVal target As IPermission) As Boolean
If target Is Nothing Then
Return Me.gFlags = BillPermissionFlags.NoFlags
End If
Dim mResult As Boolean = False
Try
Dim mPermission As BillPermission = DirectCast(target, BillPermission)
If mPermission.IsUnrestricted Then
Return True
End If
If Me.IsUnrestricted Then
Return False
End If
Dim mMaxInt As Integer = BillPermissionFlags.AllFlags + 1
Dim mNum As Integer = CInt(Math.Log(mMaxInt) / Math.Log(2))
Dim mValue As Integer
For i As Integer = 0 To mNum - 1
mValue = CInt(Math.Pow(2, i))
mResult = ((Me.gFlags And mValue) <= (mPermission.Flags And mValue))
If mResult = False Then Exit For
Next
Catch ex As InvalidCastException
Throw New ArgumentException("WrongType")
End Try
Return mResult
End Function
Public Overrides Function Union(ByVal target As IPermission) As IPermission
If target Is Nothing Then
Return Me.Copy
End If
If Not VerifyType(CType(target, CodeAccessPermission)) Then
Throw New ArgumentException("WrongType")
End If
Dim mPermission As BillPermission = DirectCast(target, BillPermission)
Return New BillPermission((Me.gFlags Or mPermission.Flags))
End Function
Public Overrides Function ToXml() As SecurityElement
Dim element As SecurityElement = XMLUtil.CreatePermissionElement(Me)
If Not Me.IsUnrestricted Then
If Me.gFlags <> BillPermissionFlags.NoFlags Then
element.AddAttribute("Flags", [Enum].GetName(GetType(BillPermissionFlags), Me.gFlags))
End If
Return element
End If
XMLUtil.AddUnrestrictedAttribute(element)
Return element
End Function
End Class
End Namespace
Imports System.Security
Imports System.Security.Permissions
Namespace LzmTW.uSystem.uSecurity
<Serializable()> _
<AttributeUsage( _
AttributeTargets.Method Or _
AttributeTargets.Constructor Or _
AttributeTargets.Struct Or _
AttributeTargets.Class Or _
AttributeTargets.Assembly, AllowMultiple:=True, Inherited:=False)> _
Public NotInheritable Class BillPermissionAttribute
Inherits CodeAccessSecurityAttribute
Private gFlags As BillPermissionFlags
Public Sub New(ByVal action As SecurityAction)
MyBase.New(action)
End Sub
Public Overrides Function CreatePermission() As System.Security.IPermission
If MyBase.Unrestricted Then
Return New BillPermission(PermissionState.Unrestricted)
End If
Return New BillPermission(Me.gFlags)
End Function
Public Property [New]() As Boolean
Get
Return GetValue(BillPermissionFlags.[New])
End Get
Set(ByVal value As Boolean)
SetValue(BillPermissionFlags.[New], value)
End Set
End Property
Public Property Edit() As Boolean
Get
Return GetValue(BillPermissionFlags.Edit)
End Get
Set(ByVal value As Boolean)
SetValue(BillPermissionFlags.Edit, value)
End Set
End Property
Public Property Delete() As Boolean
Get
Return GetValue(BillPermissionFlags.Delete)
End Get
Set(ByVal value As Boolean)
SetValue(BillPermissionFlags.Delete, value)
End Set
End Property
Private Function GetValue(ByVal flags As BillPermissionFlags) As Boolean
Return ((Me.gFlags And flags) <> BillPermissionFlags.NoFlags)
End Function
Private Sub SetValue(ByVal flags As BillPermissionFlags, ByVal value As Boolean)
If value = True Then
Me.gFlags = Me.gFlags Or flags
Else
Me.gFlags = Me.gFlags And Not flags
End If
End Sub
End Class
End Namespace
BillPermissionFlags.vbImports System.Security.Permissions
Namespace LzmTW.uSystem.uSecurity
<Serializable()> _
<AttributeUsage( _
AttributeTargets.Method Or _
AttributeTargets.Constructor Or _
AttributeTargets.Struct Or _
AttributeTargets.Class Or _
AttributeTargets.Assembly, AllowMultiple:=True, Inherited:=False)> _
Public NotInheritable Class BillPermissionAttribute
Inherits CodeAccessSecurityAttribute
Private gFlags As BillPermissionFlags
Public Sub New(ByVal action As SecurityAction)
MyBase.New(action)
End Sub
Public Overrides Function CreatePermission() As System.Security.IPermission
If MyBase.Unrestricted Then
Return New BillPermission(PermissionState.Unrestricted)
End If
Return New BillPermission(Me.gFlags)
End Function
Public Property [New]() As Boolean
Get
Return GetValue(BillPermissionFlags.[New])
End Get
Set(ByVal value As Boolean)
SetValue(BillPermissionFlags.[New], value)
End Set
End Property
Public Property Edit() As Boolean
Get
Return GetValue(BillPermissionFlags.Edit)
End Get
Set(ByVal value As Boolean)
SetValue(BillPermissionFlags.Edit, value)
End Set
End Property
Public Property Delete() As Boolean
Get
Return GetValue(BillPermissionFlags.Delete)
End Get
Set(ByVal value As Boolean)
SetValue(BillPermissionFlags.Delete, value)
End Set
End Property
Private Function GetValue(ByVal flags As BillPermissionFlags) As Boolean
Return ((Me.gFlags And flags) <> BillPermissionFlags.NoFlags)
End Function
Private Sub SetValue(ByVal flags As BillPermissionFlags, ByVal value As Boolean)
If value = True Then
Me.gFlags = Me.gFlags Or flags
Else
Me.gFlags = Me.gFlags And Not flags
End If
End Sub
End Class
End Namespace
Namespace LzmTW.uSystem.uSecurity
<Flags()> _
Public Enum BillPermissionFlags
NoFlags = 0
[New] = 1
Edit = 2
Delete = 4
AllFlags = 7
End Enum
End Namespace
XMLUtil.vb<Flags()> _
Public Enum BillPermissionFlags
NoFlags = 0
[New] = 1
Edit = 2
Delete = 4
AllFlags = 7
End Enum
End Namespace
Imports System.Security
Imports System.Security.Permissions
Namespace LzmTW.uSystem.uSecurity
Friend Class XMLUtil
Private Shared Function IsPermissionElement(ByVal element As SecurityElement) As Boolean
If Not element.Tag.Equals("Permission") AndAlso Not element.Tag.Equals("IPermission") Then
Return False
End If
Return True
End Function
Friend Shared Function CreatePermissionElement(ByVal perm As CodeAccessPermission, _
Optional ByVal permname As String = Nothing) As SecurityElement
Dim element As New SecurityElement("IPermission")
AddClassAttribute(element, perm.GetType, permname)
element.AddAttribute("version", "1")
Return element
End Function
Private Shared Sub AddClassAttribute(ByVal element As SecurityElement, ByVal type As Type, ByVal typename As String)
If typename Is Nothing Then
typename = type.FullName
End If
element.AddAttribute("class", String.Concat(typename, ", ", type.Module.Assembly.FullName.Replace(""""c, "'"c)))
End Sub
Friend Shared Sub AddUnrestrictedAttribute(ByVal element As SecurityElement)
element.AddAttribute("Unrestricted", "true")
End Sub
Friend Shared Sub ValidateElement(ByVal element As SecurityElement)
If element Is Nothing Then
Throw New ArgumentNullException("element")
End If
If Not IsPermissionElement(element) Then
Throw New ArgumentException("PermissionElement")
End If
Dim version As String = element.Attribute("version")
If Not version Is Nothing AndAlso Not version.Equals("1") Then
Throw New ArgumentException("version")
End If
End Sub
Friend Shared Function IsUnrestricted(ByVal element As SecurityElement) As Boolean
Dim Unrestricted As String = element.Attribute("Unrestricted")
If Unrestricted Is Nothing Then
Return False
End If
Return Unrestricted.Equals("true")
End Function
End Class
End Namespace
Imports System.Security.Permissions
Namespace LzmTW.uSystem.uSecurity
Friend Class XMLUtil
Private Shared Function IsPermissionElement(ByVal element As SecurityElement) As Boolean
If Not element.Tag.Equals("Permission") AndAlso Not element.Tag.Equals("IPermission") Then
Return False
End If
Return True
End Function
Friend Shared Function CreatePermissionElement(ByVal perm As CodeAccessPermission, _
Optional ByVal permname As String = Nothing) As SecurityElement
Dim element As New SecurityElement("IPermission")
AddClassAttribute(element, perm.GetType, permname)
element.AddAttribute("version", "1")
Return element
End Function
Private Shared Sub AddClassAttribute(ByVal element As SecurityElement, ByVal type As Type, ByVal typename As String)
If typename Is Nothing Then
typename = type.FullName
End If
element.AddAttribute("class", String.Concat(typename, ", ", type.Module.Assembly.FullName.Replace(""""c, "'"c)))
End Sub
Friend Shared Sub AddUnrestrictedAttribute(ByVal element As SecurityElement)
element.AddAttribute("Unrestricted", "true")
End Sub
Friend Shared Sub ValidateElement(ByVal element As SecurityElement)
If element Is Nothing Then
Throw New ArgumentNullException("element")
End If
If Not IsPermissionElement(element) Then
Throw New ArgumentException("PermissionElement")
End If
Dim version As String = element.Attribute("version")
If Not version Is Nothing AndAlso Not version.Equals("1") Then
Throw New ArgumentException("version")
End If
End Sub
Friend Shared Function IsUnrestricted(ByVal element As SecurityElement) As Boolean
Dim Unrestricted As String = element.Attribute("Unrestricted")
If Unrestricted Is Nothing Then
Return False
End If
Return Unrestricted.Equals("true")
End Function
End Class
End Namespace
生成器下载,不含源代码:CPABuilder