namespace TB.ADBlock
{
using System;
using System.Collections.Generic;
using System.DirectoryServices;
public class OU : BaseObject
{
private List<OU> children;
private string description;
private string managedBy;
private OU parent;
public const string PROPERTY_ADDRESS_CITY = "l";
public const string PROPERTY_ADDRESS_COUNTRY = "co";
public const string PROPERTY_ADDRESS_COUNTRYAB = "c";
public const string PROPERTY_ADDRESS_COUNTRYCODE = "countryCode";
public const string PROPERTY_ADDRESS_POSTALCODE = "postalCode";
public const string PROPERTY_ADDRESS_PROVINCE = "st";
public const string PROPERTY_ADDRESS_STREET = "street";
public const string PROPERTY_DESCRIPTION = "description";
public const string PROPERTY_MANAGEDBY = "managedBy";
public const string PROPERTY_OU = "ou";
public OU()
{
}
internal OU(DirectoryEntry entry)
{
if (entry == null)
{
throw new ArgumentNullException();
}
this.Parse(entry);
}
internal OU(SearchResult result)
{
if (result == null)
{
throw new ArgumentNullException();
}
this.Parse(result);
}
public void Add(string locationPath)
{
this.Add(locationPath, base.iUserName, base.iPassword);
}
public void Add(string locationPath, string userName, string password)
{
if (locationPath.IndexOf("LDAP://") >= 0)
{
locationPath = locationPath.Substring(7);
}
DirectoryEntry parent = null;
DirectoryEntry newOU = null;
if (string.IsNullOrEmpty(locationPath))
{
locationPath = ParaMgr.ADFullPath;
}
if (!ADManager.Exists(locationPath))
{
throw new EntryNotExistException("指定的位置对象不存在。");
}
string rdn = Utils.GenerateRDNOU(base.name);
if (ADManager.Exists(Utils.GenerateDN(rdn, locationPath)))
{
throw new EntryNotExistException("指定的位置下存在同名对象。");
}
try
{
parent = ADManager.GetByPath(locationPath, userName, password);
newOU = parent.Children.Add(rdn, "organizationalUnit");
Utils.SetProperty(newOU, "description", this.description);
Utils.SetProperty(newOU, "managedBy", this.managedBy);
newOU.CommitChanges();
this.Parse(newOU);
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (parent != null)
{
parent.Close();
parent.Dispose();
}
if (newOU != null)
{
newOU.Close();
newOU.Dispose();
}
}
}
public List<OU> GetChildren()
{
return this.GetChildren(base.iUserName, base.iPassword);
}
public List<OU> GetChildren(string userName, string password)
{
this.children = new List<OU>();
OU ou = null;
foreach (DirectoryEntry c in ADManager.Search(null, "organizationalUnit", null, base.Path, SearchScope.OneLevel, userName, password))
{
ou = new OU(c) {
parent = this
};
this.children.Add(ou);
if (c != null)
{
c.Close();
c.Dispose();
}
}
return this.children;
}
public OU GetParent()
{
return this.GetParent(base.iUserName, base.iPassword);
}
public OU GetParent(string userName, string password)
{
DirectoryEntry de = null;
try
{
de = ADManager.GetByGuid(base.Guid, userName, password);
DirectoryEntry parentDe = null;
try
{
parentDe = de.Parent;
if (this.parent.SchemaClassName == SchemaClass.organizationalUnit.ToString("F"))
{
this.parent = new OU(parentDe);
}
else
{
this.parent = null;
}
}
catch (DirectoryServicesCOMException dsce)
{
this.parent = null;
throw dsce;
}
finally
{
if (parentDe != null)
{
parentDe.Close();
parentDe.Dispose();
}
}
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
return this.parent;
}
public OU GetSubTree()
{
return this.GetSubTree(base.iUserName, base.iPassword);
}
public OU GetSubTree(string userName, string password)
{
DirectoryEntry de = null;
OU CS$1$0000;
try
{
de = ADManager.GetByGuid(base.guid, userName, password);
this.GetSubTree(de.Path, userName, password);
CS$1$0000 = this;
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
return CS$1$0000;
}
private void GetSubTree(string parentPath, string userName, string password)
{
this.children = new List<OU>();
OU ou = null;
foreach (DirectoryEntry c in ADManager.Search(null, "organizationalUnit", null, parentPath, SearchScope.OneLevel, userName, password))
{
ou = new OU(c) {
parent = this
};
this.children.Add(ou);
ou.GetSubTree(c.Path, userName, password);
if (c != null)
{
c.Close();
c.Dispose();
}
}
}
public void Move(string newLocationPath, bool mustOU)
{
this.Move(newLocationPath, mustOU, base.iUserName, base.iPassword);
}
public void Move(string newLocationPath, bool mustOU, string userName, string password)
{
DirectoryEntry de = ADManager.GetByDN(base.Dn, userName, password);
ADManager.MoveOU(de, newLocationPath, mustOU, userName, password);
this.Parse(de);
de.Close();
de.Dispose();
}
protected override void Parse(DirectoryEntry entry)
{
base.Parse(entry, SchemaClass.organizationalUnit);
this.description = Utils.GetProperty(entry, "description");
this.managedBy = Utils.GetProperty(entry, "managedBy");
}
protected void Parse(SearchResult result)
{
base.Parse(result, SchemaClass.organizationalUnit);
this.description = Utils.GetProperty(result, "description");
this.managedBy = Utils.GetProperty(result, "managedby");
}
public void Remove()
{
this.Remove(base.iUserName, base.iPassword);
}
public void Remove(string userName, string password)
{
DirectoryEntry de = null;
try
{
de = ADManager.GetByGuid(base.guid, userName, password);
if (de.Children.GetEnumerator().MoveNext())
{
throw new ExistChildException("组织单位下存在子对象。");
}
de.DeleteTree();
de.CommitChanges();
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
}
public void Rename(string newName)
{
this.Rename(newName, base.iUserName, base.iPassword);
}
public void Rename(string newName, string userName, string password)
{
DirectoryEntry de = null;
string rdn = Utils.GenerateRDNOU(newName);
if (ADManager.Exists(Utils.GenerateDN(rdn, Utils.GetParentDN(base.Dn))))
{
throw new SameRDNException("已存在同名对象。");
}
try
{
de = ADManager.GetByDN(base.Dn, userName, password);
de.Rename(rdn);
de.CommitChanges();
this.Parse(de);
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
}
public void Update()
{
this.Update(base.iUserName, base.iPassword);
}
public void Update(string userName, string password)
{
DirectoryEntry de = null;
try
{
de = ADManager.GetByGuid(base.Guid, userName, password);
Utils.SetProperty(de, "description", this.description);
Utils.SetProperty(de, "managedBy", this.managedBy);
de.CommitChanges();
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
}
public List<OU> Children
{
get
{
if (this.children == null)
{
this.children = new List<OU>();
}
return this.children;
}
}
public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}
public string ManagedBy
{
get
{
return this.managedBy;
}
set
{
this.managedBy = value;
}
}
public OU Parent
{
get
{
return this.parent;
}
}
}
}
{
using System;
using System.Collections.Generic;
using System.DirectoryServices;
public class OU : BaseObject
{
private List<OU> children;
private string description;
private string managedBy;
private OU parent;
public const string PROPERTY_ADDRESS_CITY = "l";
public const string PROPERTY_ADDRESS_COUNTRY = "co";
public const string PROPERTY_ADDRESS_COUNTRYAB = "c";
public const string PROPERTY_ADDRESS_COUNTRYCODE = "countryCode";
public const string PROPERTY_ADDRESS_POSTALCODE = "postalCode";
public const string PROPERTY_ADDRESS_PROVINCE = "st";
public const string PROPERTY_ADDRESS_STREET = "street";
public const string PROPERTY_DESCRIPTION = "description";
public const string PROPERTY_MANAGEDBY = "managedBy";
public const string PROPERTY_OU = "ou";
public OU()
{
}
internal OU(DirectoryEntry entry)
{
if (entry == null)
{
throw new ArgumentNullException();
}
this.Parse(entry);
}
internal OU(SearchResult result)
{
if (result == null)
{
throw new ArgumentNullException();
}
this.Parse(result);
}
public void Add(string locationPath)
{
this.Add(locationPath, base.iUserName, base.iPassword);
}
public void Add(string locationPath, string userName, string password)
{
if (locationPath.IndexOf("LDAP://") >= 0)
{
locationPath = locationPath.Substring(7);
}
DirectoryEntry parent = null;
DirectoryEntry newOU = null;
if (string.IsNullOrEmpty(locationPath))
{
locationPath = ParaMgr.ADFullPath;
}
if (!ADManager.Exists(locationPath))
{
throw new EntryNotExistException("指定的位置对象不存在。");
}
string rdn = Utils.GenerateRDNOU(base.name);
if (ADManager.Exists(Utils.GenerateDN(rdn, locationPath)))
{
throw new EntryNotExistException("指定的位置下存在同名对象。");
}
try
{
parent = ADManager.GetByPath(locationPath, userName, password);
newOU = parent.Children.Add(rdn, "organizationalUnit");
Utils.SetProperty(newOU, "description", this.description);
Utils.SetProperty(newOU, "managedBy", this.managedBy);
newOU.CommitChanges();
this.Parse(newOU);
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (parent != null)
{
parent.Close();
parent.Dispose();
}
if (newOU != null)
{
newOU.Close();
newOU.Dispose();
}
}
}
public List<OU> GetChildren()
{
return this.GetChildren(base.iUserName, base.iPassword);
}
public List<OU> GetChildren(string userName, string password)
{
this.children = new List<OU>();
OU ou = null;
foreach (DirectoryEntry c in ADManager.Search(null, "organizationalUnit", null, base.Path, SearchScope.OneLevel, userName, password))
{
ou = new OU(c) {
parent = this
};
this.children.Add(ou);
if (c != null)
{
c.Close();
c.Dispose();
}
}
return this.children;
}
public OU GetParent()
{
return this.GetParent(base.iUserName, base.iPassword);
}
public OU GetParent(string userName, string password)
{
DirectoryEntry de = null;
try
{
de = ADManager.GetByGuid(base.Guid, userName, password);
DirectoryEntry parentDe = null;
try
{
parentDe = de.Parent;
if (this.parent.SchemaClassName == SchemaClass.organizationalUnit.ToString("F"))
{
this.parent = new OU(parentDe);
}
else
{
this.parent = null;
}
}
catch (DirectoryServicesCOMException dsce)
{
this.parent = null;
throw dsce;
}
finally
{
if (parentDe != null)
{
parentDe.Close();
parentDe.Dispose();
}
}
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
return this.parent;
}
public OU GetSubTree()
{
return this.GetSubTree(base.iUserName, base.iPassword);
}
public OU GetSubTree(string userName, string password)
{
DirectoryEntry de = null;
OU CS$1$0000;
try
{
de = ADManager.GetByGuid(base.guid, userName, password);
this.GetSubTree(de.Path, userName, password);
CS$1$0000 = this;
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
return CS$1$0000;
}
private void GetSubTree(string parentPath, string userName, string password)
{
this.children = new List<OU>();
OU ou = null;
foreach (DirectoryEntry c in ADManager.Search(null, "organizationalUnit", null, parentPath, SearchScope.OneLevel, userName, password))
{
ou = new OU(c) {
parent = this
};
this.children.Add(ou);
ou.GetSubTree(c.Path, userName, password);
if (c != null)
{
c.Close();
c.Dispose();
}
}
}
public void Move(string newLocationPath, bool mustOU)
{
this.Move(newLocationPath, mustOU, base.iUserName, base.iPassword);
}
public void Move(string newLocationPath, bool mustOU, string userName, string password)
{
DirectoryEntry de = ADManager.GetByDN(base.Dn, userName, password);
ADManager.MoveOU(de, newLocationPath, mustOU, userName, password);
this.Parse(de);
de.Close();
de.Dispose();
}
protected override void Parse(DirectoryEntry entry)
{
base.Parse(entry, SchemaClass.organizationalUnit);
this.description = Utils.GetProperty(entry, "description");
this.managedBy = Utils.GetProperty(entry, "managedBy");
}
protected void Parse(SearchResult result)
{
base.Parse(result, SchemaClass.organizationalUnit);
this.description = Utils.GetProperty(result, "description");
this.managedBy = Utils.GetProperty(result, "managedby");
}
public void Remove()
{
this.Remove(base.iUserName, base.iPassword);
}
public void Remove(string userName, string password)
{
DirectoryEntry de = null;
try
{
de = ADManager.GetByGuid(base.guid, userName, password);
if (de.Children.GetEnumerator().MoveNext())
{
throw new ExistChildException("组织单位下存在子对象。");
}
de.DeleteTree();
de.CommitChanges();
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
}
public void Rename(string newName)
{
this.Rename(newName, base.iUserName, base.iPassword);
}
public void Rename(string newName, string userName, string password)
{
DirectoryEntry de = null;
string rdn = Utils.GenerateRDNOU(newName);
if (ADManager.Exists(Utils.GenerateDN(rdn, Utils.GetParentDN(base.Dn))))
{
throw new SameRDNException("已存在同名对象。");
}
try
{
de = ADManager.GetByDN(base.Dn, userName, password);
de.Rename(rdn);
de.CommitChanges();
this.Parse(de);
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
}
public void Update()
{
this.Update(base.iUserName, base.iPassword);
}
public void Update(string userName, string password)
{
DirectoryEntry de = null;
try
{
de = ADManager.GetByGuid(base.Guid, userName, password);
Utils.SetProperty(de, "description", this.description);
Utils.SetProperty(de, "managedBy", this.managedBy);
de.CommitChanges();
}
catch (DirectoryServicesCOMException dsce)
{
throw dsce;
}
finally
{
if (de != null)
{
de.Close();
de.Dispose();
}
}
}
public List<OU> Children
{
get
{
if (this.children == null)
{
this.children = new List<OU>();
}
return this.children;
}
}
public string Description
{
get
{
return this.description;
}
set
{
this.description = value;
}
}
public string ManagedBy
{
get
{
return this.managedBy;
}
set
{
this.managedBy = value;
}
}
public OU Parent
{
get
{
return this.parent;
}
}
}
}