Skip to content

Commit

Permalink
fix(user-importer): don't delete admins in the cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tidusjar committed Jul 16, 2023
1 parent a34a4f7 commit 895b9bf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/Ombi.Schedule.Tests/PlexUserImporterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -368,9 +369,9 @@ public async Task Import_Cleanup_Missing_Plex_Users()

_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
Expand All @@ -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<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);
}
}
}
18 changes: 16 additions & 2 deletions src/Ombi.Schedule/Jobs/Plex/PlexUserImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 895b9bf

Please sign in to comment.