mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
Created ActiveDirectoryDeserializer. Import is now working
This commit is contained in:
@@ -24,7 +24,6 @@ namespace mRemoteNG.App
|
||||
}
|
||||
|
||||
#region Public Methods
|
||||
//TODO Fix for TreeListView
|
||||
public static void ImportFromFile(ContainerInfo importDestinationContainer, bool alwaysUseSelectedTreeNode = false)
|
||||
{
|
||||
try
|
||||
@@ -46,9 +45,7 @@ namespace mRemoteNG.App
|
||||
openFileDialog.Filter = string.Join("|", fileTypes.ToArray());
|
||||
|
||||
if (openFileDialog.ShowDialog() != DialogResult.OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var fileName in openFileDialog.FileNames)
|
||||
{
|
||||
@@ -91,34 +88,17 @@ namespace mRemoteNG.App
|
||||
}
|
||||
}
|
||||
|
||||
public static void ImportFromActiveDirectory(string ldapPath)
|
||||
public static void ImportFromActiveDirectory(string ldapPath, ContainerInfo importDestinationContainer)
|
||||
{
|
||||
try
|
||||
{
|
||||
var rootTreeNode = ConnectionTree.TreeView.Nodes[0];
|
||||
var selectedTreeNode = ConnectionTree.TreeView.SelectedNode;
|
||||
|
||||
var parentTreeNode = GetParentTreeNode(rootTreeNode, selectedTreeNode);
|
||||
if (parentTreeNode == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
ActiveDirectoryImporter.Import(ldapPath, parentTreeNode);
|
||||
|
||||
parentTreeNode.Expand();
|
||||
var parentContainer = (ContainerInfo) parentTreeNode.Tag;
|
||||
if (parentContainer != null)
|
||||
{
|
||||
parentContainer.IsExpanded = true;
|
||||
}
|
||||
|
||||
var importer = new ActiveDirectoryImporter();
|
||||
importer.Import(ldapPath, importDestinationContainer);
|
||||
Runtime.SaveConnectionsBG();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.MessageCollector.AddExceptionMessage("App.Import.ImportFromActiveDirectory() failed.", ex,
|
||||
logOnly: true);
|
||||
Runtime.MessageCollector.AddExceptionMessage("App.Import.ImportFromActiveDirectory() failed.", ex, logOnly: true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -152,13 +132,9 @@ namespace mRemoteNG.App
|
||||
logOnly: true);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Private Methods
|
||||
|
||||
private static TreeNode GetParentTreeNode(TreeNode rootTreeNode, TreeNode selectedTreeNode,
|
||||
bool alwaysUseSelectedTreeNode = false)
|
||||
private static TreeNode GetParentTreeNode(TreeNode rootTreeNode, TreeNode selectedTreeNode, bool alwaysUseSelectedTreeNode = false)
|
||||
{
|
||||
TreeNode parentTreeNode;
|
||||
|
||||
@@ -229,6 +205,5 @@ namespace mRemoteNG.App
|
||||
return FileType.Unknown;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -1,115 +1,36 @@
|
||||
using System;
|
||||
using System.Windows.Forms;
|
||||
using System.DirectoryServices;
|
||||
using System.Linq;
|
||||
using mRemoteNG.App;
|
||||
using System.Text.RegularExpressions;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Config.Serializers;
|
||||
using mRemoteNG.Container;
|
||||
using mRemoteNG.Tree;
|
||||
|
||||
|
||||
namespace mRemoteNG.Config.Import
|
||||
{
|
||||
public class ActiveDirectoryImporter
|
||||
public class ActiveDirectoryImporter : IConnectionImporter
|
||||
{
|
||||
public static void Import(string ldapPath, TreeNode parentTreeNode)
|
||||
public void Import(object ldapPath, ContainerInfo destinationContainer)
|
||||
{
|
||||
var ldapPathAsString = ldapPath as string;
|
||||
if (ldapPathAsString == null) return;
|
||||
Import(ldapPathAsString, destinationContainer);
|
||||
}
|
||||
|
||||
public void Import(string ldapPath, ContainerInfo destinationContainer)
|
||||
{
|
||||
try
|
||||
{
|
||||
var treeNode = ConnectionTreeNode.AddNode(TreeNodeType.Container);
|
||||
|
||||
var containerInfo = new ContainerInfo();
|
||||
containerInfo.TreeNode = treeNode;
|
||||
|
||||
var name = "";
|
||||
var match = Regex.Match(ldapPath, "ou=([^,]*)", RegexOptions.IgnoreCase);
|
||||
if (match.Success)
|
||||
{
|
||||
name = match.Groups[1].Captures[0].Value;
|
||||
}
|
||||
else
|
||||
{
|
||||
name = Language.strActiveDirectory;
|
||||
}
|
||||
|
||||
containerInfo.Name = name;
|
||||
|
||||
// We can only inherit from a container node, not the root node or connection nodes
|
||||
if (ConnectionTreeNode.GetNodeType(parentTreeNode) == TreeNodeType.Container)
|
||||
{
|
||||
containerInfo.Parent = (ContainerInfo)parentTreeNode.Tag;
|
||||
}
|
||||
else
|
||||
{
|
||||
containerInfo.Inheritance.DisableInheritance();
|
||||
}
|
||||
|
||||
treeNode.Text = name;
|
||||
treeNode.Name = name;
|
||||
treeNode.Tag = containerInfo;
|
||||
Runtime.ContainerList.Add(containerInfo);
|
||||
|
||||
ImportComputers(ldapPath, treeNode);
|
||||
|
||||
parentTreeNode.Nodes.Add(treeNode);
|
||||
}
|
||||
var deserializer = new ActiveDirectoryDeserializer(ldapPath);
|
||||
var connectionTreeModel = deserializer.Deserialize();
|
||||
var importedRootNode = connectionTreeModel.RootNodes.First();
|
||||
if (importedRootNode == null) return;
|
||||
var childrenToAdd = importedRootNode.Children.ToArray();
|
||||
destinationContainer.AddChildRange(childrenToAdd);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.MessageCollector.AddExceptionMessage("Config.Import.ActiveDirectory.Import() failed.", ex, logOnly: true);
|
||||
}
|
||||
}
|
||||
|
||||
private static void ImportComputers(string ldapPath, TreeNode parentTreeNode)
|
||||
{
|
||||
try
|
||||
{
|
||||
const string ldapFilter = "(objectClass=computer)";
|
||||
|
||||
var ldapSearcher = new DirectorySearcher();
|
||||
var ldapResults = default(SearchResultCollection);
|
||||
var ldapResult = default(SearchResult);
|
||||
|
||||
ldapSearcher.SearchRoot = new DirectoryEntry(ldapPath);
|
||||
ldapSearcher.PropertiesToLoad.AddRange(new[] {"securityEquals", "cn"});
|
||||
ldapSearcher.Filter = ldapFilter;
|
||||
ldapSearcher.SearchScope = SearchScope.OneLevel;
|
||||
|
||||
ldapResults = ldapSearcher.FindAll();
|
||||
|
||||
foreach (SearchResult tempLoopVar_ldapResult in ldapResults)
|
||||
{
|
||||
ldapResult = tempLoopVar_ldapResult;
|
||||
var with_2 = ldapResult.GetDirectoryEntry();
|
||||
var displayName = Convert.ToString(with_2.Properties["cn"].Value);
|
||||
var description = Convert.ToString(with_2.Properties["Description"].Value);
|
||||
var hostName = Convert.ToString(with_2.Properties["dNSHostName"].Value);
|
||||
|
||||
var treeNode = ConnectionTreeNode.AddNode(TreeNodeType.Connection, displayName);
|
||||
|
||||
var connectionInfo = new ConnectionInfo();
|
||||
var inheritanceInfo = new ConnectionInfoInheritance(connectionInfo);
|
||||
inheritanceInfo.TurnOnInheritanceCompletely();
|
||||
inheritanceInfo.Description = false;
|
||||
if (parentTreeNode.Tag is ContainerInfo)
|
||||
{
|
||||
connectionInfo.Parent = (ContainerInfo)parentTreeNode.Tag;
|
||||
}
|
||||
connectionInfo.Inheritance = inheritanceInfo;
|
||||
connectionInfo.Name = displayName;
|
||||
connectionInfo.Hostname = hostName;
|
||||
connectionInfo.Description = description;
|
||||
connectionInfo.TreeNode = treeNode;
|
||||
treeNode.Name = displayName;
|
||||
treeNode.Tag = connectionInfo; //set the nodes tag to the conI
|
||||
//add connection to connections
|
||||
Runtime.ConnectionList.Add(connectionInfo);
|
||||
|
||||
parentTreeNode.Nodes.Add(treeNode);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.MessageCollector.AddExceptionMessage("Config.Import.ActiveDirectory.ImportComputers() failed.", ex, logOnly: true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
89
mRemoteV1/Config/Serializers/ActiveDirectoryDeserializer.cs
Normal file
89
mRemoteV1/Config/Serializers/ActiveDirectoryDeserializer.cs
Normal file
@@ -0,0 +1,89 @@
|
||||
using System;
|
||||
using System.DirectoryServices;
|
||||
using System.Text.RegularExpressions;
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Connection;
|
||||
using mRemoteNG.Container;
|
||||
using mRemoteNG.Tree;
|
||||
using mRemoteNG.Tree.Root;
|
||||
|
||||
|
||||
namespace mRemoteNG.Config.Serializers
|
||||
{
|
||||
public class ActiveDirectoryDeserializer : IDeserializer
|
||||
{
|
||||
private readonly string _ldapPath;
|
||||
|
||||
public ActiveDirectoryDeserializer(string ldapPath)
|
||||
{
|
||||
_ldapPath = ldapPath;
|
||||
}
|
||||
|
||||
public ConnectionTreeModel Deserialize()
|
||||
{
|
||||
var connectionTreeModel = new ConnectionTreeModel();
|
||||
var root = new RootNodeInfo(RootNodeType.Connection);
|
||||
connectionTreeModel.AddRootNode(root);
|
||||
|
||||
ImportContainers(_ldapPath, root);
|
||||
|
||||
return connectionTreeModel;
|
||||
}
|
||||
|
||||
private void ImportContainers(string ldapPath, ContainerInfo parentContainer)
|
||||
{
|
||||
var match = Regex.Match(ldapPath, "ou=([^,]*)", RegexOptions.IgnoreCase);
|
||||
var name = match.Success ? match.Groups[1].Captures[0].Value : Language.strActiveDirectory;
|
||||
|
||||
var newContainer = new ContainerInfo {Name = name};
|
||||
parentContainer.AddChild(newContainer);
|
||||
|
||||
ImportComputers(ldapPath, newContainer);
|
||||
}
|
||||
|
||||
private void ImportComputers(string ldapPath, ContainerInfo parentContainer)
|
||||
{
|
||||
try
|
||||
{
|
||||
const string ldapFilter = "(objectClass=computer)";
|
||||
using (var ldapSearcher = new DirectorySearcher())
|
||||
{
|
||||
ldapSearcher.SearchRoot = new DirectoryEntry(ldapPath);
|
||||
ldapSearcher.Filter = ldapFilter;
|
||||
ldapSearcher.SearchScope = SearchScope.OneLevel;
|
||||
ldapSearcher.PropertiesToLoad.AddRange(new[] { "securityEquals", "cn" });
|
||||
|
||||
var ldapResults = ldapSearcher.FindAll();
|
||||
foreach (SearchResult ldapResult in ldapResults)
|
||||
{
|
||||
using (var directoryEntry = ldapResult.GetDirectoryEntry())
|
||||
DeserializeConnection(directoryEntry, parentContainer);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Runtime.MessageCollector.AddExceptionMessage("Config.Import.ActiveDirectory.ImportComputers() failed.", ex, logOnly: true);
|
||||
}
|
||||
}
|
||||
|
||||
private void DeserializeConnection(DirectoryEntry directoryEntry, ContainerInfo parentContainer)
|
||||
{
|
||||
var displayName = Convert.ToString(directoryEntry.Properties["cn"].Value);
|
||||
var description = Convert.ToString(directoryEntry.Properties["Description"].Value);
|
||||
var hostName = Convert.ToString(directoryEntry.Properties["dNSHostName"].Value);
|
||||
|
||||
var newConnectionInfo = new ConnectionInfo
|
||||
{
|
||||
Name = displayName,
|
||||
Hostname = hostName,
|
||||
Description = description
|
||||
};
|
||||
newConnectionInfo.Inheritance.TurnOnInheritanceCompletely();
|
||||
newConnectionInfo.Inheritance.Description = false;
|
||||
|
||||
parentContainer.AddChild(newConnectionInfo);
|
||||
Runtime.ConnectionList.Add(newConnectionInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ using System;
|
||||
using System.Windows.Forms;
|
||||
using WeifenLuo.WinFormsUI.Docking;
|
||||
using mRemoteNG.App;
|
||||
using mRemoteNG.Container;
|
||||
|
||||
|
||||
namespace mRemoteNG.UI.Window
|
||||
@@ -27,22 +28,22 @@ namespace mRemoteNG.UI.Window
|
||||
EnableDisableImportButton();
|
||||
}
|
||||
|
||||
public void btnImport_Click(Object sender, EventArgs e)
|
||||
public void btnImport_Click(object sender, EventArgs e)
|
||||
{
|
||||
Import.ImportFromActiveDirectory(ActiveDirectoryTree.ADPath);
|
||||
var selectedNodeAsContainer = Windows.treeForm.SelectedNode as ContainerInfo ??
|
||||
Windows.treeForm.SelectedNode.Parent;
|
||||
Import.ImportFromActiveDirectory(ActiveDirectoryTree.ADPath, selectedNodeAsContainer);
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
|
||||
static public void txtDomain_PreviewKeyDown(Object sender, PreviewKeyDownEventArgs e)
|
||||
public static void txtDomain_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Enter)
|
||||
{
|
||||
e.IsInputKey = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void txtDomain_KeyDown(Object sender, KeyEventArgs e)
|
||||
public void txtDomain_KeyDown(object sender, KeyEventArgs e)
|
||||
{
|
||||
if (e.KeyCode == Keys.Enter)
|
||||
{
|
||||
@@ -51,7 +52,7 @@ namespace mRemoteNG.UI.Window
|
||||
}
|
||||
}
|
||||
|
||||
public void btnChangeDomain_Click(Object sender, EventArgs e)
|
||||
public void btnChangeDomain_Click(object sender, EventArgs e)
|
||||
{
|
||||
ChangeDomain();
|
||||
}
|
||||
|
||||
@@ -126,6 +126,7 @@
|
||||
<Compile Include="App\Update\UpdateFile.cs" />
|
||||
<Compile Include="App\Update\UpdateInfo.cs" />
|
||||
<Compile Include="App\Windows.cs" />
|
||||
<Compile Include="Config\Serializers\ActiveDirectoryDeserializer.cs" />
|
||||
<Compile Include="Config\Serializers\CsvConnectionsSerializerMremotengFormat.cs" />
|
||||
<Compile Include="Config\Serializers\CsvConnectionsSerializerRemoteDesktop2008Format.cs" />
|
||||
<Compile Include="Config\Serializers\DataTableDeserializer.cs" />
|
||||
|
||||
Reference in New Issue
Block a user