2014-11-24 07:46:22 +00:00
|
|
|
|
using System.Linq;
|
2014-02-16 09:56:12 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using FluentValidation;
|
2014-05-02 14:11:59 +00:00
|
|
|
|
using NzbDrone.Common.EnvironmentInfo;
|
2015-01-26 02:03:21 +00:00
|
|
|
|
using NzbDrone.Common.Extensions;
|
|
|
|
|
using NzbDrone.Core.Authentication;
|
2014-02-16 09:56:12 +00:00
|
|
|
|
using NzbDrone.Core.Configuration;
|
2014-04-28 06:34:29 +00:00
|
|
|
|
using NzbDrone.Core.Update;
|
2014-05-02 14:11:59 +00:00
|
|
|
|
using NzbDrone.Core.Validation;
|
2014-12-07 06:08:41 +00:00
|
|
|
|
using NzbDrone.Core.Validation.Paths;
|
2014-02-16 09:56:12 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Api.Config
|
|
|
|
|
{
|
|
|
|
|
public class HostConfigModule : NzbDroneRestModule<HostConfigResource>
|
|
|
|
|
{
|
|
|
|
|
private readonly IConfigFileProvider _configFileProvider;
|
2016-02-02 18:42:22 +00:00
|
|
|
|
private readonly IConfigService _configService;
|
2015-01-26 02:03:21 +00:00
|
|
|
|
private readonly IUserService _userService;
|
2014-02-16 09:56:12 +00:00
|
|
|
|
|
2016-02-02 18:42:22 +00:00
|
|
|
|
public HostConfigModule(IConfigFileProvider configFileProvider, IConfigService configService, IUserService userService)
|
2014-02-16 09:56:12 +00:00
|
|
|
|
: base("/config/host")
|
|
|
|
|
{
|
|
|
|
|
_configFileProvider = configFileProvider;
|
2016-02-02 18:42:22 +00:00
|
|
|
|
_configService = configService;
|
2015-01-26 02:03:21 +00:00
|
|
|
|
_userService = userService;
|
2014-02-16 09:56:12 +00:00
|
|
|
|
|
|
|
|
|
GetResourceSingle = GetHostConfig;
|
|
|
|
|
GetResourceById = GetHostConfig;
|
|
|
|
|
UpdateResource = SaveHostConfig;
|
|
|
|
|
|
2015-05-22 20:19:31 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.BindAddress)
|
|
|
|
|
.ValidIp4Address()
|
|
|
|
|
.NotListenAllIp4Address()
|
|
|
|
|
.When(c => c.BindAddress != "*");
|
|
|
|
|
|
2014-10-02 12:32:32 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.Port).ValidPort();
|
2014-10-01 18:34:10 +00:00
|
|
|
|
|
2015-05-22 20:19:31 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.UrlBase).ValidUrlBase();
|
|
|
|
|
|
2015-01-26 02:03:21 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.Username).NotEmpty().When(c => c.AuthenticationMethod != AuthenticationType.None);
|
|
|
|
|
SharedValidator.RuleFor(c => c.Password).NotEmpty().When(c => c.AuthenticationMethod != AuthenticationType.None);
|
2014-02-16 09:56:12 +00:00
|
|
|
|
|
2014-05-02 14:11:59 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.SslPort).ValidPort().When(c => c.EnableSsl);
|
|
|
|
|
SharedValidator.RuleFor(c => c.SslCertHash).NotEmpty().When(c => c.EnableSsl && OsInfo.IsWindows);
|
2014-04-28 06:34:29 +00:00
|
|
|
|
|
2015-05-22 20:19:31 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.Branch).NotEmpty().WithMessage("Branch name is required, 'master' is the default");
|
2014-04-28 06:34:29 +00:00
|
|
|
|
SharedValidator.RuleFor(c => c.UpdateScriptPath).IsValidPath().When(c => c.UpdateMechanism == UpdateMechanism.Script);
|
2014-02-16 09:56:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HostConfigResource GetHostConfig()
|
|
|
|
|
{
|
2016-03-25 00:56:29 +00:00
|
|
|
|
var resource = _configFileProvider.ToResource(_configService);
|
2014-02-16 09:56:12 +00:00
|
|
|
|
resource.Id = 1;
|
|
|
|
|
|
2015-01-26 02:03:21 +00:00
|
|
|
|
var user = _userService.FindUser();
|
|
|
|
|
if (user != null)
|
|
|
|
|
{
|
|
|
|
|
resource.Username = user.Username;
|
|
|
|
|
resource.Password = user.Password;
|
|
|
|
|
}
|
|
|
|
|
|
2014-02-16 09:56:12 +00:00
|
|
|
|
return resource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private HostConfigResource GetHostConfig(int id)
|
|
|
|
|
{
|
|
|
|
|
return GetHostConfig();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void SaveHostConfig(HostConfigResource resource)
|
|
|
|
|
{
|
|
|
|
|
var dictionary = resource.GetType()
|
|
|
|
|
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
|
|
|
|
|
.ToDictionary(prop => prop.Name, prop => prop.GetValue(resource, null));
|
|
|
|
|
|
|
|
|
|
_configFileProvider.SaveConfigDictionary(dictionary);
|
2016-02-02 18:42:22 +00:00
|
|
|
|
_configService.SaveConfigDictionary(dictionary);
|
2015-01-26 02:03:21 +00:00
|
|
|
|
|
|
|
|
|
if (resource.Username.IsNotNullOrWhiteSpace() && resource.Password.IsNotNullOrWhiteSpace())
|
|
|
|
|
{
|
|
|
|
|
_userService.Upsert(resource.Username, resource.Password);
|
|
|
|
|
}
|
2014-02-16 09:56:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-11-24 07:46:22 +00:00
|
|
|
|
}
|