From e941aef9f2edc490a95dfbb84ebb8900785b4c2f Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Fri, 7 Oct 2011 17:17:46 -0700 Subject: [PATCH] Configuring the AuthenticationType from the WebUI will now work, just restart and NzbDrone will change the AuthenticationType on start. --- IISExpress/AppServer/applicationhost.config | 10 ++ NzbDrone/Model/AuthenticationType.cs | 13 +++ NzbDrone/NzbDrone.csproj | 2 + NzbDrone/Providers/ConfigProvider.cs | 116 ++++++++++++++++---- 4 files changed, 118 insertions(+), 23 deletions(-) create mode 100644 NzbDrone/Model/AuthenticationType.cs diff --git a/IISExpress/AppServer/applicationhost.config b/IISExpress/AppServer/applicationhost.config index 93f26b580..93879e48b 100644 --- a/IISExpress/AppServer/applicationhost.config +++ b/IISExpress/AppServer/applicationhost.config @@ -694,4 +694,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/NzbDrone/Model/AuthenticationType.cs b/NzbDrone/Model/AuthenticationType.cs new file mode 100644 index 000000000..243154383 --- /dev/null +++ b/NzbDrone/Model/AuthenticationType.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace NzbDrone.Model +{ + public enum AuthenticationType + { + Anonymous = 0, + Windows = 1 + } +} diff --git a/NzbDrone/NzbDrone.csproj b/NzbDrone/NzbDrone.csproj index b7f3cdc3f..0a0f3c79a 100644 --- a/NzbDrone/NzbDrone.csproj +++ b/NzbDrone/NzbDrone.csproj @@ -86,6 +86,7 @@ + @@ -132,6 +133,7 @@ true + diff --git a/NzbDrone/Providers/ConfigProvider.cs b/NzbDrone/Providers/ConfigProvider.cs index fe1524da6..4b65078cf 100644 --- a/NzbDrone/Providers/ConfigProvider.cs +++ b/NzbDrone/Providers/ConfigProvider.cs @@ -6,6 +6,7 @@ using System.Xml.Linq; using System.Xml.XPath; using NLog; using NLog.Config; +using NzbDrone.Model; namespace NzbDrone.Providers { @@ -31,12 +32,12 @@ namespace NzbDrone.Providers public virtual int Port { - get { return GetValueInt("Port"); } + get { return GetValueInt("Port", 8989); } } public virtual bool LaunchBrowser { - get { return GetValueBoolean("LaunchBrowser"); } + get { return GetValueBoolean("LaunchBrowser", true); } } public virtual string AppDataDirectory @@ -64,6 +65,12 @@ namespace NzbDrone.Providers get { return Path.Combine(IISFolder, "AppServer", "applicationhost.config"); } } + public virtual AuthenticationType AuthenticationType + { + get { return (AuthenticationType)GetValueInt("AuthenticationType", 0); } + set { SetValue("AuthenticationType", (int)value); } + } + public virtual void ConfigureNlog() { LogManager.Configuration = new XmlLoggingConfiguration( @@ -93,6 +100,25 @@ namespace NzbDrone.Providers new XAttribute("bindingInformation", String.Format("*:{0}:", Port)) )); + //Update the authenticationTypes + + var location = configXml.XPathSelectElement("configuration").Elements("location").Where( + d => d.Attribute("path").Value.ToLowerInvariant() == "nzbdrone").First(); + + + var authenticationTypes = location.XPathSelectElements("system.webServer/security/authentication").First().Descendants(); + + //Set all authentication types enabled to false + foreach (var child in authenticationTypes) + { + child.Attribute("enabled").Value = "false"; + } + + var configuredAuthType = String.Format("{0}Authentication", AuthenticationType.ToString()).ToLowerInvariant(); + + //Set the users authenticationType to true + authenticationTypes.Where(t => t.Name.ToString().ToLowerInvariant() == configuredAuthType).Single().Attribute("enabled").Value = "true"; + configXml.Save(configPath); } @@ -107,42 +133,86 @@ namespace NzbDrone.Providers } } - public virtual void WriteDefaultConfig() - { - var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); - - xDoc.Add(new XElement("Config", - new XElement("Port", 8989), - new XElement("LaunchBrowser", true) - ) - ); - - xDoc.Save(ConfigFile); - } - - private string GetValue(string key, string parent = null) + public virtual string GetValue(string key, object defaultValue, string parent = null) { var xDoc = XDocument.Load(ConfigFile); var config = xDoc.Descendants("Config").Single(); var parentContainer = config; - if (parent != null) + if (!String.IsNullOrEmpty(parent)) + { + //Add the parent + if (config.Descendants(parent).Count() != 1) + { + SetValue(key, defaultValue, parent); + + //Reload the configFile + xDoc = XDocument.Load(ConfigFile); + config = xDoc.Descendants("Config").Single(); + } + parentContainer = config.Descendants(parent).Single(); + } - string value = parentContainer.Descendants(key).Single().Value; + var valueHolder = parentContainer.Descendants(key).ToList(); - return value; + if (valueHolder.Count() == 1) + return valueHolder.First().Value; + + //Save the value + SetValue(key, defaultValue, parent); + + //return the default value + return defaultValue.ToString(); } - private int GetValueInt(string key, string parent = null) + public virtual int GetValueInt(string key, int defaultValue, string parent = null) { - return Convert.ToInt32(GetValue(key, parent)); + return Convert.ToInt32(GetValue(key, defaultValue, parent)); } - private bool GetValueBoolean(string key, string parent = null) + public virtual bool GetValueBoolean(string key, bool defaultValue, string parent = null) { - return Convert.ToBoolean(GetValue(key, parent)); + return Convert.ToBoolean(GetValue(key, defaultValue, parent)); + } + + public virtual void SetValue(string key, object value, string parent = null) + { + var xDoc = XDocument.Load(ConfigFile); + var config = xDoc.Descendants("Config").Single(); + + var parentContainer = config; + + if (!String.IsNullOrEmpty(parent)) + { + //Add the parent container if it doesn't already exist + if (config.Descendants(parent).Count() != 1) + { + config.Add(new XElement(parent)); + } + + parentContainer = config.Descendants(parent).Single(); + } + + var keyHolder = parentContainer.Descendants(key); + + if (keyHolder.Count() != 1) + parentContainer.Add(new XElement(key, value)); + + else + parentContainer.Descendants(key).Single().Value = value.ToString(); + + xDoc.Save(ConfigFile); + } + + public virtual void WriteDefaultConfig() + { + var xDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); + + xDoc.Add(new XElement("Config")); + + xDoc.Save(ConfigFile); } } } \ No newline at end of file