diff --git a/src/NzbDrone.Core/Authentication/UserService.cs b/src/NzbDrone.Core/Authentication/UserService.cs index 73f70aa5b..00a7018de 100644 --- a/src/NzbDrone.Core/Authentication/UserService.cs +++ b/src/NzbDrone.Core/Authentication/UserService.cs @@ -1,7 +1,11 @@ using System; +using System.Linq; using NzbDrone.Common.Disk; using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.Extensions; +using NzbDrone.Core.Configuration; +using NzbDrone.Core.Lifecycle; +using NzbDrone.Core.Messaging.Events; namespace NzbDrone.Core.Authentication { @@ -15,17 +19,22 @@ namespace NzbDrone.Core.Authentication User FindUser(Guid identifier); } - public class UserService : IUserService + public class UserService : IUserService, IHandle { private readonly IUserRepository _repo; private readonly IAppFolderInfo _appFolderInfo; private readonly IDiskProvider _diskProvider; + private readonly IConfigFileProvider _configFileProvider; - public UserService(IUserRepository repo, IAppFolderInfo appFolderInfo, IDiskProvider diskProvider) + public UserService(IUserRepository repo, + IAppFolderInfo appFolderInfo, + IDiskProvider diskProvider, + IConfigFileProvider configFileProvider) { _repo = repo; _appFolderInfo = appFolderInfo; _diskProvider = diskProvider; + _configFileProvider = configFileProvider; } public User Add(string username, string password) @@ -93,5 +102,28 @@ namespace NzbDrone.Core.Authentication { return _repo.FindUser(identifier); } + + public void Handle(ApplicationStartedEvent message) + { + if (_repo.All().Any()) + { + return; + } + + var xDoc = _configFileProvider.LoadConfigFile(); + var config = xDoc.Descendants("Config").Single(); + var usernameElement = config.Descendants("Username").FirstOrDefault(); + var passwordElement = config.Descendants("Password").FirstOrDefault(); + + if (usernameElement == null || passwordElement == null) + { + return; + } + + var username = usernameElement.Value; + var password = passwordElement.Value; + + Add(username, password); + } } } diff --git a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs index 2eec12d00..b0d68d688 100644 --- a/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs +++ b/src/NzbDrone.Core/Configuration/ConfigFileProvider.cs @@ -22,6 +22,7 @@ namespace NzbDrone.Core.Configuration public interface IConfigFileProvider : IHandleAsync, IExecute { + XDocument LoadConfigFile(); Dictionary GetConfigDictionary(); void SaveConfigDictionary(Dictionary configValues); void EnsureDefaultConfigFile(); @@ -342,7 +343,7 @@ namespace NzbDrone.Core.Configuration SaveConfigFile(xDoc); } - private XDocument LoadConfigFile() + public XDocument LoadConfigFile() { try {