예제 #1
0
        public DirectoryEntry Add(string sName, string sClass)
        {
            //for instance: sName is CN=***  sClass is "container"
            string newChildLdapPath = string.Concat("LDAP://" + sServer);
            string newChildDN       = string.Concat(sName, ",", parentDN);

            newChildLdapPath = string.Concat(newChildLdapPath, "/", newChildDN);

            //Console.WriteLine("In DirectoryEntries:Add: newChildLdapPath is " + newChildLdapPath);

            //  DirectoryEntry parent = new DirectoryEntry(string.Format("LDAP://{0}/{1}",sServer,parentDN));

            //  Console.WriteLine("parent's DN is " + parent.Name);

            //   Console.WriteLine("grab parent node info " + parent.Properties["distinguishedName"].Value.ToString());

            DirectoryEntry newChild = new DirectoryEntry(newChildLdapPath);

            newChild.SchemaClassName = sClass;

            //Console.WriteLine("dirContext is " + dirContext.DomainName + " dirContext portNumber is " + dirContext.PortNumber + " sClass is " + sClass
            //    + " newchildDN is " + newChildDN );



            int ret = SDSUtils.AddNewObj(dirContext, sClass, newChildDN);

            //System.Threading.Thread.Sleep(5000);

            //Console.WriteLine("addnew obj returned " + ret);

            if (ret == 0)
            {
                this.Add(newChild);
                return(newChild);
            }
            else
            {
                //Console.WriteLine("Failed to add one child " + newChildDN);
                return(null);
            }

            // return newChild;
        }
예제 #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;
            }
        }