Much stuff
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
Imports System.DirectoryServices
|
||||
Imports System.DirectoryServices.AccountManagement
|
||||
Imports DigitalData.Modules.Database
|
||||
Imports DigitalData.Modules.Logging
|
||||
|
||||
Public Class ActiveDirectoryInterface
|
||||
Private _logConfig As LogConfig
|
||||
Private _logger As Logger
|
||||
Private _firebird As Firebird
|
||||
|
||||
Private _rootPath As String
|
||||
Private _rootNode As DirectoryEntry
|
||||
@@ -18,9 +21,11 @@ Public Class ActiveDirectoryInterface
|
||||
Private Const NAME = "name"
|
||||
Private Const OBJECTCATEGORY = "objectCategory"
|
||||
|
||||
Public Sub New(LogConfig As LogConfig, Optional RootPath As String = Nothing)
|
||||
Public Sub New(LogConfig As LogConfig, Firebird As Firebird, Optional RootPath As String = Nothing)
|
||||
_logConfig = LogConfig
|
||||
_logger = _logConfig.GetLogger()
|
||||
_firebird = Firebird
|
||||
|
||||
If RootPath Is Nothing Then
|
||||
_rootPath = $"LDAP://{Environment.UserDomainName}"
|
||||
Else
|
||||
@@ -28,25 +33,86 @@ Public Class ActiveDirectoryInterface
|
||||
End If
|
||||
End Sub
|
||||
|
||||
Private Function GetRootNode() As DirectoryEntry
|
||||
Dim oEntry As New DirectoryEntry(_rootPath) With {
|
||||
.AuthenticationType = AuthenticationTypes.Secure,
|
||||
.Password = Nothing,
|
||||
.Username = Nothing
|
||||
}
|
||||
Public Function SyncUsersForGroup(GroupName As String) As List(Of ADUser)
|
||||
Dim oUsers As New List(Of ADUser)
|
||||
Dim oSyncedUsers As New List(Of ADUser)
|
||||
Dim oGroupId As Int64 = Nothing
|
||||
|
||||
Return oEntry
|
||||
End Function
|
||||
Private Function GetRootNode(Username As String, Password As String) As DirectoryEntry
|
||||
Dim oEntry As New DirectoryEntry(_rootPath) With {
|
||||
.AuthenticationType = AuthenticationTypes.Secure,
|
||||
.Password = Username,
|
||||
.Username = Password
|
||||
}
|
||||
Try
|
||||
_logger.Debug("Fetching users from ActiveDirectory")
|
||||
oUsers = ListUsers(GroupName)
|
||||
_logger.Debug("Found {0} users", oUsers.Count)
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
|
||||
Return oEntry
|
||||
If oUsers.Count = 0 Then
|
||||
_logger.Debug("Group {0} does not contain any users.", GroupName)
|
||||
Return oSyncedUsers
|
||||
End If
|
||||
|
||||
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 Nothing
|
||||
End If
|
||||
|
||||
_logger.Debug("Using group Id {0}", oGroupId)
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Return Nothing
|
||||
End Try
|
||||
|
||||
For Each oUser In oUsers
|
||||
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
|
||||
|
||||
|
||||
Public Sub Authenticate()
|
||||
Try
|
||||
Dim oEntry = GetRootNode()
|
||||
@@ -70,19 +136,17 @@ Public Class ActiveDirectoryInterface
|
||||
End Try
|
||||
End Sub
|
||||
|
||||
Public Function ListGroups() As List(Of ADGroup)
|
||||
Return ListGroups(_rootNode)
|
||||
Public Function ListGroups(Optional Query As String = "(&(objectClass=group) (samAccountName=*))") As List(Of ADGroup)
|
||||
Return ListGroups(_rootNode, Query)
|
||||
End Function
|
||||
|
||||
Public Function ListGroups(RootNode As DirectoryEntry) As List(Of ADGroup)
|
||||
Public Function ListGroups(RootNode As DirectoryEntry, Optional Query As String = "(&(objectClass=group) (samAccountName=*))") As List(Of ADGroup)
|
||||
Dim oGroups As New List(Of ADGroup)
|
||||
|
||||
Try
|
||||
Dim oFilterQuery As String = "(&(objectClass=group) (samAccountName=*))"
|
||||
Dim oDirectorySearcher As New DirectorySearcher(RootNode) With {
|
||||
.SearchScope = SearchScope.Subtree,
|
||||
.SizeLimit = SEARCH_LIMIT,
|
||||
.Filter = oFilterQuery
|
||||
.Filter = Query
|
||||
}
|
||||
Dim oResults As SearchResultCollection = oDirectorySearcher.FindAll()
|
||||
|
||||
@@ -95,6 +159,144 @@ Public Class ActiveDirectoryInterface
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function ListUsers(GroupName As String) As List(Of ADUser)
|
||||
Dim oUsers As New List(Of ADUser)
|
||||
|
||||
Try
|
||||
Using oContext As New PrincipalContext(ContextType.Domain)
|
||||
Using oGroupPrincipal As GroupPrincipal = GroupPrincipal.FindByIdentity(oContext, IdentityType.Name, GroupName)
|
||||
If oGroupPrincipal Is Nothing Then
|
||||
_logger.Warn("Group {0} does not exist.", GroupName)
|
||||
Return oUsers
|
||||
End If
|
||||
|
||||
Using oMembers = oGroupPrincipal.GetMembers(True)
|
||||
For Each oMember As Principal In oMembers
|
||||
If TypeOf oMember Is UserPrincipal Then
|
||||
Dim oUser As UserPrincipal = DirectCast(oMember, UserPrincipal)
|
||||
|
||||
oUsers.Add(New ADUser() With {
|
||||
.GUID = oUser.Guid,
|
||||
.SId = oUser.Sid,
|
||||
.samAccountName = oUser.SamAccountName,
|
||||
.Surname = oUser.Surname,
|
||||
.Middlename = oUser.MiddleName,
|
||||
.GivenName = oUser.GivenName,
|
||||
.Email = oUser.EmailAddress
|
||||
})
|
||||
End If
|
||||
Next
|
||||
End Using
|
||||
End Using
|
||||
End Using
|
||||
|
||||
Return oUsers
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
Public Function ListUsers(GroupNames As List(Of String)) As List(Of ADUser)
|
||||
Try
|
||||
Dim oUsers As New List(Of ADUser)
|
||||
Dim oComparer As New UserEqualityComparer()
|
||||
|
||||
For Each oGroup In GroupNames
|
||||
Dim oGroupUsers = ListUsers(oGroup)
|
||||
Dim oNewUsers = oGroupUsers.
|
||||
Except(oUsers, oComparer).
|
||||
ToList()
|
||||
oUsers.AddRange(oNewUsers)
|
||||
Next
|
||||
|
||||
Return oUsers
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Public Function GetGroupId(GroupName As String) As Integer
|
||||
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
|
||||
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 Int64
|
||||
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
|
||||
|
||||
Private Function AddUserToGroup(UserId As Integer, GroupId As Integer) As Int64
|
||||
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 Nothing
|
||||
End If
|
||||
|
||||
Return oRecordId
|
||||
Catch ex As Exception
|
||||
_logger.Error(ex)
|
||||
Throw ex
|
||||
End Try
|
||||
End Function
|
||||
|
||||
Private Function GetRootNode() As DirectoryEntry
|
||||
Dim oEntry As New DirectoryEntry(_rootPath) With {
|
||||
.AuthenticationType = AuthenticationTypes.Secure,
|
||||
.Password = Nothing,
|
||||
.Username = Nothing
|
||||
}
|
||||
|
||||
Return oEntry
|
||||
End Function
|
||||
Private Function GetRootNode(Username As String, Password As String) As DirectoryEntry
|
||||
Dim oEntry As New DirectoryEntry(_rootPath) With {
|
||||
.AuthenticationType = AuthenticationTypes.Secure,
|
||||
.Password = Username,
|
||||
.Username = Password
|
||||
}
|
||||
|
||||
Return oEntry
|
||||
End Function
|
||||
|
||||
Private Function GroupResultsToList(Results As SearchResultCollection) As List(Of ADGroup)
|
||||
Dim oGroups As New List(Of ADGroup)
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
Imports System.Security.Principal
|
||||
|
||||
Public Class ADUser
|
||||
Public GUID As Guid
|
||||
Public SId As SecurityIdentifier
|
||||
Public samAccountName As String
|
||||
Public Surname As String
|
||||
Public GivenName As String
|
||||
Public Middlename As String
|
||||
Public Email As String
|
||||
|
||||
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
|
||||
End Class
|
||||
@@ -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
|
||||
@@ -51,6 +51,7 @@
|
||||
<Reference Include="System.Configuration" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.DirectoryServices" />
|
||||
<Reference Include="System.DirectoryServices.AccountManagement" />
|
||||
<Reference Include="System.IO.Compression" />
|
||||
<Reference Include="System.Runtime.Serialization" />
|
||||
<Reference Include="System.ServiceModel" />
|
||||
@@ -75,6 +76,8 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="ActiveDirectoryInterface.vb" />
|
||||
<Compile Include="ActiveDirectoryInterface\ActiveDirectoryGroup.vb" />
|
||||
<Compile Include="ActiveDirectoryInterface\ActiveDirectoryUser.vb" />
|
||||
<Compile Include="ActiveDirectoryInterface\UserEqualityComparer.vb" />
|
||||
<Compile Include="ZUGFeRDInterface\Exceptions.vb" />
|
||||
<Compile Include="My Project\AssemblyInfo.vb" />
|
||||
<Compile Include="My Project\Application.Designer.vb">
|
||||
@@ -118,6 +121,10 @@
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Modules.Database\Database.vbproj">
|
||||
<Project>{EAF0EA75-5FA7-485D-89C7-B2D843B03A96}</Project>
|
||||
<Name>Database</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Modules.Logging\Logging.vbproj">
|
||||
<Project>{903B2D7D-3B80-4BE9-8713-7447B704E1B0}</Project>
|
||||
<Name>Logging</Name>
|
||||
|
||||
Reference in New Issue
Block a user