我有一個基類,用于檢查用戶是否有權訪問派生表單。如果用戶無權訪問表單,我需要阻止/洗掉對基類派生類中的所有方法/事件的呼叫/取消訂閱。
我嘗試了很多東西,但找不到處理這個問題的方法。
我的方法是在基本表單加載事件中關閉表單,但是如果在派生表單加載事件中加載資料,它也會在資料加載后引發和關閉。這可能會泄露一些安全問題。
我可以通過向派生形式添加一些代碼來輕松解決這個問題,但我有大約 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
我已經嘗試了一些作品,但仍然沒有成功。
uj5u.com熱心網友回復:
通過 Hans Passants 的解決方案,我修改了如下的基礎 onl oad 方法并且運行良好。
Protected Overrides Sub onl oad(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
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/361425.html
上一篇:如何從另一種形式訪問方法
下一篇:養表事件
