pull/3816/head
tidusjar 4 years ago
parent c9de7c165c
commit 0afe5de211

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

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

@ -101,6 +101,7 @@ namespace Ombi.DependencyInjection
services.AddTransient<IVoteEngine, VoteEngine>();
services.AddTransient<IDemoMovieSearchEngine, DemoMovieSearchEngine>();
services.AddTransient<IDemoTvSearchEngine, DemoTvSearchEngine>();
services.AddTransient<IUserDeletionEngine, UserDeletionEngine>();
}
public static void RegisterEnginesV2(this IServiceCollection services)

@ -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<IdentityRole> 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);
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
@ -936,6 +881,7 @@ namespace Ombi.Controllers.V1
NotificationAgent.Mobile,
NotificationAgent.Webhook
};
private async Task<List<UserNotificationPreferences>> GetPreferences(OmbiUser user)
{
var userPreferences = await _userNotificationPreferences.GetAll().Where(x => x.UserId == user.Id).ToListAsync();

Loading…
Cancel
Save