From 0afe5de211ad7eba0d9073f8dd6d2ad397b8c288 Mon Sep 17 00:00:00 2001 From: tidusjar Date: Sun, 11 Oct 2020 00:06:02 +0100 Subject: [PATCH] Fixed #3794 --- src/Ombi.Core/Engine/IUserDeletionEngine.cs | 11 ++ src/Ombi.Core/Engine/UserDeletionEngine.cs | 135 ++++++++++++++++++ .../Models}/IdentityResult.cs | 0 src/Ombi.DependencyInjection/IocExtensions.cs | 1 + src/Ombi/Controllers/V1/IdentityController.cs | 66 +-------- 5 files changed, 153 insertions(+), 60 deletions(-) create mode 100644 src/Ombi.Core/Engine/IUserDeletionEngine.cs create mode 100644 src/Ombi.Core/Engine/UserDeletionEngine.cs rename src/{Ombi/Models/Identity => Ombi.Core/Models}/IdentityResult.cs (100%) diff --git a/src/Ombi.Core/Engine/IUserDeletionEngine.cs b/src/Ombi.Core/Engine/IUserDeletionEngine.cs new file mode 100644 index 000000000..99719ae33 --- /dev/null +++ b/src/Ombi.Core/Engine/IUserDeletionEngine.cs @@ -0,0 +1,11 @@ +using Microsoft.AspNetCore.Identity; +using Ombi.Store.Entities; +using System.Threading.Tasks; + +namespace Ombi.Core.Engine +{ + public interface IUserDeletionEngine + { + Task DeleteUser(OmbiUser userToDelete); + } +} diff --git a/src/Ombi.Core/Engine/UserDeletionEngine.cs b/src/Ombi.Core/Engine/UserDeletionEngine.cs new file mode 100644 index 000000000..6ea1e794d --- /dev/null +++ b/src/Ombi.Core/Engine/UserDeletionEngine.cs @@ -0,0 +1,135 @@ +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; +using Ombi.Core.Authentication; +using Ombi.Store.Entities; +using Ombi.Store.Entities.Requests; +using Ombi.Store.Repository; +using Ombi.Store.Repository.Requests; +using System.Linq; +using System.Threading.Tasks; + +namespace Ombi.Core.Engine +{ + public class UserDeletionEngine : IUserDeletionEngine + { + private readonly IMovieRequestRepository _movieRepository; + private readonly OmbiUserManager _userManager; + private readonly IRepository _issuesRepository; + private readonly IRepository _issueCommentsRepository; + private readonly IRepository _requestLogRepository; + private readonly IRepository _notificationRepository; + private readonly IRepository _requestSubscriptionRepository; + private readonly IRepository _userNotificationPreferences; + private readonly IRepository _userQualityProfiles; + private readonly ITvRequestRepository _tvRepository; + private readonly IMusicRequestRepository _musicRepository; + private readonly IRepository _voteRepository; + private readonly IRepository _mobileDevicesRepository; + + public UserDeletionEngine(IMovieRequestRepository movieRepository, + OmbiUserManager userManager, + ITvRequestRepository tvRepository, + IMusicRequestRepository musicRepository, + IRepository issueRepo, + IRepository issueCommentsRepo, + IRepository requestLogRepo, + IRepository notificationidsRepo, + IRepository requestSubRepository, + IRepository notificationPreferencesRepo, + IRepository qualityProfilesRepo, + IRepository voteRepository, + IRepository mobileDevicesRepository + ) + { + _movieRepository = movieRepository; + _userManager = userManager; + _tvRepository = tvRepository; + _musicRepository = musicRepository; + _issuesRepository = issueRepo; + _issueCommentsRepository = issueCommentsRepo; + _notificationRepository = notificationidsRepo; + _requestLogRepository = requestLogRepo; + _requestSubscriptionRepository = requestSubRepository; + _notificationRepository = notificationidsRepo; + _userNotificationPreferences = notificationPreferencesRepo; + _userQualityProfiles = qualityProfilesRepo; + _voteRepository = voteRepository; + _mobileDevicesRepository = mobileDevicesRepository; + } + + + public async Task DeleteUser(OmbiUser userToDelete) + { + var userId = userToDelete.Id; + // We need to delete all the requests first + var moviesUserRequested = _movieRepository.GetAll().Where(x => x.RequestedUserId == userId); + var tvUserRequested = _tvRepository.GetChild().Where(x => x.RequestedUserId == userId); + var musicRequested = _musicRepository.GetAll().Where(x => x.RequestedUserId == userId); + var notificationPreferences = _userNotificationPreferences.GetAll().Where(x => x.UserId == userId); + var userQuality = await _userQualityProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == userId); + + if (moviesUserRequested.Any()) + { + await _movieRepository.DeleteRange(moviesUserRequested); + } + if (tvUserRequested.Any()) + { + await _tvRepository.DeleteChildRange(tvUserRequested); + } + if (musicRequested.Any()) + { + await _musicRepository.DeleteRange(musicRequested); + } + if (notificationPreferences.Any()) + { + await _userNotificationPreferences.DeleteRange(notificationPreferences); + } + if (userQuality != null) + { + await _userQualityProfiles.Delete(userQuality); + } + + // Delete any issues and request logs + var issues = _issuesRepository.GetAll().Where(x => x.UserReportedId == userId); + var issueComments = _issueCommentsRepository.GetAll().Where(x => x.UserId == userId); + var requestLog = _requestLogRepository.GetAll().Where(x => x.UserId == userId); + if (issues.Any()) + { + await _issuesRepository.DeleteRange(issues); + } + if (requestLog.Any()) + { + await _requestLogRepository.DeleteRange(requestLog); + } + if (issueComments.Any()) + { + await _issueCommentsRepository.DeleteRange(issueComments); + } + + // Delete the Subscriptions and mobile notification ids + var subs = _requestSubscriptionRepository.GetAll().Where(x => x.UserId == userId); + var mobileIds = _notificationRepository.GetAll().Where(x => x.UserId == userId); + var votes = _voteRepository.GetAll().Where(x => x.UserId == userId); + var newMobiles = _mobileDevicesRepository.GetAll().Where(x => x.UserId == userId); + if (subs.Any()) + { + await _requestSubscriptionRepository.DeleteRange(subs); + } + if (mobileIds.Any()) + { + await _notificationRepository.DeleteRange(mobileIds); + } + if (votes.Any()) + { + await _voteRepository.DeleteRange(votes); + } + if (newMobiles.Any()) + { + await _mobileDevicesRepository.DeleteRange(newMobiles); + } + + var result = await _userManager.DeleteAsync(userToDelete); + return result; + } + } +} diff --git a/src/Ombi/Models/Identity/IdentityResult.cs b/src/Ombi.Core/Models/IdentityResult.cs similarity index 100% rename from src/Ombi/Models/Identity/IdentityResult.cs rename to src/Ombi.Core/Models/IdentityResult.cs diff --git a/src/Ombi.DependencyInjection/IocExtensions.cs b/src/Ombi.DependencyInjection/IocExtensions.cs index 50189aa40..b8b7c6e69 100644 --- a/src/Ombi.DependencyInjection/IocExtensions.cs +++ b/src/Ombi.DependencyInjection/IocExtensions.cs @@ -101,6 +101,7 @@ namespace Ombi.DependencyInjection services.AddTransient(); services.AddTransient(); services.AddTransient(); + services.AddTransient(); } public static void RegisterEnginesV2(this IServiceCollection services) diff --git a/src/Ombi/Controllers/V1/IdentityController.cs b/src/Ombi/Controllers/V1/IdentityController.cs index fa6e98d3a..b348c1d06 100644 --- a/src/Ombi/Controllers/V1/IdentityController.cs +++ b/src/Ombi/Controllers/V1/IdentityController.cs @@ -65,7 +65,8 @@ namespace Ombi.Controllers.V1 IMusicRequestRepository musicRepo, IMovieRequestEngine movieRequestEngine, ITvRequestEngine tvRequestEngine, - IMusicRequestEngine musicEngine) + IMusicRequestEngine musicEngine, + IUserDeletionEngine deletionEngine) { UserManager = user; Mapper = mapper; @@ -92,9 +93,11 @@ namespace Ombi.Controllers.V1 _userNotificationPreferences = notificationPreferences; _userQualityProfiles = userProfiles; MusicRequestEngine = musicEngine; + _deletionEngine = deletionEngine; } private OmbiUserManager UserManager { get; } + private readonly IUserDeletionEngine _deletionEngine; private RoleManager RoleManager { get; } private IMapper Mapper { get; } private IEmailProvider EmailProvider { get; } @@ -655,7 +658,6 @@ namespace Ombi.Controllers.V1 var userToDelete = await UserManager.Users.FirstOrDefaultAsync(x => x.Id == userId); if (userToDelete != null) { - // Can we delete this user? var userRoles = await UserManager.GetRolesAsync(userToDelete); if (!CanModifyUser(userRoles)) @@ -663,65 +665,8 @@ namespace Ombi.Controllers.V1 return Error("You do not have the correct permissions to delete this user"); } - // We need to delete all the requests first - var moviesUserRequested = MovieRepo.GetAll().Where(x => x.RequestedUserId == userId); - var tvUserRequested = TvRepo.GetChild().Where(x => x.RequestedUserId == userId); - var musicRequested = MusicRepo.GetAll().Where(x => x.RequestedUserId == userId); - var notificationPreferences = _userNotificationPreferences.GetAll().Where(x => x.UserId == userId); - var userQuality = await _userQualityProfiles.GetAll().FirstOrDefaultAsync(x => x.UserId == userId); + var result = await _deletionEngine.DeleteUser(userToDelete); - if (moviesUserRequested.Any()) - { - await MovieRepo.DeleteRange(moviesUserRequested); - } - if (tvUserRequested.Any()) - { - await TvRepo.DeleteChildRange(tvUserRequested); - } - if (musicRequested.Any()) - { - await MusicRepo.DeleteRange(musicRequested); - } - if (notificationPreferences.Any()) - { - await _userNotificationPreferences.DeleteRange(notificationPreferences); - } - if (userQuality != null) - { - await _userQualityProfiles.Delete(userQuality); - } - - // Delete any issues and request logs - var issues = _issuesRepository.GetAll().Where(x => x.UserReportedId == userId); - var issueComments = _issueCommentsRepository.GetAll().Where(x => x.UserId == userId); - var requestLog = _requestLogRepository.GetAll().Where(x => x.UserId == userId); - if (issues.Any()) - { - await _issuesRepository.DeleteRange(issues); - } - if (requestLog.Any()) - { - await _requestLogRepository.DeleteRange(requestLog); - } - if (issueComments.Any()) - { - await _issueCommentsRepository.DeleteRange(issueComments); - } - - // Delete the Subscriptions and mobile notification ids - var subs = _requestSubscriptionRepository.GetAll().Where(x => x.UserId == userId); - var mobileIds = _notificationRepository.GetAll().Where(x => x.UserId == userId); - if (subs.Any()) - { - await _requestSubscriptionRepository.DeleteRange(subs); - } - - if (mobileIds.Any()) - { - await _notificationRepository.DeleteRange(mobileIds); - } - - var result = await UserManager.DeleteAsync(userToDelete); if (result.Succeeded) { return new OmbiIdentityResult @@ -936,6 +881,7 @@ namespace Ombi.Controllers.V1 NotificationAgent.Mobile, NotificationAgent.Webhook }; + private async Task> GetPreferences(OmbiUser user) { var userPreferences = await _userNotificationPreferences.GetAll().Where(x => x.UserId == user.Id).ToListAsync();