MONSTER: Rename Monorepo to Modules, only keep Projects under Modules.*

This commit is contained in:
Jonathan Jenne
2022-09-29 13:46:00 +02:00
parent e87b97bfec
commit 042bbce9f4
1557 changed files with 380 additions and 160017 deletions

View File

@@ -0,0 +1,13 @@
Public Class ADGroup
Public Property SAMAccountName As String
Public Property ObjectClass As String
Public Property CN As String
Public Property Description As String
Public Property DistinguishedName As String
Public Property Name As String
Public Property ObjectCategory As String
Public Overrides Function ToString() As String
Return SAMAccountName
End Function
End Class

View File

@@ -0,0 +1,28 @@
Imports System.Security.Principal
Public Class ADUser
Public Property GUID As Guid
Public Property samAccountName As String
Public Property SId As SecurityIdentifier
Public Property Surname As String
Public Property GivenName As String
Public Property Middlename As String
Public Property Email As String
Public CustomAttributes As List(Of CustomAttribute)
Public Overrides Function Equals(obj As Object) As Boolean
Return DirectCast(obj, ADUser).samAccountName
End Function
Public Overrides Function ToString() As String
Return samAccountName
End Function
Public Class CustomAttribute
Public Name As String
Public Value As Object
Public MSSQLColumn As String
Public FirebirdSyskey As String
End Class
End Class

View File

@@ -0,0 +1,5 @@
Public Class AttributeMapping
Public AttributeName As String
Public FirebirdSyskey As String
Public MSSQLColumn As String
End Class

View File

@@ -0,0 +1,8 @@
Public Interface ISyncUsers
Function SyncUsers(GroupName As String, Users As List(Of ADUser), PropertyMapping As List(Of AttributeMapping)) As List(Of ADUser)
Function GetGroupId(GroupName As String) As Integer
Function GetUserId(UserName As String) As Integer
Function CreateUser(User As ADUser) As Integer
Function AddUserToGroup(UserId As Integer, GroupId As Integer) As Boolean
Sub AddCustomAttributesToUser(User As ADUser, UserId As Integer)
End Interface

View File

@@ -0,0 +1,145 @@
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Interfaces
Imports DigitalData.Modules.Logging
Namespace SyncUsers
Public Class SyncUsersFirebird
Implements ISyncUsers
Private ReadOnly _logConfig As LogConfig
Private ReadOnly _logger As Logger
Private ReadOnly _firebird As Database.Firebird
Public Sub New(LogConfig As LogConfig, Firebird As Database.Firebird)
_logConfig = LogConfig
_logger = LogConfig.GetLogger()
_firebird = Firebird
End Sub
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)
Try
_logger.Debug("Getting group Id for group [{0}]", GroupName)
oGroupId = GetGroupId(GroupName)
If oGroupId = 0 Then
_logger.Debug("Group [{0}] does not exist in database or is not enabled for sync.", GroupName)
Return oSyncedUsers
End If
_logger.Debug("Using group Id [{0}]", oGroupId)
Catch ex As Exception
_logger.Error(ex)
Return oSyncedUsers
End Try
For Each oUser In Users
Dim oUserId As Int64
Dim oUserExists As Boolean = False
' Check if user already exists
Try
_logger.Debug("Checking if user [{0}] exists", oUser)
oUserId = GetUserId(oUser.samAccountName)
oUserExists = Not IsNothing(oUserId)
_logger.Debug("User [{0}] exists in database: ", oUser, oUserExists)
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could not get UserId for user. Skipping")
Continue For
End Try
' I user does not exist, create a new user
Try
If Not oUserExists Then
_logger.Debug("Creating new user for [{0}]", oUser)
oUserId = CreateUser(oUser)
_logger.Debug("User created with Id [{0}]", oUserId)
End If
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could not create user. Skipping")
Continue For
End Try
' Add the user to group
Try
AddUserToGroup(oUserId, oGroupId)
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could not add user to group. Skipping")
Continue For
End Try
oSyncedUsers.Add(oUser)
Next
Return oSyncedUsers
End Function
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 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"
Dim oGroupId = _firebird.GetScalarValue(oSQL)
If IsDBNull(oGroupId) OrElse oGroupId = 0 Then
_logger.Debug("Group {0} not found in database", GroupName)
Return Nothing
End If
Return oGroupId
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Private Function GetUserId(UserName As String) As Integer Implements ISyncUsers.GetUserId
Try
Dim oSQL As String = $"SELECT FNICM_GET_RECORD4SYSKEY('{UserName}','001-USRNAME') from RDB$DATABASE"
Dim oResult = _firebird.GetScalarValue(oSQL)
If IsDBNull(oResult) Then
Return Nothing
End If
Return oResult
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Private Function CreateUser(User As ADUser) As Integer Implements ISyncUsers.CreateUser
Try
Dim oSQL = $"SELECT FNICM_RADM_NEW_USER('{User?.GivenName}', '{User?.Surname}', '{User?.samAccountName}', 'AD-Sync') from RDB$DATABASE"
Dim oUserId As Integer = _firebird.GetScalarValue(oSQL)
Return oUserId
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
Throw New NotImplementedException()
End Sub
End Class
End Namespace

View File

@@ -0,0 +1,284 @@
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Language
Namespace SyncUsers
Public Class SyncUsersMSSQL
Implements ISyncUsers
Private _logConfig As LogConfig
Private _logger As Logger
Private _mssql As MSSQLServer
Private Const ADDED_WHO = "Active Directory Sync"
Public Sub New(LogConfig As LogConfig, MSSQL As MSSQLServer)
_logConfig = LogConfig
_logger = LogConfig.GetLogger()
_mssql = MSSQL
End Sub
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 oSyncedUserIds As New List(Of Int64)
Dim oCreatedUsers As New List(Of ADUser)
Dim oUpdatedUsers As New List(Of ADUser)
Try
_logger.Debug("Getting group Id for group {0}", GroupName)
oGroupId = GetGroupId(GroupName)
If oGroupId = 0 Then
_logger.Debug("Group {0} does not exist in database. Exiting.", GroupName)
Return oSyncedUsers
End If
_logger.Debug("Using group Id {0}", oGroupId)
Catch ex As Exception
_logger.Error(ex)
Return oSyncedUsers
End Try
For Each oUser In Users
Dim oUserId As Int64
Dim oUserExists As Boolean
' Check if user already exists
Try
_logger.Debug("Checking if user [{0}] exists", oUser)
oUserId = GetUserId(oUser.samAccountName)
oUserExists = oUserId > 0
_logger.Debug("User [{0}] exists in database: [{1}]", oUser, oUserExists)
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could not get UserId for user. Skipping.")
Continue For
End Try
' Collect user ids from existing users
If oUserExists Then
oSyncedUserIds.Add(oUserId)
End If
' Create or update user
Try
If Not oUserExists Then
_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)
oCreatedUsers.Add(oUser)
Else
_logger.Debug("Updating user [{0}]", oUser)
oUserId = UpdateUser(oUser)
If oUserId <> 0 Then
_logger.Debug("User created with Id [{0}]", oUserId)
_logger.Info("Updated User [{0}]", oUser.samAccountName)
oUpdatedUsers.Add(oUser)
End If
End If
Catch ex As Exception
_logger.Error(ex)
_logger.Warn("Could Not create/update user [{0}]. Skipping.", oUser.samAccountName)
Continue For
End Try
' Add custom attributes to user
Try
AddCustomAttributesToUser(oUser, oUserId)
Catch ex As Exception
_logger.Error(ex)
_logger.Debug("Could Not add custom attributes to user {0}. Continuing.", oUser)
End Try
' Add the user to group
Try
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)
Continue For
End Try
oSyncedUsers.Add(oUser)
Next
' Delete users that are assigned to the group but no longer exist in active directory
Dim oUserIdString = String.Join(",", oSyncedUserIds)
If oSyncedUserIds.Count = 0 Then
_logger.Info("Group {0} does not contain any users.", GroupName)
oUserIdString = 0
End If
Dim oSQL As String = $"DELETE FROM TBDD_GROUPS_USER WHERE USER_ID NOT IN ({oUserIdString}) AND GROUP_ID = {oGroupId}"
Dim oDeletedRelations = _mssql.GetScalarValue(oSQL)
If oCreatedUsers.Count > 0 Then
_logger.Info("Created [{0}] new users", oCreatedUsers.Count)
End If
_logger.Info("Updated [{0}] users", oUpdatedUsers.Count)
If oDeletedRelations > 0 Then
_logger.Info("Removed [{0}] users from Group [{1}]", oDeletedRelations, GroupName)
End If
Return oSyncedUsers
End Function
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 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.ExecuteNonQuery(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 Function
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.GetScalarValue(oSQL)
If IsDBNull(oGroupId) OrElse oGroupId = 0 Then
_logger.Debug("Group {0} not found in database.", GroupName)
Return 0
End If
Return oGroupId
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
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.GetScalarValue(oSQL)
If IsDBNull(oUserId) OrElse oUserId = 0 Then
Return 0
End If
Return oUserId
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Private Function CreateUser(User As ADUser) As Integer Implements ISyncUsers.CreateUser
Try
If User Is Nothing Then
_logger.Warn("Argument [User] is nothing. Exiting.")
Throw New ArgumentNullException("User")
End If
Dim oUserId As Integer = GetUserId(User.samAccountName)
If oUserId = 0 Then
Dim oSQL As String = $"INSERT INTO TBDD_USER (PRENAME, NAME, USERNAME, EMAIL, ADDED_WHO) VALUES ('{User?.GivenName}', '{User?.Surname?.Replace("'", "''")}', UPPER('{User?.samAccountName?.Replace("'", "''")}'), '{User?.Email?.Replace("'", "''")}', '{ADDED_WHO}')"
Dim oResult = _mssql.ExecuteNonQuery(oSQL)
If oResult = True Then
oUserId = _mssql.GetScalarValue("SELECT MAX(GUID) FROM TBDD_USER")
Return oUserId
Else
Throw New Exception($"Error while inserting user {User.samAccountName}!")
End If
Else
Return oUserId
End If
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Private Function UpdateUser(User As ADUser) As Integer
Try
If User Is Nothing Then
_logger.Warn("Error in UpdateUser - User object is nothing")
Return 0
End If
If User.samAccountName Is Nothing Then
_logger.Warn("Error in UpdateUser - User samAccountName is nothing")
Return 0
End If
Dim oUserId As Integer = GetUserId(User.samAccountName)
If Not IsNothing(oUserId) Then
If oUserId > 0 Then
Dim oGivenName As String = EscapeQuotes(User.GivenName)
Dim oSurname As String = EscapeQuotes(User.Surname)
Dim oEmail As String = EscapeQuotes(User.Email)
Dim oSQL As String = $"UPDATE TBDD_USER SET PRENAME = '{oGivenName}', NAME = '{oSurname}', EMAIL = '{oEmail}', CHANGED_WHO = '{ADDED_WHO}' WHERE GUID = {oUserId}"
Dim oResult = _mssql.ExecuteNonQuery(oSQL)
If oResult = True Then
Return oUserId
Else
Throw New Exception($"Error while updating user {User.samAccountName}!")
End If
Else
Return oUserId
End If
Else
_logger.Warn("Error in UpdateUser - Could not get a userid for samAccountName: " + User.samAccountName)
Return 0
End If
Catch ex As Exception
_logger.Error(ex)
Throw ex
End Try
End Function
Private Function EscapeQuotes(pString As String)
Dim oString = Utils.NotNull(pString, String.Empty)
Return oString.Replace("'", "''")
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.ExecuteNonQuery(oSQL)
If oResult = False Then
_logger.Debug("Custom Attribute {0} could not be added to user {1}", oAttribute.Name, User.samAccountName)
Continue For
End If
Next
End Sub
End Class
End Namespace

View File

@@ -0,0 +1,19 @@
Imports DigitalData.Modules.Interfaces
Public Class UserEqualityComparer
Implements IEqualityComparer(Of ADUser)
Public Overloads Function Equals(x As ADUser, y As ADUser) As Boolean Implements IEqualityComparer(Of ADUser).Equals
If ReferenceEquals(x, y) Then Return True
If x Is Nothing Or y Is Nothing Then Return False
Return x.SId = y.SId
End Function
Public Overloads Function GetHashCode(obj As ADUser) As Integer Implements IEqualityComparer(Of ADUser).GetHashCode
If obj Is Nothing Then Return 0
Dim oHashCode = obj.SId.GetHashCode()
Return oHashCode
End Function
End Class

View File

@@ -0,0 +1,36 @@
Imports System.DirectoryServices.AccountManagement
<DirectoryRdnPrefix("CN")>
<DirectoryObjectClass("Person")>
Public Class UserPrincipalEx
Inherits UserPrincipal
Public Sub New(Context As PrincipalContext)
MyBase.New(Context)
End Sub
Public Sub New(Context As PrincipalContext, samAccountName As String, Password As String, Enabled As Boolean)
MyBase.New(Context, samAccountName, Password, Enabled)
End Sub
Public Overloads Shared Function FindByIdentity(ByVal Context As PrincipalContext, ByVal IdentityValue As String) As UserPrincipalEx
Return CType(FindByIdentityWithType(Context, GetType(UserPrincipalEx), IdentityValue), UserPrincipalEx)
End Function
Public Overloads Shared Function FindByIdentity(ByVal Context As PrincipalContext, ByVal IdentityType As IdentityType, ByVal IdentityValue As String) As UserPrincipalEx
Return CType(FindByIdentityWithType(Context, GetType(UserPrincipalEx), IdentityType, IdentityValue), UserPrincipalEx)
End Function
Public Function GetAttributeValue(AttributeName As String) As String
Return TryGetAttribute(AttributeName)
End Function
Private Function TryGetAttribute(AttributeName As String) As String
Dim oAttribute = ExtensionGet(AttributeName)
If oAttribute.Length <> 1 Then
Return String.Empty
End If
Return CStr(oAttribute(0))
End Function
End Class