update active directory interface to sync to mssql and to sync custom attributes
This commit is contained in:
@@ -9,10 +9,19 @@ Public Class ADUser
|
||||
Public Middlename As String
|
||||
Public 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
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
Public Class AttributeMapping
|
||||
Public AttributeName As String
|
||||
Public FirebirdSyskey As String
|
||||
Public MSSQLColumn As String
|
||||
End Class
|
||||
@@ -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
|
||||
Sub AddUserToGroup(UserId As Integer, GroupId As Integer)
|
||||
Sub AddCustomAttributesToUser(User As ADUser, UserId As Integer)
|
||||
End Interface
|
||||
@@ -0,0 +1,142 @@
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Interfaces
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace SyncUsers
|
||||
Public Class Firebird
|
||||
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.Warn("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 = 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 Sub AddUserToGroup(UserId As Integer, GroupId As Integer) 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)
|
||||
End If
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Sub
|
||||
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
|
||||
170
Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb
Normal file
170
Modules.Interfaces/ActiveDirectoryInterface/SyncUsers.MSSQL.vb
Normal file
@@ -0,0 +1,170 @@
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Namespace SyncUsers
|
||||
Public Class MSSQL
|
||||
Implements ISyncUsers
|
||||
|
||||
Private _logConfig As LogConfig
|
||||
Private _logger As Logger
|
||||
Private _mssql As MSSQLServer
|
||||
|
||||
Public Sub New(LogConfig As LogConfig, MSSQL As Database.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)
|
||||
|
||||
Try
|
||||
_logger.Debug("Getting group Id for group {0}", GroupName)
|
||||
oGroupId = GetGroupId(GroupName)
|
||||
|
||||
If oGroupId = 0 Then
|
||||
_logger.Warn("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 = False
|
||||
|
||||
' 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: ", 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 custom attributes to user
|
||||
Try
|
||||
AddCustomAttributesToUser(oUser, oUserId)
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
_logger.Warn("Could not add custom attributes to user {0}. Continuing", oUser)
|
||||
End Try
|
||||
|
||||
' Add the user to group
|
||||
Try
|
||||
AddUserToGroup(oUserId, oGroupId)
|
||||
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
|
||||
|
||||
Return oSyncedUsers
|
||||
End Function
|
||||
|
||||
Private Sub AddUserToGroup(UserId As Integer, GroupId As Integer) Implements ISyncUsers.AddUserToGroup
|
||||
Try
|
||||
Dim oSQL As String = $"INSERT INTO TBDD_GROUPS_USER (USER_ID, GROUP_ID) VALUES ({UserId}, {GroupId})"
|
||||
Dim oResult = _mssql.NewExecutenonQuery(oSQL)
|
||||
|
||||
If oResult = False Then
|
||||
Throw New Exception("Error while adding user to group!")
|
||||
End If
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
Private Function GetGroupId(GroupName As String) As Integer Implements ISyncUsers.GetGroupId
|
||||
Try
|
||||
Dim oSQL As String = $"SELECT GUID FROM TBDD_GROUPS WHERE NAME = '{GroupName}'"
|
||||
Dim oGroupId = _mssql.NewExecuteScalar(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 USERNAME = '{UserName}'"
|
||||
Dim oUserId = _mssql.NewExecuteScalar(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
|
||||
Dim oSQL As String = $"INSERT INTO TBDD_USER (PRENAME, NAME, USERNAME, EMAIL) VALUES ('{User.GivenName}', '{User.Surname}', '{User.samAccountName}', '{User.Email}')"
|
||||
Dim oResult = _mssql.NewExecutenonQuery(oSQL)
|
||||
|
||||
If oResult = True Then
|
||||
Dim oUserId = _mssql.NewExecuteScalar("SELECT MAX(GUID) FROM TBDD_USER")
|
||||
Return oUserId
|
||||
Else
|
||||
Throw New Exception("Error while inserting user!")
|
||||
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
|
||||
|
||||
For Each oAttribute In oCustomAttributes
|
||||
Dim oSQL As String = $"UPDATE TBDD_USER SET {oAttribute.MSSQLColumn} = '{oAttribute.Value}' WHERE GUID = {UserId}"
|
||||
Dim oResult = _mssql.NewExecutenonQuery(oSQL)
|
||||
|
||||
If oResult = False Then
|
||||
_logger.Warn("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
|
||||
|
||||
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user