Minggu, 26 Desember 2010

menu database



Tabel RoleDetails berisi data rinci tentang hak akses untuk role tertentu



Kolom FormName yang terdapat di tabel FormActions berisi data mengenai nama-nama form yang terdapat di dalam aplikasi tersebut dan Actn merupakan kolom yang berisi data mengenai nama-nama control button yang terdapat di dalam form tersebut.



Hasilnya



Imports System.Data.SqlClient

Module modCommon

    Public sqlConn As New SqlClient.SqlConnection(My.Settings.SQLConnection)
    Public strUserName, strRoleID As String


    Public Function GetLeftRightItemFromControl(ByVal Teks As String) As String()
        '//cari item kiri dari teks
        Dim intPanjangString As Integer
        Dim intPosisiDash As Integer
        intPanjangString = Strings.Len(Teks)
        intPosisiDash = Teks.IndexOf(">")
        Dim strID As String = Trim(Strings.Left(Teks, intPosisiDash - 1))
        Dim strNama As String = Trim(Strings.Mid(Teks, intPosisiDash + 2, intPanjangString - intPosisiDash + 2))
        Dim arrString As String() = {strID, strNama}
        Return arrString
    End Function

    Private Function GetActionButton(ByVal RoleID As String, ByVal FormName As String) As List(Of String)
        Dim listOfString As New List(Of String)
        Using sqlCmd As New SqlClient.SqlCommand
            sqlCmd.Connection = modCommon.sqlConn
            sqlCmd.CommandType = CommandType.Text
            sqlCmd.CommandText = String.Format("Select * From RoleDetails Where RoleID = '{0}' And FormAction = '{1}' and Tag = 'action'", RoleID, FormName)
            modCommon.OpenConnection()
            Using sqlDr As SqlClient.SqlDataReader = sqlCmd.ExecuteReader
                If sqlDr.HasRows Then
                    While sqlDr.Read
                        listOfString.Add(sqlDr("MenuName").ToString.Trim)
                    End While
                End If
                sqlDr.Close()
            End Using
            modCommon.CloseConnection()
        End Using
        Return listOfString
    End Function

    Public Sub GetButtons(ByVal YourForm As Form)
        Dim listOfString As List(Of String) = modCommon.GetActionButton(modCommon.strRoleID, YourForm.Tag.ToString)
        If listOfString.Count > 0 Then
            For Each aControl As Control In YourForm.Controls
                modCommon.GetEnableButton(aControl, listOfString)
            Next
        Else
            For Each aControl As Control In YourForm.Controls
                modCommon.GetEnableButton(aControl)
            Next
        End If
    End Sub

    Private Sub GetEnableButton(ByVal YourControl As Control)
        '//cek dulu apakah control yang bersangkutan merupakan container control
        '//atau bukan (disini digunakan recursive method)
        If YourControl.HasChildren Then
            For Each aChildControl As Control In YourControl.Controls
                GetEnableButton(aChildControl)
            Next
        Else
            If TypeOf YourControl Is Button Then
                YourControl.Enabled = False
            End If
        End If
    End Sub

    Private Sub GetEnableButton(ByVal YourControl As Control, ByVal YourListOfString As List(Of String))
        '//cek dulu apakah control yang bersangkutan merupakan container control
        '//atau bukan (disini digunakan recursive method)
        If YourControl.HasChildren Then
            For Each aChildControl As Control In YourControl.Controls
                GetEnableButton(aChildControl, YourListOfString)
            Next
        Else
            If TypeOf YourControl Is Button Then
                If YourListOfString.Contains(YourControl.Text) Then
                    YourControl.Enabled = True
                Else
                    YourControl.Enabled = False
                End If
            End If
        End If
    End Sub

    Public Sub OpenConnection()
        If sqlConn.State <> ConnectionState.Open Then sqlConn.Open()
    End Sub

    Public Sub CloseConnection()
        If sqlConn.State = ConnectionState.Open Then sqlConn.Close()
    End Sub

End Module

Tidak ada komentar:

Posting Komentar