From ce87d2e45c7e9e7f864be7ee4d682c5ff3a2258d Mon Sep 17 00:00:00 2001 From: rockstardev Date: Sat, 1 Aug 2020 09:17:17 -0500 Subject: [PATCH] Making use of options to initalize update check on first admin registration --- BTCPayServer/Controllers/AccountController.cs | 9 +---- .../Controllers/GreenField/UsersController.cs | 8 +--- .../Extensions/ActionLogicExtensions.cs | 39 +++++++++++++++++++ 3 files changed, 42 insertions(+), 14 deletions(-) create mode 100644 BTCPayServer/Extensions/ActionLogicExtensions.cs diff --git a/BTCPayServer/Controllers/AccountController.cs b/BTCPayServer/Controllers/AccountController.cs index 6888927e7..cac9f0e36 100644 --- a/BTCPayServer/Controllers/AccountController.cs +++ b/BTCPayServer/Controllers/AccountController.cs @@ -443,13 +443,8 @@ namespace BTCPayServer.Controllers var settings = await _SettingsRepository.GetSettingAsync(); settings.FirstRun = false; await _SettingsRepository.UpdateSetting(settings); - if (_Options.DisableRegistration) - { - // Once the admin user has been created lock subsequent user registrations (needs to be disabled for unit tests that require multiple users). - Logs.PayServer.LogInformation("First admin created, disabling subscription (disable-registration is set to true)"); - policies.LockSubscription = true; - await _SettingsRepository.UpdateSetting(policies); - } + + await _SettingsRepository.FirstAdminRegistered(policies, _Options.UpdateCheck, _Options.DisableRegistration); RegisteredAdmin = true; } diff --git a/BTCPayServer/Controllers/GreenField/UsersController.cs b/BTCPayServer/Controllers/GreenField/UsersController.cs index 94adf201f..8a3dbb9cb 100644 --- a/BTCPayServer/Controllers/GreenField/UsersController.cs +++ b/BTCPayServer/Controllers/GreenField/UsersController.cs @@ -148,13 +148,7 @@ namespace BTCPayServer.Controllers.GreenField await _userManager.AddToRoleAsync(user, Roles.ServerAdmin); if (!anyAdmin) { - if (_options.DisableRegistration) - { - // automatically lock subscriptions now that we have our first admin - Logs.PayServer.LogInformation("First admin created, disabling subscription (disable-registration is set to true)"); - policies.LockSubscription = true; - await _settingsRepository.UpdateSetting(policies); - } + await _settingsRepository.FirstAdminRegistered(policies, _options.UpdateCheck, _options.DisableRegistration); } } _eventAggregator.Publish(new UserRegisteredEvent() { RequestUri = Request.GetAbsoluteRootUri(), User = user, Admin = request.IsAdministrator is true }); diff --git a/BTCPayServer/Extensions/ActionLogicExtensions.cs b/BTCPayServer/Extensions/ActionLogicExtensions.cs new file mode 100644 index 000000000..9e5d247f2 --- /dev/null +++ b/BTCPayServer/Extensions/ActionLogicExtensions.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using BTCPayServer.Configuration; +using BTCPayServer.Logging; +using BTCPayServer.Services; +using Microsoft.Extensions.Logging; + +namespace BTCPayServer +{ + // All logic that would otherwise be duplicated across solution goes into this utility class + // ~If~ Once this starts growing out of control, begin extracting action logic classes out of here + // Also some of logic in here may be result of parallel development of Greenfield API + // It's much better that we extract those common methods then copy paste and maintain same code across codebase + internal static class ActionLogicExtensions + { + internal static async Task FirstAdminRegistered(this SettingsRepository settingsRepository, PoliciesSettings policies, + bool updateCheck, bool disableRegistrations) + { + if (updateCheck) + { + Logs.PayServer.LogInformation("First admin created, enabling checks for new versions"); + policies.CheckForNewVersions = updateCheck; + } + + if (disableRegistrations) + { + // Once the admin user has been created lock subsequent user registrations (needs to be disabled for unit tests that require multiple users). + Logs.PayServer.LogInformation("First admin created, disabling subscription (disable-registration is set to true)"); + policies.LockSubscription = true; + } + + if (updateCheck || disableRegistrations) + await settingsRepository.UpdateSetting(policies); + } + } +}