Remove/Unsubscribe 从基础 Class 派生 Class 加载事件
Remove/Unsubscribe Derived Class Load event from Base Class
我有一个基础 class 检查用户是否有权访问派生表单。如果用户无权访问表单,我需要 block/remove invoke/unsubscribe 派生 class 中的所有 methods/events 来自基础 class。
我尝试了很多方法,但找不到解决这个问题的方法。
我的方法是在基本表单加载事件中关闭表单,但是如果在派生表单加载事件中加载数据,它也会在数据加载后引发和关闭。这可能是一些安全问题的泄漏。
我可以通过向派生形式添加一些代码来轻松解决这个问题,但我有大约 450~500 个关于派生形式的代码。
或者我可以定义一个函数来显示表单,在显示之前检查表单中的用户角色,但由于有许多派生表单,我无法更改。
Public Class AuthBaseForm
Inherits DevExpress.XtraEditors.XtraForm
Property IsAuthorized As Boolean = False
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'StartUpForm
'
Me.ClientSize = New System.Drawing.Size(825, 432)
Me.Name = "AuthBaseForm"
Me.AutoScaleMode = Windows.Forms.AutoScaleMode.Dpi
Me.ResumeLayout(False)
End Sub
Public Sub New()
MyBase.New()
' Here checks if user can access and visible this form
' function return true/false
IsAuthorized = GetUserRoleInForm(Me)
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsAuthorized Then
'Remove Here Derived Class Load Event
'Here, i need to find Derived class load event, and disable to invoke that method
MsgBox("You do not have permissions to show this form!", vbExclamation + vbOKOnly)
Me.Close()
Return
End If
End Sub
End Class
Public Class DetailForm
Inherits AuthBaseForm
Public Sub New()
' This call is required by the designer.
InitializeComponent()
End Sub
Private Sub DetailForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' this generated to test form role auth
MsgBox("If also my base class not authorized, this event is raising :(", vbInformation)
Me.KeyPreview = True
' Fetch data to show
Me.WaitHelper1.SetListingFunction(Sub() Me.GetData())
' Set auto filter rows
rh.devexFunc.SetGridViewFilterTypes(Me.viewSiparisler)
' Set column bolds
rh.devexFunc.SetGridViewFontBold(Me.viewSiparisler, "UNVAN")
End Sub
End Class
我尝试了一些作品,但仍然没有成功。
根据 Hans Passants 的解决方案,我修改了如下的基本 onLoad 方法并完美运行。
Protected Overrides Sub OnLoad(e As EventArgs)
If Me.IsAuthorized Then
MyBase.OnLoad(e)
Else
Me.Close()
rh.ExclamationMsgBox("You are not allowed to show this form!")
End If
End Sub
我有一个基础 class 检查用户是否有权访问派生表单。如果用户无权访问表单,我需要 block/remove invoke/unsubscribe 派生 class 中的所有 methods/events 来自基础 class。
我尝试了很多方法,但找不到解决这个问题的方法。
我的方法是在基本表单加载事件中关闭表单,但是如果在派生表单加载事件中加载数据,它也会在数据加载后引发和关闭。这可能是一些安全问题的泄漏。
我可以通过向派生形式添加一些代码来轻松解决这个问题,但我有大约 450~500 个关于派生形式的代码。
或者我可以定义一个函数来显示表单,在显示之前检查表单中的用户角色,但由于有许多派生表单,我无法更改。
Public Class AuthBaseForm
Inherits DevExpress.XtraEditors.XtraForm
Property IsAuthorized As Boolean = False
Private Sub InitializeComponent()
Me.SuspendLayout()
'
'StartUpForm
'
Me.ClientSize = New System.Drawing.Size(825, 432)
Me.Name = "AuthBaseForm"
Me.AutoScaleMode = Windows.Forms.AutoScaleMode.Dpi
Me.ResumeLayout(False)
End Sub
Public Sub New()
MyBase.New()
' Here checks if user can access and visible this form
' function return true/false
IsAuthorized = GetUserRoleInForm(Me)
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsAuthorized Then
'Remove Here Derived Class Load Event
'Here, i need to find Derived class load event, and disable to invoke that method
MsgBox("You do not have permissions to show this form!", vbExclamation + vbOKOnly)
Me.Close()
Return
End If
End Sub
End Class
Public Class DetailForm
Inherits AuthBaseForm
Public Sub New()
' This call is required by the designer.
InitializeComponent()
End Sub
Private Sub DetailForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' this generated to test form role auth
MsgBox("If also my base class not authorized, this event is raising :(", vbInformation)
Me.KeyPreview = True
' Fetch data to show
Me.WaitHelper1.SetListingFunction(Sub() Me.GetData())
' Set auto filter rows
rh.devexFunc.SetGridViewFilterTypes(Me.viewSiparisler)
' Set column bolds
rh.devexFunc.SetGridViewFontBold(Me.viewSiparisler, "UNVAN")
End Sub
End Class
我尝试了一些作品,但仍然没有成功。
根据 Hans Passants 的解决方案,我修改了如下的基本 onLoad 方法并完美运行。
Protected Overrides Sub OnLoad(e As EventArgs)
If Me.IsAuthorized Then
MyBase.OnLoad(e)
Else
Me.Close()
rh.ExclamationMsgBox("You are not allowed to show this form!")
End If
End Sub