Modules/ADSyncTest/Form1.vb
Jonathan Jenne 9010ad4139 Much stuff
2019-04-04 16:29:18 +02:00

86 lines
3.3 KiB
VB.net

Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Interfaces.ActiveDirectoryInterface
Imports DigitalData.Modules.Database
Public Class Form1
Private _sync As ActiveDirectoryInterface
Private _logConfig As LogConfig
Private _logger As Logger
Private _firebird As Firebird
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_logConfig = New LogConfig(LogConfig.PathType.CurrentDirectory)
_logConfig.Debug = True
_logger = _logConfig.GetLogger()
_firebird = New Firebird(_logConfig, "172.24.12.41", "172.24.12.41:E:\DB\Firebird\Databases\DD_ICM.fdb", "sysdba", "dd")
_sync = New ActiveDirectoryInterface(_logConfig, _firebird)
_sync.Authenticate()
Dim oType As Type = Type.GetType("DigitalData.Modules.Jobs.ADSyncJob")
Activator.CreateInstance(oType)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oGroup As String = ListBox1.SelectedItem
_sync.SyncUsersForGroup(oGroup)
'' STEP 1: Get all Groups that have AD_SYNC enabled
'Dim oSQL As String = $"SELECT RECORD_ID, GROUP_NAME FROM VWICM_GROUP WHERE AD_SYNC = 1"
'Dim oResult As DataTable = _firebird.GetDatatable(oSQL)
'For Each oRow As DataRow In oResult.Rows
' Dim oGroupName As String = oRow.Item("GROUP_NAME")
' Dim oGroupId As Int64 = oRow.Item("RECORD_ID")
' ' STEP 2: List all Users for that Group
' Dim oUsers As List(Of ADUser) = _sync.ListUsers(oGroupName)
' For Each oUser In oUsers
' ' STEP 3: Check if user exists in DB
' oSQL = $"SELECT FNICM_GET_RECORD4SYSKEY('{oUser.samAccountName}','001-USRNAME') from RDB$DATABASE"
' Dim oResult2 = _firebird.GetScalarValue(oSQL)
' Dim oUserId
' ' STEP 3.A: If user does not exists, create and add to group
' If IsDBNull(oResult2) Then
' 'Create user
' oSQL = $"SELECT FNICM_RADM_NEW_USER('{oUser.GivenName}', '{oUser.Surname}', '{oUser.samAccountName}', 'AD-Sync') from RDB$DATABASE"
' Dim oResult3 = _firebird.GetScalarValue(oSQL)
' oUserId = oResult3
' Else
' oUserId = oResult2
' End If
' ' STEP 4: Add user to group
' oSQL = $"SELECT FNICM_RADM_NEW_USER2GROUP({oUserId}, {oGroupId}, 'AD-Sync') from RDB$DATABASE"
' Dim oResult4 = _firebird.GetScalarValue(oSQL)
' Next
'Next
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim oGroups = _sync.ListGroups()
For Each oGroup In oGroups
ListBox1.Items.Add(oGroup.Name)
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim oGroup As String = ListBox1.SelectedItem
Dim oUsers = _sync.ListUsers(oGroup)
ListBox2.Items.Clear()
For Each oUser In oUsers
ListBox2.Items.Add(oUser.samAccountName)
Next
If oUsers.Count = 0 Then
ListBox2.Items.Add("No users found ¯\_(ツ)_/¯")
End If
End Sub
End Class