Always access config file via provider to utilize lock

pull/4220/head
Qstick 7 months ago
parent cdd683ae8f
commit 092e41264f

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

@ -22,6 +22,7 @@ namespace NzbDrone.Core.Configuration
public interface IConfigFileProvider : IHandleAsync<ApplicationStartedEvent>,
IExecute<ResetApiKeyCommand>
{
XDocument LoadConfigFile();
Dictionary<string, object> GetConfigDictionary();
void SaveConfigDictionary(Dictionary<string, object> configValues);
void EnsureDefaultConfigFile();
@ -342,7 +343,7 @@ namespace NzbDrone.Core.Configuration
SaveConfigFile(xDoc);
}
private XDocument LoadConfigFile()
public XDocument LoadConfigFile()
{
try
{

Loading…
Cancel
Save