The employee management tool used by our fictional company is in sore need of a role-based security system. The employee RKing is a human resources administrator who should be allowed privileges only to change employee roles and manage his own information. However, in the current state of the employee management system (EMS), he can perform
Figure 2-1:
Employees and roles
As you can see in Figure 2-1, EmployeeDatabase.mdb contains three tables: the Employee table, Role table, and EmployeeRole table. You were introduced to the Employee table in Chapter 1. The Role table contains a list of roles: Employee , Manager , HR Administrator , HR Officer , and Auditor . The EmployeeRole table contains a list of employees and the roles they are assigned to. For example, RKing is in the role of both Employee and HR Administrator . Currently, these database entries don’t actually allow or prevent users from performing tasks.
Disable functionality based on roles
In this exercise, you’ll load the roles from the database and assign them to the logged-on user. Because
Run Visual Basic .NET, and
Add a new module named RoleBasedSecurity.vb and the following Imports statements to the top of the file:
Imports System.Security.Principal
Imports System.Threading
Imports System.Data.OleDb
Imports System.Collections.Specialized
Insert the following code after the Module RoleBasedSecurity statement and before the End Module statement:
Friend Function LoadRoles(ByVal UserName As String) As String()
Dim cn As New OleDbConnection(G_CONNECTIONSTRING)
Dim strSQL As String = _
"Select Role from EmployeeRole where Username =‘" & _
UserName & "‘"
Dim cmd As New OleDbCommand(strSQL, cn)
Dim dr As OleDbDataReader
Dim collRole As New StringCollection()
Dim strRole() As String
cn.Open()
dr = cmd.ExecuteReader
collRole.Clear()
While dr.Read
collRole.Add(CStr(dr("Role")))
End While
ReDim strRole(collRole.Count - 1)
collRole.CopyTo(strRole, 0)
Return strRole
End Function
This code loads the
Add the following function to the module RoleBasedSecurity after the LoadRoles function inserted in the previous step and before the End Module statement:
Friend Sub SetPrincipalPolicy(ByVal UserName As String)
Dim strUserRoles() As String = LoadRoles(UserName)
Dim UserIdentity As New GenericIdentity(UserName)
Dim UserPrincipal As GenericPrincipal
UserPrincipal = New GenericPrincipal(UserIdentity, strUserRoles)
AppDomain.CurrentDomain.SetPrincipalPolicy( _
PrincipalPolicy.UnauthenticatedPrincipal)
Thread.CurrentPrincipal = UserPrincipal
End Sub
This code loads the roles and
In clsEmployee.vb, add the following call to SetPrincipalPolicy near the end of the Create function before the Catch ex As Exception statement:
SetPrincipalPolicy(employee.m_Username)
Open the form frmDashboard, and double-click the form background to create a Form_Load event. Add the following code to the event:
Private Sub frmDashboard_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
With System.Threading.Thread.CurrentPrincipal
Me.btnAddNew.Visible = .IsInRole("HR Officer")
Me.lblAddNew.Visible = .IsInRole("HR Officer")
Me.btnRemove.Visible = .IsInRole("HR Officer")
Me.lblRemove.Visible = .IsInRole("HR Officer")
Me.btnManage.Visible = .IsInRole("HR Administrator")
Me.lblManage.Visible = .IsInRole("HR Administrator")
Me.btnMyInfo.Visible = .IsInRole("Employee")
Me.lblMyInfo.Visible = .IsInRole("Employee")
End With
End Sub
Press F5 to run the application, and log on to the employee management system using the username RKing and password RKing.
After you have logged on, the dashboard will look like Figure 2-2. The Add New Employee and Remove Employee
Figure 2-2:
Buttons are hidden based on roles
The previous exercise