I want to create role-based permissions for each form. How method simple can I apply?. please guide me.
If logged in as the owner then he can access to all forms while if logged in as an admin then the main form for the update button cannot be used and there is a messagebox that the user does not have access and also the admin can display form2 but button1 cannot be used and there is a messagebox that the user does not have access.
This is only a sample because the original project has many forms.
Thanks
Code in Form1 (Login)
Public Class Form1
Private uService As New UserService()
Private Sub BtnLogin_Click(sender As Object, e As EventArgs) Handles BtnLogin.Click
'add dapper logics here
Dim users = uService.GetDTOUsersByUsername(txtUsername.Text)
If users.Password = txtPassword.Text AndAlso users.Username = txtUsername.Text Then
MessageBox.Show("Successfull, Welcome " & users.RoleName)
Me.ShowInTaskbar = False
Me.Hide()
Call (New FrmMain()).ShowDialog()
Environment.Exit(0)
Else
MessageBox.Show("Invalid Login details")
End If
End Sub
End Class
Public Class UserService
Public Function GetOledbConnectionString() As String
Return "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\dapperdemo.accdb;Persist Security Info=False;"
End Function
Private ReadOnly _conn As OleDbConnection
Private _connectionString As String = GetOledbConnectionString()
Public Sub New()
_conn = New OleDbConnection(_connectionString)
End Sub
Public Function GetUserByUsername(ByVal Username As String) As Users
Dim sql = $"SELECT * FROM Users WHERE Username = '{Username}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of Users)(sql).FirstOrDefault()
End Using
End Function
Public Function GetUserByRolename(ByVal Rolename As String) As Role
Dim sql = $"SELECT RoleName FROM Users WHERE Username = '{Rolename}'"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of Role)(sql).FirstOrDefault()
End Using
End Function
Public Function GetDTOUsersByUsername(ByVal Username As String) As DTOUsers
Dim sql = $"SELECT * FROM Users INNER JOIN Role ON Users.Roleid = Role.Roleid"
Using _conn = New OleDbConnection(GetOledbConnectionString())
Return _conn.Query(Of DTOUsers)(sql).FirstOrDefault()
End Using
End Function
End Class
Public Class Users
Public Property Username() As String
Public Property Password() As String
Public Property RoleID() As Integer
End Class
Public Class Role
Public Property RoleID() As Integer
Public Property RoleName() As String
End Class
Public Class DTOUsers
Public Property Username() As String
Public Property Password() As String
Public Property RoleName() As String
End Class
Code in FormMain
Public Class FrmMain
Private Sub Button2BtnShowform2_Click(sender As Object, e As EventArgs) Handles BtnShowform2.Click
Dim frm As New Form2
frm.ShowDialog()
End Sub
End Class
Table : Users
Username | password | RoleID |
---|---|---|
TEST1 | TEST@123 | 1 |
TEST2 | TEST@1234 | 2 |
Table : Role
Roleid | RoleName |
---|---|
1 | Owner |
2 | Admin |
Form1(Login)
FrmMain
Form2