Configuring the AuthenticationType from the WebUI will now work, just restart and NzbDrone will change the AuthenticationType on start.

This commit is contained in:
Mark McDowall 2011-10-07 17:17:46 -07:00
parent 2c3eff2741
commit e941aef9f2
4 changed files with 118 additions and 23 deletions

View File

@ -694,4 +694,14 @@
</handlers>
</system.webServer>
</location>
<location path="NZBDrone">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
<windowsAuthentication enabled="false" />
</authentication>
</security>
</system.webServer>
</location>
</configuration>

View File

@ -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
}
}

View File

@ -86,6 +86,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Application.cs" />
<Compile Include="Model\AuthenticationType.cs" />
<Compile Include="ProcessInfo.cs" />
<Compile Include="Providers\ConsoleProvider.cs" />
<Compile Include="Providers\DebuggerProvider.cs" />
@ -132,6 +133,7 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PreBuildEvent>

View File

@ -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);
}
}
}