diff --git a/src/Ombi.Schedule.Tests/PlexUserImporterTests.cs b/src/Ombi.Schedule.Tests/PlexUserImporterTests.cs index 39443ea55..ff879bf98 100644 --- a/src/Ombi.Schedule.Tests/PlexUserImporterTests.cs +++ b/src/Ombi.Schedule.Tests/PlexUserImporterTests.cs @@ -32,6 +32,7 @@ public class PlexUserImporterTests new OmbiUser { Id = Guid.NewGuid().ToString("N"), UserName="abc", NormalizedUserName = "ABC", UserType = UserType.LocalUser}, new OmbiUser { Id = Guid.NewGuid().ToString("N"), UserName="sys", NormalizedUserName = "SYS", UserType = UserType.SystemUser}, new OmbiUser { Id = Guid.NewGuid().ToString("N"), UserName="plex", NormalizedUserName = "PLEX", UserType = UserType.PlexUser, ProviderUserId = "PLEX_ID", Email = "dupe"}, + new OmbiUser { Id = Guid.NewGuid().ToString("N"), UserName="Admin", NormalizedUserName = "ADMIN", UserType = UserType.PlexUser, ProviderUserId = "ADMIN_ID", Email = "ADMIN@ADMIN.CO"}, }; private AutoMocker _mocker; private PlexUserImporter _subject; @@ -368,9 +369,9 @@ public async Task Import_Cleanup_Missing_Plex_Users() _mocker.Verify(x => x.DeleteUser(It.Is(x => x.ProviderUserId == "PLEX_ID" && x.Email == "dupe" && x.UserName == "plex")), Times.Once); } - + [Test] - public async Task Import_Cleanup_Missing_Plex_Admin() + public async Task Import_Cleanup_Missing_Plex_Admin_Dont_Delete() { _mocker.Setup, Task>(x => x.GetSettingsAsync()) .ReturnsAsync(new UserManagementSettings @@ -387,22 +388,25 @@ public async Task Import_Cleanup_Missing_Plex_Admin() { user = new User { - email = "diff_email", - authentication_token = "user_token", - title = "user_title", - username = "diff_username", - id = "diff_user_id", + email = "ADMIN@ADMIN.CO", + authentication_token = "Admin", + title = "Admin", + username = "Admin", + id = "ADMIN_ID", } }); + _mocker.Setup>(x => x.CreateAsync(It.Is(x => x.UserName == "diff_username" && x.Email == "diff_email" && x.ProviderUserId == "diff_user_id" && x.UserType == UserType.PlexUser))) .ReturnsAsync(IdentityResult.Success); _mocker.Setup>(x => x.AddToRoleAsync(It.Is(x => x.UserName == "diff_username"), It.Is(x => x == OmbiRoles.Admin))) .ReturnsAsync(IdentityResult.Success); + _mocker.Setup>(x => x.IsInRoleAsync(It.Is(x => x.UserName == "Admin"), OmbiRoles.Admin)).ReturnsAsync(true); + await _subject.Execute(null); - _mocker.Verify(x => x.DeleteUser(It.Is(x => x.ProviderUserId == "PLEX_ID" && x.Email == "dupe" && x.UserName == "plex")), Times.Once); + _mocker.Verify(x => x.DeleteUser(It.Is(x => x.ProviderUserId == "ADMIN_ID" && x.Email == "ADMIN@ADMIN.CO" && x.UserName == "Admin")), Times.Never); } } } diff --git a/src/Ombi.Schedule/Jobs/Plex/PlexUserImporter.cs b/src/Ombi.Schedule/Jobs/Plex/PlexUserImporter.cs index d1bbd9e72..33149489d 100644 --- a/src/Ombi.Schedule/Jobs/Plex/PlexUserImporter.cs +++ b/src/Ombi.Schedule/Jobs/Plex/PlexUserImporter.cs @@ -84,8 +84,22 @@ public async Task Execute(IJobExecutionContext job) // Refresh users from updates allUsers = await _userManager.Users.Where(x => x.UserType == UserType.PlexUser) .ToListAsync(); - var missingUsers = allUsers - .Where(x => !newOrUpdatedUsers.Contains(x)); + + var missingUsers = allUsers + .Where(x => !newOrUpdatedUsers.Contains(x)).ToList(); + + // Don't delete any admins + for (int i = missingUsers.Count() - 1; i >= 0; i--) + { + var isAdmin = await _userManager.IsInRoleAsync(missingUsers[i], OmbiRoles.Admin); + if (!isAdmin) + { + continue; + } + + missingUsers.RemoveAt(i); + } + foreach (var ombiUser in missingUsers) { _log.LogInformation("Deleting user {0} not found in Plex Server.", ombiUser.UserName);