UserManager/DDUserManager/DDUserManager/frmADImport_Users.vb
2018-06-29 16:43:15 +02:00

95 lines
4.1 KiB
VB.net

Imports System.DirectoryServices
Imports System.DirectoryServices.AccountManagement
Imports DDUserManager.UserDataSet
Imports DevExpress.XtraGrid.Views.Grid
Imports DD_LIB_Standards
Public Class frmADImport_Users
Private Shared logger As NLog.Logger = NLog.LogManager.GetCurrentClassLogger()
Private Sub frmADImport_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim groups = ClassActiveDirectory.GetActiveDirectoryGroups()
gridAD_Groups.DataSource = groups
viewAD_Groups.Columns.Item(0).Caption = "Gruppe"
Catch ex As Exception
logger.Error(ex, $"Error while loading initial groups")
MsgBox($"Error while loading initial groups")
End Try
End Sub
Private Sub gridADGroups_FocusedRowChanged(sender As Object, e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles viewAD_Groups.FocusedRowChanged
Dim groupName As String = viewAD_Groups.GetRow(e.FocusedRowHandle)
Try
Dim usersForGroup As List(Of ClassActiveDirectory.ADUser) = ClassActiveDirectory.GetActiveDirectoryUsersForGroup(groupName)
UserDataSet.TBLOCAL_ADUSERS.Clear()
For Each user As ClassActiveDirectory.ADUser In usersForGroup
Dim row As TBLOCAL_ADUSERSRow = UserDataSet.TBLOCAL_ADUSERS.NewTBLOCAL_ADUSERSRow()
row.NAME = user.Surname
row.PRENAME = user.GivenName
row.USERNAME = user.Username
row.EMAIL = user.Email
UserDataSet.TBLOCAL_ADUSERS.AddTBLOCAL_ADUSERSRow(row)
Next
TBLOCAL_ADUSERSBindingSource.DataSource = UserDataSet.TBLOCAL_ADUSERS
Catch ex As Exception
logger.Error(ex, $"Error while loading users for group {groupName}")
MsgBox($"Error while loading users for group {groupName}")
End Try
End Sub
Private Sub btnImport_Click(sender As Object, e As EventArgs) Handles btnImport.Click
Try
Dim selectedUserHandles As List(Of Integer) = viewAD_Users.GetSelectedRows().ToList()
Dim importedUsers As Integer = 0
For Each rowHandle In selectedUserHandles
Dim rowView As DataRowView = viewAD_Users.GetRow(rowHandle)
Dim userRow As TBLOCAL_ADUSERSRow = rowView.Row
Dim Username As String = userRow.USERNAME
Dim Prename As String = IIf(IsDBNull(userRow.PRENAME), Nothing, userRow.PRENAME)
Dim Name As String = IIf(IsDBNull(userRow.NAME), Nothing, userRow.NAME)
Dim Email As String = IIf(IsDBNull(userRow.EMAIL), Nothing, userRow.EMAIL)
If Not ClassData.UserExists(Username) Then
If ClassData.InsertUser(Username, Prename, Name, Email) Then
importedUsers = importedUsers + 1
Else
Throw New Exception($"Could not insert user {Username} into Database. Check the log.")
End If
End If
Next
If importedUsers = 0 Then
logger.Warn("No new users imported. All selected users are already in database.")
MsgBox($"Es wurden keine neuen Benutzer importiert, da alle ausgewählten Benutzer bereits in der Benutzerverwaltung vorhanden sind.", MsgBoxStyle.Exclamation, "UserManager")
Else
logger.Info($"{importedUsers} users sucessfully imported.")
MsgBox($"{importedUsers} Benutzer wurden erfolgreich importiert!", MsgBoxStyle.Information, "UserManager")
End If
Catch ex As Exception
logger.Error(ex, "Error while importing users")
MsgBox($"Error while importing users: {ex.Message}", MsgBoxStyle.Critical)
End Try
End Sub
Private Sub viewAD_Users_SelectionChanged(sender As Object, e As DevExpress.Data.SelectionChangedEventArgs) Handles viewAD_Users.SelectionChanged
Dim view As GridView = sender
If view.SelectedRowsCount > 0 Then
btnImport.Enabled = True
Else
btnImport.Enabled = False
End If
End Sub
End Class