fix(user-importer): don't delete admins in the cleanup

pull/4987/head
tidusjar 1 year ago
parent a34a4f7f78
commit 895b9bf6a0

@ -32,6 +32,7 @@ namespace Ombi.Schedule.Tests
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 @@ namespace Ombi.Schedule.Tests
_mocker.Verify<IUserDeletionEngine>(x => x.DeleteUser(It.Is<OmbiUser>(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<ISettingsService<UserManagementSettings>, Task<UserManagementSettings>>(x => x.GetSettingsAsync())
.ReturnsAsync(new UserManagementSettings
@ -387,22 +388,25 @@ namespace Ombi.Schedule.Tests
{
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<OmbiUserManager, Task<IdentityResult>>(x => x.CreateAsync(It.Is<OmbiUser>(x => x.UserName == "diff_username" && x.Email == "diff_email" && x.ProviderUserId == "diff_user_id" && x.UserType == UserType.PlexUser)))
.ReturnsAsync(IdentityResult.Success);
_mocker.Setup<OmbiUserManager, Task<IdentityResult>>(x => x.AddToRoleAsync(It.Is<OmbiUser>(x => x.UserName == "diff_username"), It.Is<string>(x => x == OmbiRoles.Admin)))
.ReturnsAsync(IdentityResult.Success);
_mocker.Setup<OmbiUserManager, Task<bool>>(x => x.IsInRoleAsync(It.Is<OmbiUser>(x => x.UserName == "Admin"), OmbiRoles.Admin)).ReturnsAsync(true);
await _subject.Execute(null);
_mocker.Verify<IUserDeletionEngine>(x => x.DeleteUser(It.Is<OmbiUser>(x => x.ProviderUserId == "PLEX_ID" && x.Email == "dupe" && x.UserName == "plex")), Times.Once);
_mocker.Verify<IUserDeletionEngine>(x => x.DeleteUser(It.Is<OmbiUser>(x => x.ProviderUserId == "ADMIN_ID" && x.Email == "ADMIN@ADMIN.CO" && x.UserName == "Admin")), Times.Never);
}
}
}

@ -84,8 +84,22 @@ namespace Ombi.Schedule.Jobs.Plex
// 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);

Loading…
Cancel
Save