关于AD的操作--Group类

namespace TB.ADBlock
{
    using System;
    using System.Collections.Generic;
    using System.DirectoryServices;


    public class Group : BaseObject
    {
        private string accountName;
        private string description;
        private string info;
        private string[] memberOf;
        private string[] members;
        public const string PROPERTY_ACCOUNT = "sAMAccountName";
        public const string PROPERTY_ACCOUNTYPE = "sAMAccountType";
        public const string PROPERTY_CN = "cn";
        public const string PROPERTY_DESCRIPTION = "description";
        public const string PROPERTY_GROUPTYPE = "groupType";
        public const string PROPERTY_INFO = "info";
        public const string PROPERTY_MAIL = "mail";
        public const string PROPERTY_MANAGEDBY = "managedBy";
        public const string PROPERTY_MEMBER = "member";
        public const string PROPERTY_MEMBEROF = "memberOf";


        public Group()
        {
        }


        internal Group(DirectoryEntry entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException();
            }
            this.Parse(entry);
        }


        internal Group(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 newGroup = null;
            if (string.IsNullOrEmpty(locationPath))
            {
                locationPath = "CN=Users," + ParaMgr.ADFullPath;
            }
            if (!ADManager.Exists(locationPath))
            {
                throw new EntryNotExistException("指定的位置对象不存在。");
            }
            string rdn = Utils.GenerateRDNCN(base.name);
            if (ADManager.Exists(Utils.GenerateDN(rdn, locationPath)))
            {
                throw new EntryNotExistException("指定的位置下存在同名对象。");
            }
            try
            {
                parent = ADManager.GetByPath(locationPath, userName, password);
                newGroup = parent.Children.Add(rdn, "group");
                Utils.SetProperty(newGroup, "sAMAccountName", this.accountName);
                Utils.SetProperty(newGroup, "info", this.info);
                Utils.SetProperty(newGroup, "description", this.description);
                Utils.SetProperty(newGroup, "groupType", -2147483646);
                newGroup.CommitChanges();
                this.Parse(newGroup);
            }
            catch (DirectoryServicesCOMException dsce)
            {
                throw dsce;
            }
            finally
            {
                if (parent != null)
                {
                    parent.Close();
                    parent.Dispose();
                }
                if (newGroup != null)
                {
                    newGroup.Close();
                    newGroup.Dispose();
                }
            }
        }


        public void AddUser(params string[] usersDN)
        {
            this.AddUser(base.iUserName, base.iPassword, usersDN);
        }


        public void AddUser(string userName, string password, params string[] usersDN)
        {
            DirectoryEntry de = null;
            try
            {
                de = ADManager.GetByGuid(base.guid, userName, password);
                List<string> toAdd = new List<string>();
                foreach (string udn in usersDN)
                {
                    if (Array.BinarySearch<string>(this.members, udn) < 0)
                    {
                        toAdd.Add(udn);
                    }
                }
                foreach (string user in toAdd)
                {
                    de.Properties["member"].Add(Utils.UnEscapeDNBackslashedChar(user));
                }
                de.CommitChanges();
            }
            catch (DirectoryServicesCOMException dsce)
            {
                throw dsce;
            }
            finally
            {
                if (de != null)
                {
                    de.Close();
                    de.Dispose();
                }
            }
        }


        public List<string> GetMemberOfDN()
        {
            List<string> dn = new List<string>();
            dn.AddRange(this.memberOf);
            return dn;
        }


        public List<DirectoryEntry> GetMembers()
        {
            return this.GetMembers(base.iUserName, base.iPassword);
        }


        public List<DirectoryEntry> GetMembers(string userName, string password)
        {
            List<DirectoryEntry> CS$1$0000;
            List<DirectoryEntry> entries = new List<DirectoryEntry>();
            DirectoryEntry de = null;
            try
            {
                foreach (string member in this.members)
                {
                    de = ADManager.GetByDN(member, userName, password);
                    if (de != null)
                    {
                        entries.Add(de);
                    }
                }
                CS$1$0000 = entries;
            }
            catch (DirectoryServicesCOMException dsce)
            {
                foreach (DirectoryEntry d in entries)
                {
                    if (d != null)
                    {
                        d.Close();
                        d.Dispose();
                    }
                }
                throw dsce;
            }
            return CS$1$0000;
        }


        public List<User> GetUserMembers()
        {
            return this.GetUserMembers(base.iUserName, base.iPassword);
        }


        public List<User> GetUserMembers(string userName, string password)
        {
            List<User> CS$1$0000;
            List<User> users = new List<User>();
            DirectoryEntry de = null;
            string userSchemaClassName = SchemaClass.user.ToString("F");
            try
            {
                foreach (string member in this.members)
                {
                    de = ADManager.GetByDN(member, userName, password);
                    if (de != null)
                    {
                        if (de.SchemaClassName == userSchemaClassName)
                        {
                            users.Add(new User(de));
                        }
                        de.Close();
                        de.Dispose();
                    }
                }
                CS$1$0000 = users;
            }
            catch (DirectoryServicesCOMException dsce)
            {
                throw dsce;
            }
            finally
            {
                if (de != null)
                {
                    de.Close();
                    de.Dispose();
                }
            }
            return CS$1$0000;
        }


        protected override void Parse(DirectoryEntry entry)
        {
            base.Parse(entry, SchemaClass.group);
            this.accountName = Utils.GetProperty(entry, "sAMAccountName");
            this.description = Utils.GetProperty(entry, "description");
            this.info = Utils.GetProperty(entry, "info");
            if (entry.Properties.Contains("member"))
            {
                List<string> ms = new List<string>();
                foreach (object m in entry.Properties["member"])
                {
                    ms.Add(Utils.EscapeDNBackslashedChar(m.ToString()));
                }
                ms.Sort();
                this.members = ms.ToArray();
            }
            else
            {
                this.members = new string[0];
            }
            if (entry.Properties.Contains("memberOf"))
            {
                List<string> ms = new List<string>();
                foreach (object m in entry.Properties["memberOf"])
                {
                    ms.Add(Utils.EscapeDNBackslashedChar(m.ToString()));
                }
                ms.Sort();
                this.memberOf = ms.ToArray();
            }
            else
            {
                this.memberOf = new string[0];
            }
        }


        protected void Parse(SearchResult result)
        {
            base.Parse(result, SchemaClass.group);
            this.accountName = Utils.GetProperty(result, "samaccountname");
            this.description = Utils.GetProperty(result, "description");
            this.info = Utils.GetProperty(result, "info");
            if (result.Properties.Contains("member"))
            {
                List<string> ms = new List<string>();
                foreach (object m in result.Properties["member"])
                {
                    ms.Add(Utils.EscapeDNBackslashedChar(m.ToString()));
                }
                ms.Sort();
                this.members = ms.ToArray();
            }
            else
            {
                this.members = new string[0];
            }
            if (result.Properties.Contains("memberof"))
            {
                List<string> ms = new List<string>();
                foreach (object m in result.Properties["memberof"])
                {
                    ms.Add(Utils.EscapeDNBackslashedChar(m.ToString()));
                }
                ms.Sort();
                this.memberOf = ms.ToArray();
            }
            else
            {
                this.memberOf = new string[0];
            }
        }


        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);
                de.DeleteTree();
                de.CommitChanges();
            }
            catch (DirectoryServicesCOMException dsce)
            {
                throw dsce;
            }
            finally
            {
                if (de != null)
                {
                    de.Close();
                    de.Dispose();
                }
            }
        }


        public void RemoveUser(params string[] usersDN)
        {
            this.RemoveUser(base.iUserName, base.iPassword, usersDN);
        }


        public void RemoveUser(string userName, string password, params string[] usersDN)
        {
            DirectoryEntry de = null;
            try
            {
                de = ADManager.GetByGuid(base.guid, userName, password);
                List<string> toRemoves = new List<string>();
                foreach (string user in usersDN)
                {
                    if (Array.BinarySearch<string>(this.members, user) >= 0)
                    {
                        toRemoves.Add(user);
                    }
                }
                foreach (string user in toRemoves)
                {
                    de.Properties["member"].Remove(Utils.UnEscapeDNBackslashedChar(user));
                }
                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.GenerateRDNCN(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, "sAMAccountName", this.accountName);
                Utils.SetProperty(de, "info", this.info);
                Utils.SetProperty(de, "description", this.description);
                de.CommitChanges();
            }
            catch (DirectoryServicesCOMException dsce)
            {
                throw dsce;
            }
            finally
            {
                if (de != null)
                {
                    de.Close();
                    de.Dispose();
                }
            }
        }


        public string AccountName
        {
            get
            {
                return this.accountName;
            }
            set
            {
                this.accountName = value;
                foreach (char i in Utils.InvalidSAMAccountNameChars)
                {
                    this.accountName = this.accountName.Replace(i, '_');
                }
            }
        }


        public string Description
        {
            get
            {
                return this.description;
            }
            set
            {
                this.description = value;
            }
        }


        public string Info
        {
            get
            {
                return this.info;
            }
            set
            {
                this.info = value;
            }
        }


        public string[] MembersDN
        {
            get
            {
                return this.members;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值