fix bug in jobrunner, adsync

This commit is contained in:
Jonathan Jenne 2020-01-09 16:43:31 +01:00
parent 0a7cdaa903
commit 8b84e7b9e4
3 changed files with 26 additions and 24 deletions

View File

@ -8,9 +8,6 @@ Public Class ActiveDirectoryInterface
Private _logConfig As LogConfig
Private _logger As Logger
Private ReadOnly _firebird As Firebird
Private ReadOnly _mssql As MSSQLServer
Private ReadOnly _rootPath As String
Private _rootNode As DirectoryEntry
@ -54,8 +51,8 @@ Public Class ActiveDirectoryInterface
Dim oSyncedUsers As New List(Of ADUser)
Dim oGroupId As Int64 = Nothing
Dim oFirebirdSync As New SyncUsers.SyncUsersFirebird(_logConfig, _firebird)
Dim oSQLSync As New SyncUsers.SyncUsersMSSQL(_logConfig, _mssql)
Dim oFirebirdSync As New SyncUsers.SyncUsersFirebird(_logConfig, Firebird)
Dim oSQLSync As New SyncUsers.SyncUsersMSSQL(_logConfig, MSSQL)
Dim oSyncedUsersFirebird, oSyncedUsersMSSQL As List(Of ADUser)
Try
@ -73,19 +70,23 @@ Public Class ActiveDirectoryInterface
End If
' Do the actual sync into firebird
If _firebird IsNot Nothing Then
If Firebird IsNot Nothing Then
oSyncedUsersFirebird = oFirebirdSync.SyncUsers(GroupName, oUsers, AttributeMappings)
If oSyncedUsersFirebird.Count > 0 Then
_logger.Info("Synced {0} users to Firebird", oSyncedUsersFirebird.Count)
End If
Else
_logger.Debug("SyncUsersForGroup: _firebird is nothing. ")
End If
' Do the actual sync into MSSQL
If _mssql IsNot Nothing Then
If MSSQL IsNot Nothing Then
oSyncedUsersMSSQL = oSQLSync.SyncUsers(GroupName, oUsers, AttributeMappings)
If oSyncedUsersMSSQL.Count > 0 Then
_logger.Info("Synced {0} users to MSSQLServer", oSyncedUsersMSSQL.Count)
End If
Else
_logger.Debug("SyncUsersForGroup: _mssql is nothing. ")
End If
Return oUsers
@ -171,10 +172,10 @@ Public Class ActiveDirectoryInterface
Dim oUserFound = FindUserWithFilter(oUserEx, Filter)
If oUserFound = False Then
_logger.Debug("User '{0}' was skipped out due to user filter.", oUserEx.SamAccountName)
_logger.Debug("User [{0}] was skipped out due to user filter.", oUserEx.SamAccountName)
Continue For
End If
_logger.Debug("User '{0}' passed the filter.", oUserEx.SamAccountName)
_logger.Debug("User [{0}] passed the filter.", oUserEx.SamAccountName)
' TODO: Figure out why oUserEx can be nothing for certain users
If oUserEx IsNot Nothing Then
@ -182,7 +183,7 @@ Public Class ActiveDirectoryInterface
Dim oAttributeValue = oUserEx.GetAttributeValue(oMap.AttributeName)
If oAttributeValue <> String.Empty Then
_logger.Debug("Attribute {0} is not empty.", oMap.AttributeName)
_logger.Debug("Attribute [{0}] is not empty.", oMap.AttributeName)
oCustomAttributes.Add(New ADUser.CustomAttribute() With {
.Name = oMap.AttributeName,
@ -193,10 +194,10 @@ Public Class ActiveDirectoryInterface
End If
Next
Else
_logger.Debug("Could not fetch CustomAttributes for user {0}", oUser)
_logger.Debug("Could not fetch CustomAttributes for user [{0}]", oUser)
End If
_logger.Debug("Trying to add User {0} to user list", oUser)
_logger.Debug("Trying to add User [{0}] to user list", oUser)
Dim oNewUser As New ADUser With {
.SId = oUser.Sid,
@ -239,14 +240,14 @@ Public Class ActiveDirectoryInterface
}
If Filter = String.Empty Then
_logger.Warn("FindUserWithFilter: Filter was empty, returning True for User {0}", User.SamAccountName)
_logger.Debug("FindUserWithFilter: Filter was empty, returning True for User [{0}]", User.SamAccountName)
Return True
End If
If Filter.Contains(oPlaceholder) Then
Filter = Filter.Replace(oPlaceholder, User.SamAccountName)
Else
_logger.Warn("FindUserWithFilter: Placeholder '{0}' was not found in filter. Results may not be correct.")
_logger.Warn("FindUserWithFilter: Placeholder [{0}] was not found in filter. Results may not be correct.")
End If
Dim oSearcher As New DirectorySearcher(oEntry, Filter)

View File

@ -58,6 +58,7 @@ Namespace SyncUsers
_logger.Debug("Creating new user for {0}", oUser)
oUserId = CreateUser(oUser)
_logger.Debug("User created with Id {0}", oUserId)
_logger.Info("Added new User [{0}]", oUser.samAccountName)
End If
Catch ex As Exception
_logger.Error(ex)
@ -76,6 +77,7 @@ Namespace SyncUsers
' Add the user to group
Try
AddUserToGroup(oUserId, oGroupId)
_logger.Info("User [{0}] added to group [{1}]", oUser.samAccountName, GroupName)
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could not add user {0} to group {1}. Skipping.", oUser, GroupName)
@ -92,12 +94,11 @@ Namespace SyncUsers
Try
Dim oSQL = $"SELECT COUNT(*) FROM TBDD_GROUPS_USER WHERE USER_ID = {UserId} AND GROUP_ID = {GroupId}"
Dim oResult = True
If _mssql.NewExecuteScalar(oSQL) = 0 Then
If _mssql.GetScalarValue(oSQL) = 0 Then
oSQL = $"INSERT INTO TBDD_GROUPS_USER (USER_ID, GROUP_ID, ADDED_WHO) VALUES ({UserId}, {GroupId}, '{ADDED_WHO}')"
oResult = _mssql.NewExecutenonQuery(oSQL)
Else
_logger.Debug($"UserGroup-Relation [{UserId}/{GroupId}] already existing")
End If
If oResult = False Then
@ -112,7 +113,7 @@ Namespace SyncUsers
Private Function GetGroupId(GroupName As String) As Integer Implements ISyncUsers.GetGroupId
Try
Dim oSQL As String = $"SELECT GUID FROM TBDD_GROUPS WHERE UPPER(NAME) = UPPER('{GroupName}') AND AD_SYNC = 1 AND ACTIVE = 1"
Dim oGroupId = _mssql.NewExecuteScalar(oSQL)
Dim oGroupId = _mssql.GetScalarValue(oSQL)
If IsDBNull(oGroupId) OrElse oGroupId = 0 Then
_logger.Debug("Group {0} not found in database.", GroupName)
@ -129,7 +130,7 @@ Namespace SyncUsers
Private Function GetUserId(UserName As String) As Integer Implements ISyncUsers.GetUserId
Try
Dim oSQL As String = $"SELECT GUID FROM TBDD_USER WHERE UPPER(USERNAME) = UPPER('{UserName}')"
Dim oUserId = _mssql.NewExecuteScalar(oSQL)
Dim oUserId = _mssql.GetScalarValue(oSQL)
If IsDBNull(oUserId) OrElse oUserId = 0 Then
Return 0
@ -150,7 +151,7 @@ Namespace SyncUsers
Dim oResult = _mssql.NewExecutenonQuery(oSQL)
If oResult = True Then
oUserId = _mssql.NewExecuteScalar("SELECT MAX(GUID) FROM TBDD_USER")
oUserId = _mssql.GetScalarValue("SELECT MAX(GUID) FROM TBDD_USER")
Return oUserId
Else
Throw New Exception("Error while inserting user!")

View File

@ -25,18 +25,18 @@ Public Class ADSyncJob
Exit Sub
End If
Dim oGroups = GetGroups(Arguments.GroupFilter)
Dim oGroups = GetGroups()
Dim oAttributeMappings = GetAttributeMappings()
_Logger.Debug("Found {0} Groups", oGroups)
_Logger.Debug("Found {0} Groups", oGroups.Count)
For Each oGroup In oGroups
_Logger.Debug("Syncing Group {0}", oGroup)
_Logger.Debug("Syncing Group [{0}]", oGroup)
Dim oSyncedUsers = oSync.SyncUsersForGroup(oGroup, oAttributeMappings, _Firebird, _MSSQL, Arguments.UserFilter)
If oSyncedUsers Is Nothing Then
_Logger.Warn("Group {0} could not be synced!", oGroup)
_Logger.Warn("Group [{0}] could not be synced!", oGroup)
ElseIf oSyncedUsers.Count > 0 Then
_Logger.Info("Synced {0} users for group {1}", oSyncedUsers.Count, oGroup)
_Logger.Info("Synced [{0}] users for group [{1}]", oSyncedUsers.Count, oGroup)
End If
Next