Exemplo n.º 1
0
        private void moveFolderToBackup(ADUsers user)
        {
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword);//No Domain is required
            try
            {
                //\\192.168.0.111\backups
                string dest = ProgParams.BackupFolder + @"\" + user.Username;
                System.IO.Directory.CreateDirectory(dest);
                System.IO.Directory.CreateDirectory(dest + "\\HDrive");
                System.IO.Directory.CreateDirectory(dest + "\\Profile");
                System.IO.Directory.CreateDirectory(dest + "\\ProfileV2");
                //System.IO.Directory.Move(user.HDrive, dest);

                //System.IO.Directory.CreateDirectory(destDirName);

                DirectoryInfo sourceDir = new DirectoryInfo(user.HDrive);
                DirectoryInfo destDir = new DirectoryInfo(dest + "\\HDrive");

                //Copy the files in the current directory.
                FileInfo[] files = sourceDir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string newPath = Path.Combine(dest + "\\HDrive", file.Name);
                    file.CopyTo(newPath);
                }

                //Copy all sub directories.
                DirectoryInfo[] subDirs = sourceDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    string newPath = Path.Combine(dest + "\\HDrive", subDir.Name);
                    JDStuart.DirectoryUtils.Directory.Move(subDir.FullName, newPath);
                }

                System.IO.Directory.Delete(user.HDrive, true);

                sourceDir = new DirectoryInfo(user.ProfilePath);
                destDir = new DirectoryInfo(dest + "\\Profile");
                //Copy the files in the current directory.
                files = sourceDir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string newPath = Path.Combine(dest + "\\Profile", file.Name);
                    file.CopyTo(newPath);
                }

                //Copy all sub directories.
                subDirs = sourceDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    string newPath = Path.Combine(dest + "\\Profile", subDir.Name);
                    JDStuart.DirectoryUtils.Directory.Move(subDir.FullName, newPath);
                }

                System.IO.Directory.Delete(user.ProfilePath, true);

                sourceDir = new DirectoryInfo(user.ProfilePath + ".V2");
                destDir = new DirectoryInfo(dest + "\\ProfileV2");

                //Copy the files in the current directory.
                files = sourceDir.GetFiles();
                foreach (FileInfo file in files)
                {
                    string newPath = Path.Combine(dest + "\\ProfileV2", file.Name);
                    file.CopyTo(newPath);
                }

                //Copy all sub directories.
                subDirs = sourceDir.GetDirectories();
                foreach (DirectoryInfo subDir in subDirs)
                {
                    string newPath = Path.Combine(dest + "\\ProfileV2", subDir.Name);
                    JDStuart.DirectoryUtils.Directory.Move(subDir.FullName, newPath);
                }

                System.IO.Directory.Delete(user.ProfilePath + ".V2", true);

                //System.IO.Directory.Move(user.ProfilePath, dest);
                //System.IO.Directory.Move(user.ProfilePath + ".V2", dest);
            }
            catch (Exception) { }
            impersonator.undoimpersonateUser();
        }
Exemplo n.º 2
0
        private void uploadCSVFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            // Show the dialog and get result.
            if (Users.Count <= 0) { Users = new SortableBindingList<ADUsers>(); }

            OpenFileDialog openfile = new OpenFileDialog();
            DialogResult result = (openfile.ShowDialog());
            if (result == DialogResult.OK) // Test result.
            {

                FileInfo file = new FileInfo(openfile.FileName);
                if (file.Extension == ".csv")
                {
                    try
                    {
                        using (StreamReader readFile = new StreamReader(file.FullName))
                        {
                            string line;
                            string[] row;
                            int i = 0;
                            while ((line = readFile.ReadLine()) != null)
                            {
                                if (i == 0)
                                {
                                    i++;
                                    continue;
                                }

                                row = line.Split(',');

                                ADUsers alreadyexists = Users.FirstOrDefault<ADUsers>(x => x.Username == row[5]);
                                if (alreadyexists != null)
                                {
                                    Users.First<ADUsers>(x => x.Username == row[5]).ExistsInCSVFile = true;
                                }
                                else
                                {

                                    //output.AppendText(line + "\n");
                                    ADUsers user = new ADUsers();
                                    user.Password = row[0];
                                    user.Grad = row[1];
                                    user.LastName = row[2];
                                    user.FirstName = row[3];
                                    user.MiddleName = row[4];
                                    //if (user.MiddleName.Length >= 6)
                                    //{
                                    //    user.MiddleName = user.MiddleName.Substring(0, 1);
                                    //}
                                    user.Email = row[5] + "@uwo.ca";
                                    user.ExistsInCSVFile = true;
                                    user.Username = row[5];
                                    Users.Add(user);
                                }
                                //parsedData.Add(row);
                            }
                        }
                    }
                    catch (Exception es)
                    {
                        MessageBox.Show(es.Message);
                    }
                }
                else
                {
                    StatusBarUpdate("Incorrect file type.", Color.Red);
                }
            }
            RebindDataSource();
        }
Exemplo n.º 3
0
        private void GetActiveDirectoryStudents()
        {
            try
            {

                string DomainPath = "LDAP:// " + ProgParams.ServerConnectionString;
                DirectoryEntry searchRoot = new DirectoryEntry(DomainPath, ProgParams.UserAccount, ProgParams.UserPassword);
                DirectorySearcher search = new DirectorySearcher(searchRoot);

                search.Filter = "(&(objectClass=user)(objectCategory=person))";
                //search.PropertiesToLoad.Add("samaccountname");
                //search.PropertiesToLoad.Add("mail"); //email
                //search.PropertiesToLoad.Add("usergroup");
                //search.PropertiesToLoad.Add("givenname");//first name
                //search.PropertiesToLoad.Add("lastname");//last name
                //search.PropertiesToLoad.Add("displayname");//full name
                //search.PropertiesToLoad.Add("userprinciplename"); //username
                //search.PropertiesToLoad.Add("profilepath");//first name
                //search.PropertiesToLoad.Add("homedirectory");//first name
                SearchResult result;
                SearchResultCollection resultCol = search.FindAll();
                if (resultCol != null)
                {
                    for (int counter = 0; counter < resultCol.Count; counter++)
                    {
                        string UserNameEmailString = string.Empty;
                        result = resultCol[counter];
                        if (result.Properties.Contains("samaccountname") && result.Properties.Contains("mail")
                            && result.Path.Contains("students"))
                        {
                            ADUsers objSurveyUsers = new ADUsers();
                            objSurveyUsers.Path = (string)result.Path;
                            objSurveyUsers.Sid = (byte[])result.Properties["objectSid"][0];
                            objSurveyUsers.Email = (String)result.Properties["mail"][0];
                            objSurveyUsers.UserDN = (string)result.Path;
                            if (result.Properties.Contains("givenname")) { objSurveyUsers.FirstName = (String)result.Properties["givenname"][0]; }
                            if (result.Properties.Contains("sn")) { objSurveyUsers.LastName = (String)result.Properties["sn"][0]; }
                            if (result.Properties.Contains("initials")) { objSurveyUsers.MiddleName = (String)result.Properties["initials"][0]; }
                            if (result.Properties.Contains("samaccountname")) { objSurveyUsers.Username = (String)result.Properties["samaccountname"][0]; }
                            if (result.Properties.Contains("profilepath")) {objSurveyUsers.ProfilePath = (String)result.Properties["profilepath"][0];}
                            if(result.Properties.Contains("homedirectory")){ objSurveyUsers.HDrive = (String)result.Properties["homedirectory"][0];}
                            try{
                            if (result.Properties.Contains("lastlogontimestamp")){ objSurveyUsers.LastLogin = DateTime.FromFileTime(Convert.ToInt64(result.Properties["lastlogontimestamp"][0].ToString()));}
                            }
                            catch (Exception ex)
                            {
                                //donothing
                            }

                            if (result.Path.Contains("ugrad"))
                            {
                                objSurveyUsers.Grad = "UGRD";
                            }
                            else
                            {
                                objSurveyUsers.Grad = "GRAD";
                            }
                            objSurveyUsers.ExistsInActiveDirectory = true;
                            if (objSurveyUsers != null)
                            {
                                Users.Add(objSurveyUsers);
                            }
                        }
                    }
                }
            }
            catch (IOException)
            {
                MessageBox.Show("Could not receive the active directory users from server. Please make sure server is accessible from from this computer.");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not receive the active directory users from server. Please make sure server is accessible from from this computer.");

            }
        }
Exemplo n.º 4
0
        private void SetFolders(ADUsers user)
        {
            UserImpersonation impersonator = new UserImpersonation();
            impersonator.impersonateUser(ProgParams.UserAccount, "", ProgParams.UserPassword); //No Domain is required

            System.IO.Directory.CreateDirectory(user.HDrive);

            if (System.IO.Directory.Exists(user.HDrive))
            {

                DirectoryInfo diClientDirectory = new DirectoryInfo(user.HDrive);
                DirectorySecurity dsClientDirectory = diClientDirectory.GetAccessControl();
                SecurityIdentifier si = new SecurityIdentifier(user.Sid, 0);
                dsClientDirectory.AddAccessRule(new FileSystemAccessRule(si,
                    FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow));
                diClientDirectory.SetAccessControl(dsClientDirectory);

            }

            System.IO.Directory.CreateDirectory(user.ProfilePath);
            if (System.IO.Directory.Exists(user.ProfilePath))
            {

                DirectoryInfo diClientDirectory = new DirectoryInfo(user.ProfilePath);
                DirectorySecurity dsClientDirectory = diClientDirectory.GetAccessControl();
                SecurityIdentifier si = new SecurityIdentifier(user.Sid, 0);
                dsClientDirectory.AddAccessRule(new FileSystemAccessRule(si,
                    FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow));
                diClientDirectory.SetAccessControl(dsClientDirectory);

            }

            System.IO.Directory.CreateDirectory(user.ProfilePath + ".V2");
            if (System.IO.Directory.Exists(user.ProfilePath + ".V2"))
            {

                DirectoryInfo diClientDirectory = new DirectoryInfo(user.ProfilePath + ".V2");
                DirectorySecurity dsClientDirectory = diClientDirectory.GetAccessControl();
                SecurityIdentifier si = new SecurityIdentifier(user.Sid, 0);
                dsClientDirectory.AddAccessRule(new FileSystemAccessRule(si,
                    FileSystemRights.FullControl,
                    InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit,
                    PropagationFlags.None,
                    AccessControlType.Allow));
                diClientDirectory.SetAccessControl(dsClientDirectory);

            }

            impersonator.undoimpersonateUser();
        }