Improve Logging, Add UpdateUsers for MSSQL

This commit is contained in:
Jonathan Jenne 2020-01-20 13:53:58 +01:00
parent 7c12fe60da
commit 3a53c70960
5 changed files with 61 additions and 18 deletions

View File

@ -73,7 +73,7 @@ Public Class ActiveDirectoryInterface
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)
_logger.Debug("Synced {0} users to Firebird", oSyncedUsersFirebird.Count)
End If
Else
_logger.Debug("SyncUsersForGroup: _firebird is nothing. ")
@ -83,7 +83,7 @@ Public Class ActiveDirectoryInterface
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)
_logger.Debug("Synced {0} users to MSSQLServer", oSyncedUsersMSSQL.Count)
End If
Else
_logger.Debug("SyncUsersForGroup: _mssql is nothing. ")

View File

@ -3,6 +3,6 @@
Function GetGroupId(GroupName As String) As Integer
Function GetUserId(UserName As String) As Integer
Function CreateUser(User As ADUser) As Integer
Sub AddUserToGroup(UserId As Integer, GroupId As Integer)
Function AddUserToGroup(UserId As Integer, GroupId As Integer) As Boolean
Sub AddCustomAttributesToUser(User As ADUser, UserId As Integer)
End Interface

View File

@ -79,19 +79,22 @@ Namespace SyncUsers
Return oSyncedUsers
End Function
Private Sub AddUserToGroup(UserId As Integer, GroupId As Integer) Implements ISyncUsers.AddUserToGroup
Private Function AddUserToGroup(UserId As Integer, GroupId As Integer) As Boolean Implements ISyncUsers.AddUserToGroup
Try
Dim oSQL = $"SELECT FNICM_RADM_NEW_USER2GROUP({UserId}, {GroupId}, 'AD-Sync') from RDB$DATABASE"
Dim oRecordId = _firebird.GetScalarValue(oSQL)
If IsDBNull(oRecordId) Then
_logger.Warn("UserId {0} - GroupId {1} relation already exists.", UserId, GroupId)
Return False
End If
Return True
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Sub
End Function
Private Function GetGroupId(GroupName As String) As Integer Implements ISyncUsers.GetGroupId
Try
Dim oSQL As String = $"SELECT FNICM_GET_RECORD4SYSKEY('{GroupName}','002-NAME') from RDB$DATABASE"

View File

@ -20,6 +20,8 @@ Namespace SyncUsers
Public Function SyncUsers(GroupName As String, Users As List(Of ADUser), PropertyMapping As List(Of AttributeMapping)) As List(Of ADUser) Implements ISyncUsers.SyncUsers
Dim oGroupId As Integer
Dim oSyncedUsers As New List(Of ADUser)
Dim oCreatedUsers As New List(Of ADUser)
Dim oUpdatedUsers As New List(Of ADUser)
Try
_logger.Debug("Getting group Id for group {0}", GroupName)
@ -52,17 +54,26 @@ Namespace SyncUsers
Continue For
End Try
' I user does not exist, create a new user
' Create or update user
Try
If Not oUserExists Then
_logger.Debug("Creating new user for {0}", oUser)
_logger.Debug("Creating new user for [{0}]", oUser)
oUserId = CreateUser(oUser)
_logger.Debug("User created with Id {0}", oUserId)
_logger.Debug("User created with Id [{0}]", oUserId)
_logger.Info("Added new User [{0}]", oUser.samAccountName)
oCreatedUsers.Add(oUser)
Else
_logger.Debug("Updating user [{0}]", oUser)
oUserId = UpdateUser(oUser)
_logger.Debug("User created with Id [{0}]", oUserId)
_logger.Info("Updated User [{0}]", oUser.samAccountName)
oUpdatedUsers.Add(oUser)
End If
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could not create user. Skipping.")
_logger.Warn("Could Not create/update user. Skipping.")
Continue For
End Try
@ -71,44 +82,51 @@ Namespace SyncUsers
AddCustomAttributesToUser(oUser, oUserId)
Catch ex As Exception
_logger.Error(ex)
_logger.Debug("Could not add custom attributes to user {0}. Continuing.", oUser)
_logger.Debug("Could Not add custom attributes to user {0}. Continuing.", oUser)
End Try
' Add the user to group
Try
AddUserToGroup(oUserId, oGroupId)
_logger.Info("User [{0}] added to group [{1}]", oUser.samAccountName, GroupName)
If AddUserToGroup(oUserId, oGroupId) Then
_logger.Info("User [{0}] added to group [{1}]", oUser.samAccountName, GroupName)
End If
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could not add user {0} to group {1}. Skipping.", oUser, GroupName)
_logger.Warn("Could Not add user {0} to group {1}. Skipping.", oUser, GroupName)
Continue For
End Try
oSyncedUsers.Add(oUser)
Next
_logger.Info("Created [{0}] new users", oCreatedUsers.Count)
_logger.Info("Updated [{0}] users", oUpdatedUsers.Count)
Return oSyncedUsers
End Function
Private Sub AddUserToGroup(UserId As Integer, GroupId As Integer) Implements ISyncUsers.AddUserToGroup
Private Function AddUserToGroup(UserId As Integer, GroupId As Integer) As Boolean Implements ISyncUsers.AddUserToGroup
Try
Dim oSQL = $"SELECT COUNT(*) FROM TBDD_GROUPS_USER WHERE USER_ID = {UserId} AND GROUP_ID = {GroupId}"
Dim oSQL = $"SELECT COUNT(*) FROM TBDD_GROUPS_USER WHERE USER_ID = {UserId} And GROUP_ID = {GroupId}"
Dim oResult = True
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")
Return False
End If
If oResult = False Then
Throw New Exception("Error while adding user to group!")
End If
Return True
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Sub
End Function
Private Function GetGroupId(GroupName As String) As Integer Implements ISyncUsers.GetGroupId
Try
@ -154,7 +172,7 @@ Namespace SyncUsers
oUserId = _mssql.GetScalarValue("SELECT MAX(GUID) FROM TBDD_USER")
Return oUserId
Else
Throw New Exception("Error while inserting user!")
Throw New Exception($"Error while inserting user {User.samAccountName}!")
End If
Else
Return oUserId
@ -166,12 +184,34 @@ Namespace SyncUsers
End Try
End Function
Private Function UpdateUser(User As ADUser) As Integer
Try
Dim oUserId As Integer = GetUserId(User.samAccountName)
If oUserId > 0 Then
Dim oSQL As String = $"UPDATE TBDD_USER SET PRENAME = '{User.GivenName}', NAME = '{User.Surname}', EMAIL = '{User.Email}', CHANGED_WHO = '{ADDED_WHO}' WHERE GUID = {oUserId}"
Dim oResult = _mssql.NewExecutenonQuery(oSQL)
If oResult = True Then
Return oUserId
Else
Throw New Exception($"Error while updating user {User.samAccountName}!")
End If
Else
Return oUserId
End If
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Public Sub AddCustomAttributesToUser(User As ADUser, UserId As Integer) Implements ISyncUsers.AddCustomAttributesToUser
Dim oCustomAttributes = User.CustomAttributes
_logger.Debug("Adding {0} Custom Attributes to User {1}", oCustomAttributes.Count, User)
For Each oAttribute In oCustomAttributes
_logger.Debug("Adding Custom Attribute [{0}] with value [{1}] to User [{2}]", oAttribute.MSSQLColumn, oAttribute.Value, User)
Dim oSQL As String = $"UPDATE TBDD_USER SET {oAttribute.MSSQLColumn} = '{oAttribute.Value}', CHANGED_WHO = '{ADDED_WHO}' WHERE GUID = {UserId}"
Dim oResult = _mssql.NewExecutenonQuery(oSQL)

View File

@ -36,7 +36,7 @@ Public Class ADSyncJob
If oSyncedUsers Is Nothing Then
_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("Processed [{0}] users for group [{1}]", oSyncedUsers.Count, oGroup)
End If
Next