Skip to content

Commit

Permalink
Fixed #3794
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Oct 10, 2020
1 parent c9de7c1 commit 0afe5de
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 60 deletions.
11 changes: 11 additions & 0 deletions src/Ombi.Core/Engine/IUserDeletionEngine.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Identity;
using Ombi.Store.Entities;
using System.Threading.Tasks;

namespace Ombi.Core.Engine
{
public interface IUserDeletionEngine
{
Task<IdentityResult> DeleteUser(OmbiUser userToDelete);
}
}
135 changes: 135 additions & 0 deletions src/Ombi.Core/Engine/UserDeletionEngine.cs
Original file line number Diff line number Diff line change
@@ -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<Issues> _issuesRepository;
private readonly IRepository<IssueComments> _issueCommentsRepository;
private readonly IRepository<RequestLog> _requestLogRepository;
private readonly IRepository<NotificationUserId> _notificationRepository;
private readonly IRepository<RequestSubscription> _requestSubscriptionRepository;
private readonly IRepository<UserNotificationPreferences> _userNotificationPreferences;
private readonly IRepository<UserQualityProfiles> _userQualityProfiles;
private readonly ITvRequestRepository _tvRepository;
private readonly IMusicRequestRepository _musicRepository;
private readonly IRepository<Votes> _voteRepository;
private readonly IRepository<MobileDevices> _mobileDevicesRepository;

public UserDeletionEngine(IMovieRequestRepository movieRepository,
OmbiUserManager userManager,
ITvRequestRepository tvRepository,
IMusicRequestRepository musicRepository,
IRepository<Issues> issueRepo,
IRepository<IssueComments> issueCommentsRepo,
IRepository<RequestLog> requestLogRepo,
IRepository<NotificationUserId> notificationidsRepo,
IRepository<RequestSubscription> requestSubRepository,
IRepository<UserNotificationPreferences> notificationPreferencesRepo,
IRepository<UserQualityProfiles> qualityProfilesRepo,
IRepository<Votes> voteRepository,
IRepository<MobileDevices> 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<IdentityResult> 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;
}
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions src/Ombi.DependencyInjection/IocExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static void RegisterEngines(this IServiceCollection services)
services.AddTransient<IVoteEngine, VoteEngine>();
services.AddTransient<IDemoMovieSearchEngine, DemoMovieSearchEngine>();
services.AddTransient<IDemoTvSearchEngine, DemoTvSearchEngine>();
services.AddTransient<IUserDeletionEngine, UserDeletionEngine>();
}

public static void RegisterEnginesV2(this IServiceCollection services)
Expand Down
66 changes: 6 additions & 60 deletions src/Ombi/Controllers/V1/IdentityController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public IdentityController(OmbiUserManager user, IMapper mapper, RoleManager<Iden
IMusicRequestRepository musicRepo,
IMovieRequestEngine movieRequestEngine,
ITvRequestEngine tvRequestEngine,
IMusicRequestEngine musicEngine)
IMusicRequestEngine musicEngine,
IUserDeletionEngine deletionEngine)
{
UserManager = user;
Mapper = mapper;
Expand All @@ -92,9 +93,11 @@ public IdentityController(OmbiUserManager user, IMapper mapper, RoleManager<Iden
_userNotificationPreferences = notificationPreferences;
_userQualityProfiles = userProfiles;
MusicRequestEngine = musicEngine;
_deletionEngine = deletionEngine;
}

private OmbiUserManager UserManager { get; }
private readonly IUserDeletionEngine _deletionEngine;
private RoleManager<IdentityRole> RoleManager { get; }
private IMapper Mapper { get; }
private IEmailProvider EmailProvider { get; }
Expand Down Expand Up @@ -655,73 +658,15 @@ public async Task<OmbiIdentityResult> DeleteUser(string userId)
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))
{
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);

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);
}
var result = await _deletionEngine.DeleteUser(userToDelete);

// 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
Expand Down Expand Up @@ -936,6 +881,7 @@ public async Task<List<UserNotificationPreferences>> GetUserPreferences(string u
NotificationAgent.Mobile,
NotificationAgent.Webhook
};

private async Task<List<UserNotificationPreferences>> GetPreferences(OmbiUser user)
{
var userPreferences = await _userNotificationPreferences.GetAll().Where(x => x.UserId == user.Id).ToListAsync();
Expand Down

0 comments on commit 0afe5de

Please sign in to comment.