public async Task Execute(IJobExecutionContext job) { var userManagementSettings = await _userManagementSettings.GetSettingsAsync(); if (!userManagementSettings.ImportPlexUsers) { return; } var settings = await _plexSettings.GetSettingsAsync(); if (!settings.Enable) { return; } await _notification.Clients.Clients(NotificationHub.AdminConnectionIds) .SendAsync(NotificationHub.NotificationEvent, "Plex User Importer Started"); var allUsers = await _userManager.Users.Where(x => x.UserType == UserType.PlexUser).ToListAsync(); foreach (var server in settings.Servers) { if (string.IsNullOrEmpty(server.PlexAuthToken)) { continue; } await ImportAdmin(userManagementSettings, server, allUsers); var users = await _api.GetUsers(server.PlexAuthToken); foreach (var plexUser in users.User) { // Check if we should import this user if (userManagementSettings.BannedPlexUserIds.Contains(plexUser.Id)) { // Do not import these, they are not allowed into the country. continue; } // Check if this Plex User already exists // We are using the Plex USERNAME and Not the TITLE, the Title is for HOME USERS var existingPlexUser = allUsers.FirstOrDefault(x => x.ProviderUserId == plexUser.Id); if (existingPlexUser == null) { if (!plexUser.Username.HasValue()) { _log.LogInformation("Could not create Plex user since the have no username, PlexUserId: {0}", plexUser.Id); continue; } if ((plexUser.Email.HasValue()) && await _userManager.FindByEmailAsync(plexUser.Email) != null) { _log.LogWarning($"Cannot add user {plexUser.Username} because their email address is already in Ombi, skipping this user"); continue; } // Create this users // We do not store a password against the user since they will authenticate via Plex var newUser = new OmbiUser { UserType = UserType.PlexUser, UserName = plexUser?.Username ?? plexUser.Id, ProviderUserId = plexUser.Id, Email = plexUser?.Email ?? string.Empty, Alias = string.Empty, MovieRequestLimit = userManagementSettings.MovieRequestLimit, EpisodeRequestLimit = userManagementSettings.EpisodeRequestLimit, StreamingCountry = userManagementSettings.DefaultStreamingCountry }; _log.LogInformation("Creating Plex user {0}", newUser.UserName); var result = await _userManager.CreateAsync(newUser); if (!LogResult(result)) { continue; } if (userManagementSettings.DefaultRoles.Any()) { // Get the new user object to avoid any concurrency failures var dbUser = await _userManager.Users.FirstOrDefaultAsync(x => x.UserName == newUser.UserName); foreach (var defaultRole in userManagementSettings.DefaultRoles) { await _userManager.AddToRoleAsync(dbUser, defaultRole); } } } else { // Do we need to update this user? existingPlexUser.Email = plexUser.Email; existingPlexUser.UserName = plexUser.Username; await _userManager.UpdateAsync(existingPlexUser); } } } await _notification.Clients.Clients(NotificationHub.AdminConnectionIds) .SendAsync(NotificationHub.NotificationEvent, "Plex User Importer Finished"); }
public async Task Start() { var userManagementSettings = await _userManagementSettings.GetSettingsAsync(); if (!userManagementSettings.ImportPlexUsers) { return; } var settings = await _plexSettings.GetSettingsAsync(); if (!settings.Enable) { return; } var allUsers = await _userManager.Users.Where(x => x.UserType == UserType.PlexUser).ToListAsync(); foreach (var server in settings.Servers) { if (string.IsNullOrEmpty(server.PlexAuthToken)) { continue; } await ImportAdmin(userManagementSettings, server, allUsers); var users = await _api.GetUsers(server.PlexAuthToken); foreach (var plexUser in users.User) { // Check if we should import this user if (userManagementSettings.BannedPlexUserIds.Contains(plexUser.Id)) { // Do not import these, they are not allowed into the country. continue; } // Check if this Plex User already exists // We are using the Plex USERNAME and Not the TITLE, the Title is for HOME USERS var existingPlexUser = allUsers.FirstOrDefault(x => x.ProviderUserId == plexUser.Id); if (existingPlexUser == null) { // Create this users // We do not store a password against the user since they will authenticate via Plex var newUser = new OmbiUser { UserType = UserType.PlexUser, UserName = plexUser?.Username ?? plexUser.Id, ProviderUserId = plexUser.Id, Email = plexUser?.Email ?? string.Empty, Alias = string.Empty, MovieRequestLimit = userManagementSettings.MovieRequestLimit, EpisodeRequestLimit = userManagementSettings.EpisodeRequestLimit }; _log.LogInformation("Creating Plex user {0}", newUser.UserName); var result = await _userManager.CreateAsync(newUser); if (!LogResult(result)) { continue; } if (userManagementSettings.DefaultRoles.Any()) { // Get the new user object to avoid any concurrency failures var dbUser = await _userManager.Users.FirstOrDefaultAsync(x => x.UserName == newUser.UserName); foreach (var defaultRole in userManagementSettings.DefaultRoles) { await _userManager.AddToRoleAsync(dbUser, defaultRole); } } } else { // Do we need to update this user? existingPlexUser.Email = plexUser.Email; existingPlexUser.UserName = plexUser.Username; await _userManager.UpdateAsync(existingPlexUser); } } } }