private void setPermissions(XElement permsElement, string directory, FtpPermissions permissions)
        {
            var q = from e in permsElement.Elements()
                    where e.Attribute("Dir").Value == directory
                    select e;

            XElement pElement;

            if (q.Count() != 0)
            {
                pElement = q.First();
            }
            else
            {
                // Add permissions element where it doesn't exist.
                pElement = new XElement("Permission", new XAttribute("Dir", directory));
                permsElement.Add(pElement);
            }

            setOptionElementValue(pElement, "FileRead", permissions.FileRead);
            setOptionElementValue(pElement, "FileWrite", permissions.FileWrite);
            setOptionElementValue(pElement, "FileDelete", permissions.FileDelete);
            setOptionElementValue(pElement, "FileAppend", permissions.FileAppend);
            setOptionElementValue(pElement, "DirCreate", permissions.DirectoryCreate);
            setOptionElementValue(pElement, "DirDelete", permissions.DirectoryDelete);
            setOptionElementValue(pElement, "DirList", permissions.DirectoryList);
            setOptionElementValue(pElement, "DirSubdirs", permissions.DirectoryOpen);
        }
        public void SetUserPermissions(string userName, DirectoryInfo directoryInfo, FtpPermissions permissions)
        {
            XDocument document     = getConfigDocument();
            XElement  permsElement = getUserQuery(userName, document).First().Element("Permissions");

            setPermissions(permsElement, directoryInfo.ToString(), permissions);
            saveConfigDocument(document);
        }
Exemple #3
0
        public static FtpPermissions ToFtpPermissions(bool execute, bool write, bool read)
        {
            FtpPermissions ftpPermissions = FtpPermissions.None;

            ftpPermissions |= execute ? FtpPermissions.Execute : FtpPermissions.None;
            ftpPermissions |= write ? FtpPermissions.Write : FtpPermissions.None;
            ftpPermissions |= read ? FtpPermissions.Read : FtpPermissions.None;

            return(ftpPermissions);
        }
        private void createFtpGroup(Customer customer)
        {
            DirectoryInfo homeDirectory = customer.GetDirectory(BaseWebsiteDirectory);

            // Create the FTP group to which all users will be added.
            ftpManager.CreateGroup(customer.FtpGroupName, homeDirectory);

            // Don't allow users to create anything in base directory.
            FtpPermissions basePerms = new FtpPermissions();

            basePerms.DirectoryOpen = true;
            basePerms.DirectoryList = true;
            ftpManager.SetGroupPermissions(customer.FtpGroupName, homeDirectory, basePerms);
        }
Exemple #5
0
        public void CrudGroupAndUser()
        {
            FileZillaManager manager          = LocalContext.Default.CreateManager <FileZillaManager>();
            string           homePath         = Path.Combine(@"C:\Websites", RhspDataID.Generate().ToString());
            string           group            = "test-group-" + Guid.NewGuid().ToString();
            string           userName         = "******" + Guid.NewGuid().ToString();
            string           password         = "******";
            string           uploadText       = "Hello world!";
            string           ftpServer        = "192.168.10.10"; // FileZilla doesn't like localhost or 127.0.0.1
            string           uploadFileName   = "Test.txt";
            string           downloadText     = string.Empty;
            string           websiteDirectory = "test-website.com";
            string           websitePath      = Path.Combine(homePath, websiteDirectory);

            manager.CreateGroup(group, new DirectoryInfo(homePath));
            manager.CreateUser(userName, password, true, group);

            // Ensure that FTP test has something to write to.
            Directory.CreateDirectory(homePath);
            Directory.CreateDirectory(websitePath);

            // Don't allow users to create anything in base directory.
            FtpPermissions basePerms = new FtpPermissions();

            basePerms.DirectoryOpen = true;
            basePerms.DirectoryList = true;
            manager.SetGroupPermissions(group, new DirectoryInfo(homePath), basePerms);

            // Allow user to do anything in the website directory.
            manager.SetGroupPermissions(group, new DirectoryInfo(websitePath), FtpPermissions.AllowAll);

            bool   ftpFailed = false;
            string ftpError  = null;

            try
            {
                FtpClient client = new FtpClient(ftpServer, userName, password);
                client.Open();

                // Upload to mock website directory as no access is granted on root.
                client.ChangeWorkingDirectory(websiteDirectory);

                MemoryStream uploadStream = new MemoryStream();
                uploadStream.Write(Encoding.UTF8.GetBytes(uploadText), 0, uploadText.Length);
                client.Upload(uploadStream, uploadFileName, false);

                MemoryStream downloadStream = new MemoryStream();
                client.Download(uploadFileName, downloadStream);
                downloadText = Encoding.UTF8.GetString(downloadStream.ToArray());

                client.DeleteFile(uploadFileName);
                client.Close();
            }
            catch (Exception ex)
            {
                ftpFailed = true;
                ftpError  = ex.Message;
            }

            // Clean up first before asserting.
            manager.DeleteGroup(group, true);

            // Delete now we're finished.
            Directory.Delete(homePath, true);

            if (ftpFailed)
            {
                Assert.Fail("FTP test failed: " + ftpError);
            }
            else
            {
                // Ensure that user can write to home directory.
                Assert.AreEqual(uploadText, downloadText);
            }
        }