/// <summary> /// 获取组织单位对象的子树。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> /// <returns></returns> public OU GetSubTree(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.guid, userName, password); this.GetSubTree(de.Path, userName, password); return(this); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } }
/// <summary> /// 更新组织单位。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Update(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.Guid, userName, password); Utils.SetProperty(de, OU.PROPERTY_DESCRIPTION, this.description); Utils.SetProperty(de, OU.PROPERTY_MANAGEDBY, this.managedBy); // 注意,不能是转义/的DN de.CommitChanges(); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } }
/// <summary> /// 删除组织单位。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Remove(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.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(); } } }
/// <summary> /// 删除组。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Remove(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.guid, userName, password); de.DeleteTree(); de.CommitChanges(); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } }
/// <summary> /// 更新组。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Update(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.Guid, userName, password); Utils.SetProperty(de, Group.PROPERTY_ACCOUNT, this.accountName); // 会更改组名 Utils.SetProperty(de, Group.PROPERTY_INFO, this.info); Utils.SetProperty(de, Group.PROPERTY_DESCRIPTION, this.description); de.CommitChanges(); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } }
/// <summary> /// 获取User所在的位置的Guid /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> /// <returns></returns> public Guid GetLocation(string userName, string password) { DirectoryEntry de = null; DirectoryEntry parent = null; try { de = ADManager.GetByGuid(this.guid); parent = de.Parent; return(parent.Guid); } catch { throw; } finally { if (de != null) { de.Close(); de.Dispose(); } if (parent != null) { parent.Close(); parent.Dispose(); } } }
/// <summary> /// 移动User到指定位置。 /// </summary> /// <param name="newLocationPath">移动到的位置的ADsPath</param> /// <param name="mustOU">移动到的位置对应的DirectoryEntry是否必须是组织单位。</param> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Move(string newLocationPath, bool mustOU, string userName, string password) { DirectoryEntry de = ADManager.GetByGuid(this.guid, userName, password); ADManager.MoveUser(de, newLocationPath, mustOU, userName, password); this.Parse(de); de.Close(); de.Dispose(); }
/// <summary> /// 获取组织单位的父组织单位, /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> /// <returns></returns> public OU GetParent(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.Guid, userName, password); DirectoryEntry parentDe = null; try { parentDe = de.Parent; if (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); }
/// <summary> /// 将用户从组中移除。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> /// <param name="usersDN">需要移除的用户的DN。</param> public void RemoveUser(string userName, string password, params string[] usersDN) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.guid, userName, password); // 过滤 -- 如果移除一个不存在的member,会引发异常 List <string> toRemoves = new List <string>(); foreach (string user in usersDN) { if (Array.BinarySearch(this.members, user) >= 0) { toRemoves.Add(user); } } foreach (string user in toRemoves) { de.Properties[Group.PROPERTY_MEMBER].Remove( Utils.UnEscapeDNBackslashedChar(user)); // 去除/转义,以便匹配 } de.CommitChanges(); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } }
/// <summary> /// 更新用户。 /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> public void Update(string userName, string password) { DirectoryEntry de = null; try { de = ADManager.GetByGuid(this.guid, userName, password); Utils.SetProperty(de, User.PROPERTY_ACCOUNT_SAM, this.userName); // 可以更改 Utils.SetProperty(de, User.PROPERTY_GENERAL_GIVENNAME, this.firstName); Utils.SetProperty(de, User.PROPERTY_GENERAL_LASTNAME, this.lastName); Utils.SetProperty(de, User.PROPERTY_GENERAL_INITIALS, this.initials); Utils.SetProperty(de, User.PROPERTY_GENERAL_DISPLAYNAME, this.displayName); Utils.SetProperty(de, User.PROPERTY_GENERAL_OFFICE, this.office); Utils.SetProperty(de, User.PROPERTY_ORGAN_TITLE, this.title); Utils.SetProperty(de, User.PROPERTY_ORGAN_MANAGER, this.manager); // 注意,不能是转义/的DN Utils.SetProperty(de, User.PROPERTY_ORGAN_DEPARTMENT, this.department); Utils.SetProperty(de, User.PROPERTY_GENERAL_TEL, this.telephone); Utils.SetProperty(de, User.PROPERTY_TEL_MOBILE, this.mobile); Utils.SetProperty(de, User.PROPERTY_GENERAL_MAIL, this.mail); Utils.SetProperty(de, User.PROPERTY_ACCOUNT_PRINCIPAL, this.principalName); // 可以更改 Utils.SetProperty(de, User.PROPERTY_ACCOUNT_CONTROL, this.userAccountControl); de.CommitChanges(); } catch (DirectoryServicesCOMException dsce) { throw dsce; } finally { if (de != null) { de.Close(); de.Dispose(); } } }
/// <summary> /// 获取User所在的位置的Guid /// </summary> /// <param name="userName">用户身份标识--用户名。为空时使用默认用户身份标识。</param> /// <param name="password">用户身份标识--密码。</param> /// <returns></returns> public OU GetOrganization(string userName, string password) { DirectoryEntry de = null; DirectoryEntry parent = null; try { de = ADManager.GetByGuid(this.guid); parent = de.Parent; if (parent.SchemaClassName == SchemaClass.organizationalUnit.ToString("F")) { return(new OU(parent)); } else { return(null); } } catch { throw; } finally { if (de != null) { de.Close(); de.Dispose(); } if (parent != null) { parent.Close(); parent.Dispose(); } } }