LDAP-Synchronisation

This commit is contained in:
2023-05-15 13:06:53 +02:00
parent fa193fcfbd
commit 0f491590af
3 changed files with 125 additions and 110 deletions

View File

@@ -25,14 +25,9 @@ Public Class frmADsearch
dt.Columns.Add("sAMAccountName", GetType(String))
dt.Columns.Add("givenName", GetType(String))
dt.Columns.Add("sn", GetType(String))
dt.Columns.Add("DisplayName", GetType(String))
dt.Columns.Add("distinguishedName", GetType(String))
dt.Columns.Add("department", GetType(String))
dt.Columns.Add("company", GetType(String))
dt.Columns.Add("mail", GetType(String))
dt.Columns.Add("cn", GetType(String))
dt.Columns.Add("telephoneNumber", GetType(String))
dt.Columns.Add("extensionAttribute7", GetType(String))
dt.Columns.Add("adEntry", GetType(System.DirectoryServices.DirectoryEntry))
End Sub
@@ -50,7 +45,6 @@ Public Class frmADsearch
.Columns("GivenName").Width = 75
.Columns("sn").Width = 100
.Columns("sn").HeaderText = "Lastname"
.Columns("DisplayName").Visible = False
.Columns("sAMAccountName").Width = 75
.Columns("sAMAccountName").HeaderText = "Account"
.Columns("department").Width = 75
@@ -59,9 +53,7 @@ Public Class frmADsearch
.Columns("department").HeaderText = "Department"
.Columns("company").Width = 75
.Columns("company").HeaderText = "Company"
.Columns("telephoneNumber").Visible = False
.Columns("extensionAttribute7").Visible = False
.Columns("cn").Visible = False
.Columns("adEntry").Visible = False
End With
@@ -80,9 +72,8 @@ Public Class frmADsearch
Try
dirEntry = New System.DirectoryServices.DirectoryEntry(LDAPString)
dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)
dirSearcher.Filter = "(samAccountName=" & username & ")"
dirSearcher.PropertiesToLoad.Add("GivenName")
dirSearcher.PropertiesToLoad.Add("sn")
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"
@@ -95,19 +86,13 @@ Public Class frmADsearch
Dim de As System.DirectoryServices.DirectoryEntry
de = srEntry.GetDirectoryEntry()
'Dim ObjFirstName As String = ""
'Dim ObjLastName As String = String.Empty
Dim R As DataRow = dt.NewRow
R("givenName") = de.Properties("givenName").Value.ToString()
R("sAMAccountName") = de.Properties("sAMAccountName").Value.ToString()
R("department") = de.Properties("department").Value.ToString()
R("company") = de.Properties("company").Value.ToString()
R("sn") = de.Properties("sn").Value.ToString()
R("cn") = de.Properties("cn").Value.ToString()
R("telephoneNumber") = de.Properties("telephoneNumber").Value.ToString()
R("distinguishedName") = de.Properties("distinguishedName").Value.ToString()
R("extensionAttribute7") = de.Properties("extensionAttribute7").Value.ToString()
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)
@@ -162,23 +147,30 @@ Public Class frmADsearch
For Each row As DataGridViewRow In dgvAD.SelectedRows
activeDirectoryObj = New ADObject With {
.sn = IIf(IsDBNull(row.Cells("sn").Value), "", row.Cells("sn").Value),
.sAMAccountName = IIf(IsDBNull(row.Cells("sAMAccountName").Value), "", row.Cells("sAMAccountName").Value),
.givenName = IIf(IsDBNull(row.Cells("givenName").Value), "", row.Cells("givenName").Value),
.cn = IIf(IsDBNull(row.Cells("cn").Value), "", row.Cells("cn").Value),
.department = IIf(IsDBNull(row.Cells("department").Value), "", row.Cells("department").Value),
.company = IIf(IsDBNull(row.Cells("company").Value), "", row.Cells("company").Value),
.telephoneNumber = IIf(IsDBNull(row.Cells("telephoneNumber").Value), "", row.Cells("telephoneNumber").Value),
.distinguishedName = IIf(IsDBNull(row.Cells("distinguishedName").Value), "", row.Cells("distinguishedName").Value),
.mail = IIf(IsDBNull(row.Cells("mail").Value), "", row.Cells("mail").Value),
.domain = cboDomain.Text,
.emailExtension = IIf(IsDBNull(row.Cells("extensionAttribute7").Value), "", row.Cells("extensionAttribute7").Value),
.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
@@ -186,17 +178,7 @@ Public Class ADObject
Public Sub New()
End Sub
Public Property sn As String
Public Property givenName As String
Public Property cn As String
Public Property department As String
Public Property company As String
Public Property telephoneNumber As String
Public Property distinguishedName As String
Public Property mail As String
Public Property sAMAccountName As String
Public Property domain As String
Public Property emailExtension As String
Public Property ADEntry As System.DirectoryServices.DirectoryEntry
End Class

View File

@@ -71,6 +71,9 @@ Partial Class frmMitarbDetails
Me.Label59 = New System.Windows.Forms.Label()
Me.Label4 = New System.Windows.Forms.Label()
Me.GroupBox2 = New System.Windows.Forms.GroupBox()
Me.btnUpdateAD = New System.Windows.Forms.Button()
Me.txtEmailExtension = New System.Windows.Forms.TextBox()
Me.lblEmailExtension = New System.Windows.Forms.Label()
Me.txtAbweichenderAnzeigename = New System.Windows.Forms.TextBox()
Me.Label57 = New System.Windows.Forms.Label()
Me.cboFaktGrp = New VERAG_PROG_ALLGEMEIN.MyComboBox()
@@ -184,9 +187,6 @@ Partial Class frmMitarbDetails
Me.PictureBox6 = New System.Windows.Forms.PictureBox()
Me.cboAdminBer = New System.Windows.Forms.ComboBox()
Me.Label7 = New System.Windows.Forms.Label()
Me.txtEmailExtension = New System.Windows.Forms.TextBox()
Me.lblEmailExtension = New System.Windows.Forms.Label()
Me.btnUpdateAD = New System.Windows.Forms.Button()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.Panel1.SuspendLayout()
Me.TabControl2.SuspendLayout()
@@ -798,6 +798,36 @@ Partial Class frmMitarbDetails
Me.GroupBox2.TabStop = False
Me.GroupBox2.Text = "Firmendaten"
'
'btnUpdateAD
'
Me.btnUpdateAD.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.btnUpdateAD.BackColor = System.Drawing.Color.White
Me.btnUpdateAD.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnUpdateAD.Image = Global.ADMIN.My.Resources.Resources.refresh
Me.btnUpdateAD.Location = New System.Drawing.Point(669, 37)
Me.btnUpdateAD.Name = "btnUpdateAD"
Me.btnUpdateAD.Size = New System.Drawing.Size(27, 20)
Me.btnUpdateAD.TabIndex = 94
Me.btnUpdateAD.UseVisualStyleBackColor = False
'
'txtEmailExtension
'
Me.txtEmailExtension.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtEmailExtension.Location = New System.Drawing.Point(402, 204)
Me.txtEmailExtension.Name = "txtEmailExtension"
Me.txtEmailExtension.Size = New System.Drawing.Size(143, 20)
Me.txtEmailExtension.TabIndex = 111
'
'lblEmailExtension
'
Me.lblEmailExtension.AutoSize = True
Me.lblEmailExtension.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblEmailExtension.Location = New System.Drawing.Point(399, 188)
Me.lblEmailExtension.Name = "lblEmailExtension"
Me.lblEmailExtension.Size = New System.Drawing.Size(88, 13)
Me.lblEmailExtension.TabIndex = 112
Me.lblEmailExtension.Text = "E-Mail-Extension:"
'
'txtAbweichenderAnzeigename
'
Me.txtAbweichenderAnzeigename.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
@@ -2027,36 +2057,6 @@ Partial Class frmMitarbDetails
Me.Label7.TabIndex = 78
Me.Label7.Text = "Berechtigung:"
'
'txtEmailExtension
'
Me.txtEmailExtension.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.txtEmailExtension.Location = New System.Drawing.Point(402, 204)
Me.txtEmailExtension.Name = "txtEmailExtension"
Me.txtEmailExtension.Size = New System.Drawing.Size(143, 20)
Me.txtEmailExtension.TabIndex = 111
'
'lblEmailExtension
'
Me.lblEmailExtension.AutoSize = True
Me.lblEmailExtension.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblEmailExtension.Location = New System.Drawing.Point(399, 188)
Me.lblEmailExtension.Name = "lblEmailExtension"
Me.lblEmailExtension.Size = New System.Drawing.Size(88, 13)
Me.lblEmailExtension.TabIndex = 112
Me.lblEmailExtension.Text = "E-Mail-Extension:"
'
'btnUpdateAD
'
Me.btnUpdateAD.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
Me.btnUpdateAD.BackColor = System.Drawing.Color.White
Me.btnUpdateAD.FlatStyle = System.Windows.Forms.FlatStyle.Flat
Me.btnUpdateAD.Image = Global.ADMIN.My.Resources.Resources.refresh
Me.btnUpdateAD.Location = New System.Drawing.Point(669, 37)
Me.btnUpdateAD.Name = "btnUpdateAD"
Me.btnUpdateAD.Size = New System.Drawing.Size(27, 20)
Me.btnUpdateAD.TabIndex = 94
Me.btnUpdateAD.UseVisualStyleBackColor = False
'
'frmMitarbDetails
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)

View File

@@ -7,8 +7,11 @@ Imports System.Net.WebRequestMethods
Imports System.Security.Policy
Imports System.Text
Imports System.Windows
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel
Imports DAKOSY_Worker.DEERRE
Imports GrapeCity.ActiveReports.ReportsCore.Tools
Imports GrapeCity.Enterprise.Data.Expressions.Evaluation
Imports Microsoft.VisualBasic.ApplicationServices
Imports Newtonsoft.Json
Imports VERAG_PROG_ALLGEMEIN
@@ -887,23 +890,23 @@ Public Class frmMitarbDetails
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles btnADladen.Click
Dim frmAD = New frmADsearch()
If frmAD.ShowDialog = DialogResult.OK Then
txtEmail.Text = ""
txtUsername.Text = ""
txtVname.Text = ""
txtNname.Text = ""
txtDurchwahl.Text = ""
txtALIASUser.Text = ""
txtEmailExtension.Text = ""
txtEmail.Text = frmAD.activeDirectoryObj.mail
txtUsername.Text = frmAD.activeDirectoryObj.sAMAccountName
txtVname.Text = frmAD.activeDirectoryObj.givenName
txtNname.Text = frmAD.activeDirectoryObj.sn
txtDurchwahl.Text = frmAD.activeDirectoryObj.telephoneNumber
txtALIASUser.Text = frmAD.activeDirectoryObj.sAMAccountName
txtEmailExtension.Text = frmAD.activeDirectoryObj.emailextension
txtEmail.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("mail").Value)
txtUsername.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("sAMAccountName").Value)
txtVname.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("givenName").Value)
txtNname.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("sn").Value)
txtStrasse.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("streetAdress").Value)
txtPlz.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("postalCode").Value)
txtOrt.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("I").Value)
txtMobiltel.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("Mobile").Value)
txtDurchwahl.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("telephoneNumber").Value)
txtALIASUser.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("sAMAccountName").Value)
txtEmailExtension.Text = frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("extensionAttribute7").Value)
cboALIASDomain.changeItem(frmAD.activeDirectoryObj.domain)
cboAbteilung.changeItem(frmAD.activeDirectoryObj.department)
cboNiederlassung.changeItem(frmAD.activeDirectoryObj.company)
cboAbteilung.changeItem(frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("department").Value))
cboNiederlassung.changeItem(frmAD.setNoNullableValue(frmAD.activeDirectoryObj.ADEntry.Properties("company").Value))
End If
End Sub
@@ -916,8 +919,6 @@ Public Class frmMitarbDetails
Dim a As MsgBoxResult = MsgBox("Die Daten des Mitarbeiters mit dem AD synchronisieren?", vbYesNo)
If a = vbYes Then
Dim domainName As String = ""
Dim ldap As String = ""
Select Case cboALIASDomain._value
@@ -935,18 +936,22 @@ Public Class frmMitarbDetails
Try
dirEntry = New System.DirectoryServices.DirectoryEntry(ldap)
dirSearcher = New System.DirectoryServices.DirectorySearcher(dirEntry)
dirSearcher.Filter = "(samAccountName=" & txtALIASUser.Text & ")"
dirSearcher.PropertiesToLoad.Add("GivenName")
dirSearcher.PropertiesToLoad.Add("sn")
dirSearcher.Filter = " (&(objectClass=user)(objectCategory=person)(|(samAccountName=*" & txtALIASUser.Text & "*)))"
Dim sr As DirectoryServices.SearchResult = dirSearcher.FindOne()
If sr Is Nothing Then
Dim b As MsgBoxResult = MsgBox("Mitarbeiterdaten sind im AD nicht vorhanden?" & vbNewLine & "Solle der Mitarbeiter " & txtALIASUser.Text & " in der Domain " & cboALIASDomain.Text & " angelegt werden?", vbYesNo)
Dim b As MsgBoxResult = MsgBox("User ist im AD nicht vorhanden?" & vbNewLine & "Solle der Mitarbeiter " & txtALIASUser.Text & " in der Domain " & cboALIASDomain.Text & " angelegt werden?", vbYesNo)
If b = vbYes Then
Dim frmAD = New frmADsearch("Referenzuser (Gruppen werden von diesem User übernommen)!")
Dim frmAD = New frmADsearch("Referenzuser (Gruppenberechtigungen werden von diesem User übernommen)!")
If frmAD.ShowDialog = DialogResult.OK Then
'frmAD.activeDirectoryObj.ADEntry.CopyTo(New DirectoryEntry(frmAD.activeDirectoryObj.ADEntry.Parent.Path, txtALIASUser.Text, txtPwd.Text))
MsgBox("The Lightweight Directory Access Protocol (LDAP) provider does not CURRENTLY support this method!")
'Sobald diese Funktion unterstützt wird, kann nachfolgende Funktion auskommentiert werden
'https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices.directoryentry.copyto
'Dim NewADAccount As DirectoryEntry = New DirectoryEntry(frmAD.activeDirectoryObj.ADEntry.Parent.Path, txtALIASUser.Text, txtPwd.Text)
'frmAD.activeDirectoryObj.ADEntry.CopyTo(NewADAccount)
End If
Else
@@ -957,13 +962,28 @@ Public Class frmMitarbDetails
Dim de As System.DirectoryServices.DirectoryEntry = sr.GetDirectoryEntry()
de.Properties("givenName").Value = txtVname.Text
'de.Properties("sAMAccountName").Value.ToString()
de.Properties("department").Value = cboAbteilung._value
de.Properties("company").Value = cboFirma.SelectedValue
de.Properties("sn").Value = txtNname.Text
'de.Properties("extensionAttribute7").Value = txtEmailExtension.Text
de.Properties("mail").Value = txtEmail.Text
If de.SchemaClassName <> "user" Then
MsgBox("hinterlegter AD-User ist nicht vom Typ USER")
Exit Sub
End If
If (isFilled(de.Properties("givenName").Value)) Then de.Properties("givenName").Value = txtVname.Text
If (isFilled(de.Properties("department").Value)) Then de.Properties("department").Value = cboAbteilung._value
If (isFilled(de.Properties("company").Value)) Then de.Properties("company").Value = cboFirma.SelectedValue
If (isFilled(de.Properties("sn").Value)) Then de.Properties("sn").Value = txtNname.Text
If (isFilled(de.Properties("mail").Value)) Then de.Properties("mail").Value = txtEmail.Text
If cbxGekuendigt.Checked Then
Dim expire As DateTime = datGekuendigtAm.Value
expire = expire.AddDays(1)
de.Properties("accountExpires").Value = expire.ToFileTime.ToString
End If
'If (isFilled(de.Properties("streetAdress").Value)) Then de.Properties("streetAdress").Value = txtStrasse.Text
'If (isFilled(de.Properties("postalCode").Value)) Then de.Properties("postalCode").Value = txtPlz.Text
'If (isFilled(de.Properties("I").Value)) Then de.Properties("I").Value = txtOrt.Text
'If (isFilled(de.Properties("Mobile").Value)) Then de.Properties("Mobile").Value = txtMobiltel.Text
de.CommitChanges()
Catch ex As Exception
@@ -973,13 +993,26 @@ Public Class frmMitarbDetails
End Sub
Private Function setNoNullableValue(value As String) As String
Private Sub txtALIASUser_TextChanged(sender As Object, e As EventArgs) Handles txtALIASUser.TextChanged, cboALIASDomain.TextChanged
If txtALIASUser.Text <> "" AndAlso cboALIASDomain._value <> "" Then
btnUpdateAD.Enabled = True
Else
btnUpdateAD.Enabled = False
End If
End Sub
Public Function isFilled(value As String) As Boolean
If value IsNot Nothing Then
If value <> "" Then
Return value
Return False ' Werte aus AD sollen nur gesetzt, nicht überschr
Else
Return True
End If
End If
Return True
End Function