Example #1
0
        private void Cleanup()
        {
            try
            {
                var items         = Repo.GetAll();
                var ordered       = items.OrderByDescending(x => x.Date).ToList();
                var itemsToDelete = new List <LogEntity>();
                if (ordered.Count > ItemsToDelete)
                {
                    itemsToDelete = ordered.Skip(ItemsToDelete).ToList();
                }

                foreach (var o in itemsToDelete)
                {
                    Repo.Delete(o);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                JobRecord.Record(JobNames.StoreCleanup);
                JobRecord.SetRunning(false, JobNames.CpCacher);
            }
        }
Example #2
0
        public void Start()
        {
            JobRecord.SetRunning(true, JobNames.EmbyUserChecker);

            try
            {
                var settings = EmbySettings.GetSettings();
                if (string.IsNullOrEmpty(settings.ApiKey) || !settings.Enable)
                {
                    return;
                }
                var embyUsers = EmbyApi.GetUsers(settings.FullUri, settings.ApiKey);
                var userManagementSettings = UserManagementSettings.GetSettings();

                var dbUsers = Repo.GetAll().ToList();

                // Regular users
                foreach (var user in embyUsers)
                {
                    var dbUser = dbUsers.FirstOrDefault(x => x.EmbyUserId == user.Id);
                    if (dbUser != null)
                    {
                        // we already have a user
                        continue;
                    }

                    // Looks like it's a new user!
                    var m = new EmbyUsers
                    {
                        EmbyUserId  = user.Id,
                        Permissions = UserManagementHelper.GetPermissions(userManagementSettings),
                        Features    = UserManagementHelper.GetFeatures(userManagementSettings),
                        UserAlias   = string.Empty,
                        Username    = user.Name,
                        LoginId     = Guid.NewGuid().ToString()
                    };


                    // If it's the admin, give them the admin permission
                    if (user.Policy.IsAdministrator)
                    {
                        if (!((Permissions)m.Permissions).HasFlag(Permissions.Administrator))
                        {
                            m.Permissions += (int)Permissions.Administrator;
                        }
                    }

                    Repo.Insert(m);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                JobRecord.SetRunning(false, JobNames.EmbyUserChecker);
                JobRecord.Record(JobNames.EmbyUserChecker);
            }
        }
Example #3
0
 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);
     }
 }
Example #4
0
        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 mainPlexAccount        = PlexApi.GetAccount(settings.PlexAuthToken);
                var requests = RequestService.GetAll().ToList();

                var dbUsers    = Repo.GetAll().ToList();
                var localUsers = LocalUserRepository.GetAll().ToList();

                // Regular users
                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;

                        if (!string.IsNullOrEmpty(user.Username)) // If true then this is a managed user, we do not want to update the email since Managed Users do not have email addresses
                        {
                            // 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.Title, 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.Title, StringComparison.CurrentCultureIgnoreCase));
                                dbUser.Username = user.Title;
                                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.Title);   // 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.Title,
                        LoginId      = Guid.NewGuid().ToString()
                    };

                    Repo.Insert(m);
                }

                // Main Plex user
                var dbMainAcc    = dbUsers.FirstOrDefault(x => x.Username.Equals(mainPlexAccount.Username, StringComparison.CurrentCulture));
                var localMainAcc = localUsers.FirstOrDefault(x => x.UserName.Equals(mainPlexAccount.Username, StringComparison.CurrentCulture));

                // TODO if admin acc does exist, check if we need to update it


                // Create the local admin account if it doesn't already exist
                if (dbMainAcc == null && localMainAcc == null)
                {
                    var a = new PlexUsers
                    {
                        PlexUserId   = mainPlexAccount.Id,
                        Permissions  = UserManagementHelper.GetPermissions(userManagementSettings),
                        Features     = UserManagementHelper.GetFeatures(userManagementSettings),
                        UserAlias    = string.Empty,
                        EmailAddress = mainPlexAccount.Email,
                        Username     = mainPlexAccount.Username,
                        LoginId      = Guid.NewGuid().ToString()
                    };

                    a.Permissions += (int)Permissions.Administrator;  // Make admin

                    Repo.Insert(a);
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            finally
            {
                JobRecord.SetRunning(false, JobNames.PlexUserChecker);
                JobRecord.Record(JobNames.PlexUserChecker);
            }
        }
Example #5
0
 public void Execute(IJobExecutionContext context)
 {
     JobRecord.SetRunning(true, JobNames.CpCacher);
     Cleanup();
 }
Example #6
0
 public void Start()
 {
     JobRecord.SetRunning(true, JobNames.CpCacher);
     Cleanup();
 }