private void CreateFullListOfADContainers()
        {
            for (int i = 0; i < myParent.ListOfAdContainers.Count; i++)
            {
                // Process Organizational Units
                if (myParent.ListOfAdContainers[i].IsOU)
                {
                    // adding this container first, to ensure that any custom sisID fields are preserved. if it already exists, remove it
                    if (fullListOfContainers.ContainsKey(myParent.ListOfAdContainers[i].Guid))
                    {
                        fullListOfContainers.Remove(myParent.ListOfAdContainers[i].Guid);
                    }
                    fullListOfContainers.Add(myParent.ListOfAdContainers[i].Guid, myParent.ListOfAdContainers[i]);

                    ADDomainController dc       = myParent.DictionaryOfADDomainControllers[myParent.ListOfAdContainers[i].DomainControllerKey];
                    DirectoryEntry     parentOU = new DirectoryEntry(myParent.ListOfAdContainers[i].Adspath, dc.DirectoryServerUsername, dc.getDecryptedPassword());
                    Console.WriteLine("!!!!!!" + myParent.ListOfAdContainers[i].Adspath);
                    DirectorySearcher searchForChildOUs = new DirectorySearcher(parentOU);

                    searchForChildOUs.PropertiesToLoad.Add("adspath");
                    searchForChildOUs.PropertiesToLoad.Add("Name");
                    searchForChildOUs.PropertiesToLoad.Add("objectguid");
                    searchForChildOUs.Filter = "(objectCategory=organizationalUnit)";

                    try
                    {
                        SearchResultCollection resultCol = searchForChildOUs.FindAll();

                        for (int j = 0; j < resultCol.Count; j++)
                        {
                            Console.WriteLine("New OU: " + resultCol[j].Properties["Name"][0].ToString() + " PATH: " + resultCol[j].Properties["adspath"][0].ToString());
                            ADContainer containerToAdd = new ADContainer(resultCol[j].Properties["Name"][0].ToString(), resultCol[j].Properties["adspath"][0].ToString(), true, new Guid((System.Byte[])resultCol[j].Properties["objectguid"][0]).ToString(), myParent.ListOfAdContainers[i].SchoolSisID);
                            if (!fullListOfContainers.ContainsKey(containerToAdd.Guid))
                            {
                                fullListOfContainers.Add(containerToAdd.Guid, containerToAdd);
                                containerToAdd.DomainControllerKey = myParent.ListOfAdContainers[i].DomainControllerKey;
                                Console.WriteLine("*** ADDED OU: " + containerToAdd.Name);
                            }
                        }
                    } catch (System.Runtime.InteropServices.COMException)
                    {
                        MessageBox.Show("The server does not appear to be running at " + myParent.ListOfAdContainers[i].Adspath, "Cannot reach server", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        syncErrorExperienced = true;
                        return;
                    }
                }
                else
                // Process Groups
                {
                    fullListOfContainers.Add(myParent.ListOfAdContainers[i].Guid, myParent.ListOfAdContainers[i]);
                    Console.WriteLine("***" + "* ADDED GROUP: " + myParent.ListOfAdContainers[i].Name);
                }
            }
        }
Example #2
0
        private void Button2_Click(object sender, EventArgs e)
        {
            ADDomainController dc = new ADDomainController("Hostname", "AD", "Username", "Domain");

            myParent.LoadedSettings.ListOfADDomainControllers.Add(dc);

            ServerSettingsForm serverSettingsForm = new ServerSettingsForm(dc, myParent);

            serverSettingsForm.ShowDialog();
            UpdateUI();
        }
        private void setParentOUGUIDs()
        {
            foreach (ADContainer group in fullListOfContainers.Values)
            {
                if (group.IsOU)
                {
                    ADDomainController dc       = myParent.DictionaryOfADDomainControllers[group.DomainControllerKey];
                    DirectoryEntry     parentOU = new DirectoryEntry(group.Adspath, dc.DirectoryServerUsername, dc.getDecryptedPassword());
                    //Console.WriteLine("PARENT is: " + parentOU.Parent.Guid);

                    //Check whether the parent group has a manual SIS ID in place, and use that if it exists
                    if (fullListOfContainers.ContainsKey(parentOU.Parent.Guid.ToString()) && fullListOfContainers[parentOU.Parent.Guid.ToString()].UsingManualSisID)
                    {
                        Console.WriteLine("OU NAME TO CHECK: " + fullListOfContainers[parentOU.Guid.ToString()].Name);
                        Console.WriteLine("CUSTOM ID: " + fullListOfContainers[parentOU.Parent.Guid.ToString()].UsingManualSisID + " Parent: " + fullListOfContainers[parentOU.Parent.Guid.ToString()].ManualSisID);
                        group.ParentContainerID = fullListOfContainers[parentOU.Parent.Guid.ToString()].ManualSisID;
                        group.ParentGuid        = parentOU.Parent.Guid.ToString();
                    }
                    else
                    {
                        Console.WriteLine("NOT CUSTOM. CHECKING: " + parentOU.Parent.Name + " USING PARENT ID: " + parentOU.Parent.Guid.ToString());
                        group.ParentContainerID = parentOU.Parent.Guid.ToString();
                        group.ParentGuid        = parentOU.Parent.Guid.ToString();
                    }
                }
                else
                {
                    ADDomainController dc          = myParent.DictionaryOfADDomainControllers[group.DomainControllerKey];
                    DirectoryEntry     parentGroup = new DirectoryEntry(group.Adspath, dc.DirectoryServerUsername, dc.getDecryptedPassword());
                    Console.WriteLine("PARENT GROUP IS: " + parentGroup.Parent.Guid);

                    if (fullListOfContainers.ContainsKey(parentGroup.Parent.Guid.ToString()) && fullListOfContainers[parentGroup.Parent.Guid.ToString()].UsingManualSisID)
                    {
                        Console.WriteLine("GROUP NAME TO CHECK: " + fullListOfContainers[parentGroup.Guid.ToString()].Name);
                        Console.WriteLine("CUSTOM ID: " + fullListOfContainers[parentGroup.Parent.Guid.ToString()].ManualSisID);
                    }
                }
            }
        }
        private void AddMembersToADContainers()
        {
            foreach (var group in fullListOfContainers.Values)
            {
                // getting ADDC details
                ADDomainController dc = myParent.DictionaryOfADDomainControllers[group.DomainControllerKey];
                if (group.IsOU)
                {
                    Console.WriteLine("*****SEARCHING OU: " + group.Name + "GUID: " + group.Guid);
                    DirectoryEntry    ouToSearch = new DirectoryEntry(group.Adspath, dc.DirectoryServerUsername, dc.getDecryptedPassword());
                    DirectorySearcher userSearch = new DirectorySearcher(ouToSearch);
                    userSearch.PageSize    = 1000;
                    userSearch.SearchScope = SearchScope.OneLevel;

                    userSearch.PropertiesToLoad.Add("givenName");
                    userSearch.PropertiesToLoad.Add("sn");
                    userSearch.PropertiesToLoad.Add("mail");
                    userSearch.PropertiesToLoad.Add("samaccountname");
                    userSearch.PropertiesToLoad.Add("name");
                    userSearch.PropertiesToLoad.Add("objectguid");

                    userSearch.Filter = "(&(objectCategory=User)(givenname=*)(sn=*)(samaccountname=*)(name=*))";

                    SearchResultCollection resultCol = userSearch.FindAll();

                    foreach (SearchResult user in resultCol)
                    {
                        //Console.WriteLine("USER ADDED: " + new Guid((System.Byte[])user.Properties["objectguid"][0]).ToString());
                        string mailAddress;
                        if (user.Properties["mail"].Count == 0)
                        {
                            mailAddress = user.Properties["samaccountname"][0].ToString() + "@" + dc.DirectoryServerDomain + ".com";
                        }
                        else
                        {
                            mailAddress = user.Properties["mail"][0].ToString();
                        }
                        User newUser = new User(user.Properties["samaccountname"][0].ToString(), user.Properties["givenName"][0].ToString(), user.Properties["sn"][0].ToString(), mailAddress, new Guid((System.Byte[])user.Properties["objectguid"][0]).ToString());
                        newUser.SchoolSISId = group.SchoolSisID;

                        if (settings.UsingUsernameAsSisID)
                        {
                            newUser.UserSISId = user.Properties["samaccountname"][0].ToString();
                        }
                        else
                        {
                            newUser.UserSISId = newUser.Guid;
                        }

                        group.ListOfMembers.Add(newUser);
                        //Console.WriteLine("ADDED TO GROUP:" + group.Name);

                        if (!fullListOfUsers.ContainsKey(newUser.Guid))
                        {
                            fullListOfUsers.Add(newUser.Guid, newUser);
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Searching Group: " + group.Name);
                    string port;
                    if (dc.Ldaps)
                    {
                        port = ":636";
                    }
                    else
                    {
                        port = ":389";
                    }
                    DirectoryEntry    groupDE = new DirectoryEntry("LDAP://" + dc.DirectoryServerHostname + port, dc.DirectoryServerUsername, dc.getDecryptedPassword());
                    DirectorySearcher groupSE = new DirectorySearcher(groupDE);
                    groupSE.PropertiesToLoad.Add("givenName");
                    groupSE.PropertiesToLoad.Add("sn");
                    groupSE.PropertiesToLoad.Add("mail");
                    groupSE.PropertiesToLoad.Add("samaccountname");
                    groupSE.PropertiesToLoad.Add("name");
                    groupSE.PropertiesToLoad.Add("objectguid");

                    Console.WriteLine("GETTING USERS FOR GROUP WITH DN: " + group.Cn);
                    groupSE.Filter = "(&(memberOf=" + group.Cn + ")(objectCategory=User)(givenname=*)(sn=*)(samaccountname=*)(name=*))";
                    Console.WriteLine("FILTERING: " + groupSE.Filter);

                    foreach (SearchResult user in groupSE.FindAll())
                    {
                        string mailAddress;
                        if (user.Properties["mail"].Count == 0)
                        {
                            mailAddress = user.Properties["samaccountname"][0].ToString() + "@" + dc.DirectoryServerDomain + ".com";
                        }
                        else
                        {
                            mailAddress = user.Properties["mail"][0].ToString();
                        }

                        User newUser = new User(user.Properties["samaccountname"][0].ToString(), user.Properties["givenName"][0].ToString(), user.Properties["sn"][0].ToString(), mailAddress.ToString(), new Guid((System.Byte[])user.Properties["objectguid"][0]).ToString());
                        newUser.SchoolSISId = group.SchoolSisID;
                        if (settings.UsingUsernameAsSisID)
                        {
                            newUser.UserSISId = user.Properties["samaccountname"][0].ToString();
                        }
                        else
                        {
                            newUser.UserSISId = newUser.Guid;
                        }

                        group.ListOfMembers.Add(newUser);
                        Console.WriteLine("ADDED TO GROUP:" + group.Name);

                        if (!fullListOfUsers.ContainsKey(newUser.Guid))
                        {
                            fullListOfUsers.Add(newUser.Guid, newUser);
                        }
                    }
                }
            }
        }