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

@@ -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