public void Execute(IJobExecutionContext context) { try { var settings = NewsletterSettings.GetSettings(); if (!settings.SendRecentlyAddedEmail) { return; } JobRecord.SetRunning(true, JobNames.RecentlyAddedEmail); Start(settings); } catch (Exception e) { Log.Error(e); } finally { JobRecord.Record(JobNames.RecentlyAddedEmail); JobRecord.SetRunning(false, JobNames.RecentlyAddedEmail); } }
public void Execute(IJobExecutionContext context) { JobRecord.SetRunning(true, JobNames.CpCacher); Cleanup(); }
public void Execute(IJobExecutionContext context) { JobRecord.SetRunning(true, JobNames.PlexUserChecker); try { var settings = PlexSettings.GetSettings(); if (string.IsNullOrEmpty(settings.PlexAuthToken)) { return; } var plexUsers = PlexApi.GetUsers(settings.PlexAuthToken); var userManagementSettings = UserManagementSettings.GetSettings(); var requests = RequestService.GetAll().ToList(); var dbUsers = Repo.GetAll().ToList(); var localUsers = LocalUserRepository.GetAll().ToList(); foreach (var user in plexUsers.User) { var dbUser = dbUsers.FirstOrDefault(x => x.PlexUserId == user.Id); if (dbUser != null) { // We already have the user, let's check if they have updated any of their info. var needToUpdate = false; var usernameChanged = false; // Do we need up update any info? if (!dbUser.EmailAddress.Equals(user.Email, StringComparison.CurrentCultureIgnoreCase)) { dbUser.EmailAddress = user.Email; needToUpdate = true; } if (!dbUser.Username.Equals(user.Username, StringComparison.CurrentCultureIgnoreCase)) { needToUpdate = true; usernameChanged = true; } if (needToUpdate) { if (usernameChanged) { // The username has changed, let's check if the username matches any local users var localUser = localUsers.FirstOrDefault(x => x.UserName.Equals(user.Username, StringComparison.CurrentCultureIgnoreCase)); dbUser.Username = user.Username; if (localUser != null) { // looks like we have a local user with the same name... // We should delete the local user and the Plex user will become the master, // I am not going to update the Plex Users permissions as that could end up leading to a security vulnerability // Where anyone could change their Plex Username to the PR.Net server admins name and get all the admin permissions. LocalUserRepository.Delete(localUser); } // Since the username has changed, we need to update all requests with that username (unless we are using the alias! Since the alias won't change) if (string.IsNullOrEmpty(dbUser.UserAlias)) { // Update all requests var requestsWithThisUser = requests.Where(x => x.RequestedUsers.Contains(user.Username)).ToList(); foreach (var r in requestsWithThisUser) { r.RequestedUsers.Remove(user.Username); // Remove old r.RequestedUsers.Add(dbUser.Username); // Add new } if (requestsWithThisUser.Any()) { RequestService.BatchUpdate(requestsWithThisUser); } } } Repo.Update(dbUser); } continue; } // Looks like it's a new user! var m = new PlexUsers { PlexUserId = user.Id, Permissions = UserManagementHelper.GetPermissions(userManagementSettings), Features = UserManagementHelper.GetFeatures(userManagementSettings), UserAlias = string.Empty, EmailAddress = user.Email, Username = user.Username, LoginId = Guid.NewGuid().ToString() }; Repo.Insert(m); } } catch (Exception e) { Log.Error(e); } finally { JobRecord.SetRunning(false, JobNames.PlexUserChecker); JobRecord.Record(JobNames.PlexUserChecker); } }