Exemplo n.º 1
0
        //The DeleteTree method deletes this entry and its entire subtree from the Active Directory hierarchy.
        public void DeleteTree()
        {
            if (!get_baseDnFor_guidOrsid_called)
            {
                Get_baseDn_Guid_Or_sid();
            }

            DirectoryEntries oneLevel_children = this.Children;

            if (oneLevel_children != null && oneLevel_children.Count > 0)
            {
                foreach (DirectoryEntry entry in oneLevel_children)
                {
                    entry.DeleteTree();
                }
            }

            int ret = SDSUtils.DeleteObj(dirContext, baseDn);
        }
Exemplo n.º 2
0
        public void CommitChanges()
        {
            Assign_dirContext();

            if (dirContext == null)
            {
                return;
            }

            if (!get_baseDnFor_guidOrsid_called)
            {
                Get_baseDn_Guid_Or_sid();
            }

            string[]    search_attrs = { null };
            LdapMessage ldapMessage  = dirContext.SearchSynchronous(
                baseDn,
                LdapAPI.LDAPSCOPE.BASE,
                "(objectClass=*)",
                search_attrs,
                false);
            List <LdapEntry> ldapEntries = (ldapMessage != null ? ldapMessage.Ldap_Get_Entries() : null);

            //if this object does not exist in AD, we need create it first
            if (ldapEntries == null || ldapEntries.Count == 0)
            {
                int ret = SDSUtils.AddNewObj(dirContext, objectClassType, baseDn);
                if (ret != 0)
                {
                    //Console.WriteLine("Create new object failed!");
                    return;
                }
            }

            //go through the properties to check whether there is PropertyValueCollection has been modified
            //PropertyCollection: Dictionary<string, PropertyValueCollection>
            if (propertyCollection != null && propertyCollection.Count > 0)
            {
                foreach (KeyValuePair <string, PropertyValueCollection> kvp in propertyCollection)
                {
                    if (kvp.Value.Modified)
                    {
                        //Console.WriteLine("BaseDN is " + baseDn + " Modified key value pair: " + kvp.Key );
                        int ret = SDSUtils.ModifyProperty(dirContext, baseDn, kvp.Key, kvp.Value);
                        //if (ret != 0) ; Console.WriteLine("Modify a property failed");
                    }
                }
            }

            //go through its children to see whether this is any children marked needed be deleted
            if (children != null && children.Count > 0)
            {
                DirectoryEntries modifiedChildren = new DirectoryEntries();

                foreach (DirectoryEntry child in children)
                {
                    if (child.ToBeDeleted) //delete this DE
                    {
                        int ret = SDSUtils.DeleteObj(dirContext, child.Name);
                    }
                }

                //reflect the changes to children collection
                foreach (DirectoryEntry child in children)
                {
                    if (!child.ToBeDeleted)
                    {
                        modifiedChildren.Add(child);
                    }
                }

                children = modifiedChildren;
            }
        }