protected void Page_Load(object sender, EventArgs e) { // wire the events GeneratePasswordButton.Click += new EventHandler(GeneratePasswordButton_Click); ForcePasswordButton.Click += new EventHandler(ForcePasswordButton_Click); // this page requires an authenticated user Master.ForceAuthentication(); // this page requires an authenticated administrator Master.ForceAdministrator(); // get the target user from the query string userName = WebConvert.ToString(Request.QueryString["ID"], ""); // create the configuration related objects webSettings = new WebSettings(); fz = new Filezilla(webSettings.Lookup("ConfigurationFile")); // load the user object fzu = fz.GetUser(userName); if (!Page.IsPostBack) { // load the display with the user values UsernameTitleLiteral.Text = fzu.Username; UsernameLabel.Text = fzu.Username; PasswordLabel.Text = fzu.Password; EnabledLabel.Text = (fzu.Enabled) ? "Yes" : "No"; HomeDirectoryLabel.Text = fzu.Home; // load the first permission FilezillaPermission fzp = fzu.Permissions[0]; DirectoryLabel.Text = fzp.Dir; AliasPathLabel.Text = fzp.AliasPath; IsHomeLabel.Text = (fzp.OptionIsHome) ? "Yes" : "No"; FilePermissionsLabel.Text = ""; FilePermissionsLabel.Text += DisplayPermission("Read", fzp.OptionFileRead ); FilePermissionsLabel.Text += DisplayPermission("Write", fzp.OptionFileWrite); FilePermissionsLabel.Text += DisplayPermission("Delete", fzp.OptionFileDelete); FilePermissionsLabel.Text += DisplayPermission("Append", fzp.OptionFileAppend); DirPermissionsLabel.Text = ""; DirPermissionsLabel.Text += DisplayPermission("List", fzp.OptionDirList); DirPermissionsLabel.Text += DisplayPermission("Create", fzp.OptionDirCreate); DirPermissionsLabel.Text += DisplayPermission("Delete", fzp.OptionDirDelete); DirPermissionsLabel.Text += DisplayPermission("Subdirs", fzp.OptionDirSubdirs); AutoCreateLabel.Text = (fzp.OptionAutoCreate) ? "Yes" : "No"; } }
/// <summary> /// Create a new user account /// </summary> /// <param name="username"></param> /// <param name="password"></param> /// <returns></returns> public bool CreateUser(string username, string password, bool enabled, bool fileRead, bool fileWrite, bool fileDelete, bool fileAppend, bool dirCreate, bool dirDelete, bool dirList, bool dirSubdirs, bool autoCreate) { WebSettings ws = new WebSettings(); try { // create the new user FilezillaUser fzu = new FilezillaUser(); fzu.Username = username; fzu.Password = password; fzu.Enabled = enabled; // define the user's home directory string homeDir = ws.Lookup("NewUserRoot") + "\\" + username; // create the directory if it doesn't already exist if (!System.IO.Directory.Exists(homeDir)) { System.IO.Directory.CreateDirectory(homeDir); } // create the user's home directory permission FilezillaPermission fzp = new FilezillaPermission(); fzp.Dir = homeDir; fzp.OptionIsHome = true; fzp.OptionFileRead = fileRead; fzp.OptionFileWrite = fileWrite; fzp.OptionFileDelete = fileDelete; fzp.OptionFileAppend = fileAppend; fzp.OptionDirCreate = dirCreate; fzp.OptionDirDelete = dirDelete; fzp.OptionDirList = dirList; fzp.OptionDirSubdirs = dirSubdirs; fzp.OptionAutoCreate = autoCreate; // add the home directory to the user8 fzu.Permissions.Add(fzp); // look up the user in the configuration XmlNode usersNode = config.SelectSingleNode("/FileZillaServer/Users"); XmlNode newUserNode = fzu.Create(config); usersNode.AppendChild(newUserNode); // notify the administrator AccountNotification("Create User", username, password); } catch (Exception) { return false; } return true; }
/// <summary> /// Add the users in the file to a list of users /// </summary> /// <param name="userList">list to receive users</param> /// <returns>count of users added</returns> public int AddUsersToList(ref List<FilezillaUser> userList) { int count = 0; /// get a collection of all users XmlNodeList users = config.SelectNodes("/FileZillaServer/Users/User"); // walk the collection of accessible folders foreach (XmlNode user in users) { FilezillaUser fzu = new FilezillaUser(); if (fzu.Read(user)) { userList.Add(fzu); count++; } } return count; }
/// <summary> /// Check to see if a valid user exists /// </summary> /// <param name="userName"></param> /// <returns></returns> public bool UserExists(string userName) { FilezillaUser fzu = new FilezillaUser(); // look up the user in the configuration XmlNode userNode = config.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + userName + "']"); return (userNode != null); }
/// <summary> /// Retrieve a user object by user name. This will throw if the /// user cannot be found /// </summary> /// <param name="userName">target user</param> /// <returns>filezilla user object</returns> public FilezillaUser GetUser(string userName) { FilezillaUser fzu = new FilezillaUser(); // look up the user in the configuration XmlNode userNode = config.SelectSingleNode("/FileZillaServer/Users/User[@Name='" + userName + "']"); if (!fzu.Read(userNode)) { throw new Exception("Unable to parse user record"); } return fzu; }