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