Clean unreachable store if user is deleted

This commit is contained in:
nicolas.dorier 2018-07-20 15:24:19 +09:00
parent 883cd41232
commit 96721e95a2
3 changed files with 22 additions and 12 deletions

View file

@ -4,6 +4,7 @@ using BTCPayServer.Models.ServerViewModels;
using BTCPayServer.Services; using BTCPayServer.Services;
using BTCPayServer.Services.Mails; using BTCPayServer.Services.Mails;
using BTCPayServer.Services.Rates; using BTCPayServer.Services.Rates;
using BTCPayServer.Services.Stores;
using BTCPayServer.Validations; using BTCPayServer.Validations;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
@ -25,14 +26,17 @@ namespace BTCPayServer.Controllers
private UserManager<ApplicationUser> _UserManager; private UserManager<ApplicationUser> _UserManager;
SettingsRepository _SettingsRepository; SettingsRepository _SettingsRepository;
private BTCPayRateProviderFactory _RateProviderFactory; private BTCPayRateProviderFactory _RateProviderFactory;
private StoreRepository _StoreRepository;
public ServerController(UserManager<ApplicationUser> userManager, public ServerController(UserManager<ApplicationUser> userManager,
BTCPayRateProviderFactory rateProviderFactory, BTCPayRateProviderFactory rateProviderFactory,
SettingsRepository settingsRepository) SettingsRepository settingsRepository,
Services.Stores.StoreRepository storeRepository)
{ {
_UserManager = userManager; _UserManager = userManager;
_SettingsRepository = settingsRepository; _SettingsRepository = settingsRepository;
_RateProviderFactory = rateProviderFactory; _RateProviderFactory = rateProviderFactory;
_StoreRepository = storeRepository;
} }
[Route("server/rates")] [Route("server/rates")]
@ -188,6 +192,7 @@ namespace BTCPayServer.Controllers
if (user == null) if (user == null)
return NotFound(); return NotFound();
await _UserManager.DeleteAsync(user); await _UserManager.DeleteAsync(user);
await _StoreRepository.CleanUnreachableStores();
StatusMessage = "User deleted"; StatusMessage = "User deleted";
return RedirectToAction(nameof(ListUsers)); return RedirectToAction(nameof(ListUsers));
} }

View file

@ -61,18 +61,9 @@ namespace BTCPayServer.HostedServices
} }
} }
private async Task UnreachableStoreCheck() private Task UnreachableStoreCheck()
{ {
using (var ctx = _DBContextFactory.CreateContext()) return _StoreRepository.CleanUnreachableStores();
{
if (!ctx.Database.SupportDropForeignKey())
return;
foreach (var store in await ctx.Stores.Where(s => s.UserStores.Where(u => u.Role == StoreRoles.Owner).Count() == 0).ToArrayAsync())
{
ctx.Stores.Remove(store);
}
await ctx.SaveChangesAsync();
}
} }
private async Task DeprecatedLightningConnectionStringCheck() private async Task DeprecatedLightningConnectionStringCheck()

View file

@ -112,6 +112,20 @@ namespace BTCPayServer.Services.Stores
} }
} }
public async Task CleanUnreachableStores()
{
using (var ctx = _ContextFactory.CreateContext())
{
if (!ctx.Database.SupportDropForeignKey())
return;
foreach (var store in await ctx.Stores.Where(s => s.UserStores.Where(u => u.Role == StoreRoles.Owner).Count() == 0).ToArrayAsync())
{
ctx.Stores.Remove(store);
}
await ctx.SaveChangesAsync();
}
}
public async Task RemoveStoreUser(string storeId, string userId) public async Task RemoveStoreUser(string storeId, string userId)
{ {
using (var ctx = _ContextFactory.CreateContext()) using (var ctx = _ContextFactory.CreateContext())