Imports Org.BouncyCastle.Asn1
Imports VERAG_PROG_ALLGEMEIN

Public Class frmADsearch
    Dim dt As New DataTable()
    Public activeDirectoryObj = New ADObject()

    Public Sub New()
        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()
    End Sub

    Public Sub New(changelabel As String)

        ' Dieser Aufruf ist für den Designer erforderlich.
        InitializeComponent()

        lblUser.Text = changelabel

    End Sub


    Sub initDataTable()

        dt.Columns.Add("sAMAccountName", GetType(String))
        dt.Columns.Add("givenName", GetType(String))
        dt.Columns.Add("sn", GetType(String))
        dt.Columns.Add("distinguishedName", GetType(String))
        dt.Columns.Add("department", GetType(String))
        dt.Columns.Add("company", GetType(String))
        dt.Columns.Add("adEntry", GetType(System.DirectoryServices.DirectoryEntry))

    End Sub

    Sub initDGV(dt As DataTable)

        dgvAD.DataSource = dt


        If dgvAD.Columns.Count > 0 Then
            With dgvAD
                .RowHeadersWidth = 10

                .Columns("GivenName").HeaderText = "Firstname"
                .Columns("GivenName").Width = 75
                .Columns("sn").Width = 100
                .Columns("sn").HeaderText = "Lastname"
                .Columns("sAMAccountName").Width = 75
                .Columns("sAMAccountName").HeaderText = "Account"
                .Columns("department").Width = 75
                .Columns("distinguishedName").HeaderText = "DN-Name"
                .Columns("distinguishedName").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
                .Columns("department").HeaderText = "Department"
                .Columns("company").Width = 75
                .Columns("company").HeaderText = "Company"
                .Columns("adEntry").Visible = False

            End With

        End If


    End Sub


    Private Sub GetActiveDirUserDetails(ByVal username As String, Optional LDAPString As String = "LDAP://DC=VERAG,DC=OST,DC=DMN")

        dt.Clear()

        Dim dirEntry As System.DirectoryServices.DirectoryEntry
        Dim dirSearcher As System.DirectoryServices.DirectorySearcher
        Try
            dirEntry = New System.DirectoryServices.DirectoryEntry(LDAPString)
            dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)
            dirSearcher.Filter = " (&(objectClass=user)(objectCategory=person)(|(samAccountName=*" & username & "*)))"

            Dim sr As DirectoryServices.SearchResultCollection = dirSearcher.FindAll()
            If sr Is Nothing Then
                lblHint.Text = "No Entries found"
                Exit Sub
            ElseIf sr.Count = 0 Then
                lblHint.Text = sr.Count & " Entries found"
            End If

            For Each srEntry As DirectoryServices.SearchResult In sr
                Dim de As System.DirectoryServices.DirectoryEntry
                de = srEntry.GetDirectoryEntry()

                Dim R As DataRow = dt.NewRow
                R("givenName") = setNoNullableValue(de.Properties("givenName").Value)
                R("sn") = setNoNullableValue(de.Properties("sn").Value)
                R("sAMAccountName") = setNoNullableValue(de.Properties("sAMAccountName").Value)
                R("department") = setNoNullableValue(de.Properties("department").Value)
                R("distinguishedName") = setNoNullableValue(de.Properties("distinguishedName").Value)
                R("company") = setNoNullableValue(de.Properties("company").Value)
                R("adEntry") = de
                dt.Rows.Add(R)

            Next

        Catch ex As Exception
            lblHint.Text = ex.Message
        End Try
    End Sub

    Public Shared Function GetUsersInGroup(ByVal groupname As String, Optional ByVal LDAP As String = "LDAP://DC=VERAG,DC=OST,DC=DMN") As List(Of String)
        Dim Userlist As New List(Of String)

        Dim locDirectoryEntry As New DirectoryServices.DirectoryEntry(LDAP)
        Dim Searcher As New DirectoryServices.DirectorySearcher(locDirectoryEntry, "sAMAccountName=" & groupname)
        Dim Result As DirectoryServices.SearchResult = Searcher.FindOne

        If Result IsNot Nothing Then
            For Each User In Result.Properties("Member")
                Userlist.Add(User)
            Next
        End If
        Return Userlist
    End Function

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click

        lblHint.Text = ""

        If cboDomain._value = "" Then
            lblHint.Text = "Domain auswählen"
            Exit Sub
        End If

        If txtUser.Text = "" Then
            lblHint.Text = "User eingeben!"
            Exit Sub
        End If
        GetActiveDirUserDetails(txtUser.Text)


    End Sub

    Private Sub frmADsearch_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        cboDomain.Items.Add(New VERAG_PROG_ALLGEMEIN.MyListItem("VERAGOST", "LDAP://DC=VERAG,DC=OST,DC=DMN"))
        cboDomain.changeItem("verag.ost.dmn")
        initDataTable()
        initDGV(dt)
    End Sub

    Private Sub btnUebernehmen_Click(sender As Object, e As EventArgs) Handles btnUebernehmen.Click

        For Each row As DataGridViewRow In dgvAD.SelectedRows
            activeDirectoryObj = New ADObject With {
                .domain = cboDomain.Text,
                .ADEntry = IIf(IsDBNull(row.Cells("adEntry").Value), "", row.Cells("adEntry").Value)
            }

        Next

    End Sub

    Public Function setNoNullableValue(value As String) As String

        If value IsNot Nothing Then
            If value <> "" Then
                Return value
            End If
        End If
        Return ""

    End Function

    Private Sub txtUser_KeyDown(sender As Object, e As KeyEventArgs) Handles txtUser.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnSearch_Click(sender, e)
        End If
    End Sub
End Class

Public Class ADObject

    Public Sub New()
    End Sub

    Public Property domain As String
    Public Property ADEntry As System.DirectoryServices.DirectoryEntry

End Class