Beispiel #1
0
        public async Task <SaveWizardResult> CreateWizardUser([FromBody] CreateUserWizardModel user)
        {
            var users = UserManager.Users;
            // There could be a SINGLE plex user as you can create that in the wizard flow, but there should not be anything else
            var plexUsersCount = await users.CountAsync(x => x.UserType == UserType.PlexUser);

            if (plexUsersCount > 1 || users.Any(x => x.UserType == UserType.LocalUser))
            {
                // No one should be calling this. Only the wizard
                return(new SaveWizardResult {
                    Result = false, Errors = new List <string> {
                        "Looks like there is an existing user!"
                    }
                });
            }

            if (user.UsePlexAdminAccount)
            {
                var settings = await _plexSettings.GetSettingsAsync();

                var authToken = settings.Servers.FirstOrDefault()?.PlexAuthToken ?? string.Empty;
                if (authToken.IsNullOrEmpty())
                {
                    _log.LogWarning("Could not find an auth token to create the plex user with");
                    return(new SaveWizardResult {
                        Result = false
                    });
                }
                var plexUser = await _plexApi.GetAccount(authToken);

                var adminUser = new OmbiUser
                {
                    UserName         = plexUser.user.username,
                    UserType         = UserType.PlexUser,
                    Email            = plexUser.user.email,
                    ProviderUserId   = plexUser.user.id,
                    StreamingCountry = "US" // Default
                };

                await _userManagementSettings.SaveSettingsAsync(new UserManagementSettings
                {
                    ImportPlexAdmin = true
                });

                return(await SaveWizardUser(user, adminUser, false));
            }

            var userToCreate = new OmbiUser
            {
                UserName         = user.Username,
                UserType         = UserType.LocalUser,
                StreamingCountry = "US"
            };

            return(await SaveWizardUser(user, userToCreate, true));
        }
        public async Task CreateWizardUser_Should_CreateUser_WhenThereAreNoOtherUsers()
        {
            var model = new CreateUserWizardModel()
            {
                Password = "******",
                Username = "******"
            };

            var result = await Controller.CreateWizardUser(model);

            Assert.That(result, Is.True);
        }
Beispiel #3
0
        public async Task <SaveWizardResult> CreateWizardUser([FromBody] CreateUserWizardModel user)
        {
            var users = UserManager.Users;

            if (users.Any(x => !x.UserName.Equals("api", StringComparison.InvariantCultureIgnoreCase)))
            {
                // No one should be calling this. Only the wizard
                return(new SaveWizardResult {
                    Result = false, Errors = new List <string> {
                        "Looks like there is an existing user!"
                    }
                });
            }

            if (user.UsePlexAdminAccount)
            {
                var settings = await _plexSettings.GetSettingsAsync();

                var authToken = settings.Servers.FirstOrDefault()?.PlexAuthToken ?? string.Empty;
                if (authToken.IsNullOrEmpty())
                {
                    _log.LogWarning("Could not find an auth token to create the plex user with");
                    return(new SaveWizardResult {
                        Result = false
                    });
                }
                var plexUser = await _plexApi.GetAccount(authToken);

                var adminUser = new OmbiUser
                {
                    UserName       = plexUser.user.username,
                    UserType       = UserType.PlexUser,
                    Email          = plexUser.user.email,
                    ProviderUserId = plexUser.user.id
                };

                await _userManagementSettings.SaveSettingsAsync(new UserManagementSettings
                {
                    ImportPlexAdmin = true
                });

                return(await SaveWizardUser(user, adminUser));
            }

            var userToCreate = new OmbiUser
            {
                UserName = user.Username,
                UserType = UserType.LocalUser
            };

            return(await SaveWizardUser(user, userToCreate));
        }
Beispiel #4
0
        private async Task <SaveWizardResult> SaveWizardUser(CreateUserWizardModel user, OmbiUser userToCreate, bool completeWizard)
        {
            IdentityResult result;
            var            retVal = new SaveWizardResult();

            // When creating the admin as the plex user, we do not pass in the password.
            if (user.Password.HasValue())
            {
                result = await UserManager.CreateAsync(userToCreate, user.Password);
            }
            else
            {
                result = await UserManager.CreateAsync(userToCreate);
            }
            if (result.Succeeded)
            {
                _log.LogInformation("Created User {0}", userToCreate.UserName);
                await CreateRoles();

                _log.LogInformation("Created the roles");
                var roleResult = await UserManager.AddToRoleAsync(userToCreate, OmbiRoles.Admin);

                if (!roleResult.Succeeded)
                {
                    LogErrors(roleResult);
                }
                else
                {
                    _log.LogInformation("Added the Admin role");
                }

                if (completeWizard)
                {
                    // Update the wizard flag
                    var settings = await OmbiSettings.GetSettingsAsync();

                    settings.Wizard = true;
                    await OmbiSettings.SaveSettingsAsync(settings);
                }
            }
            if (!result.Succeeded)
            {
                LogErrors(result);
                retVal.Errors.AddRange(result.Errors.Select(x => x.Description));
            }

            retVal.Result = result.Succeeded;
            return(retVal);
        }
        public async Task CreateWizardUser_ShouldNot_CreateUser_WhenThereAreOtherUsers()
        {
            var um = _serviceProvider.GetService <OmbiUserManager>();
            var r  = await um.CreateAsync(new OmbiUser { UserName = "******", UserType = UserType.LocalUser }, "bbb");

            var model = new CreateUserWizardModel
            {
                Password = "******",
                Username = "******"
            };

            var result = await Controller.CreateWizardUser(model);

            Assert.That(result, Is.False);
        }
Beispiel #6
0
        public async Task <bool> CreateWizardUser([FromBody] CreateUserWizardModel user)
        {
            var users = UserManager.Users;

            if (users.Any())
            {
                // No one should be calling this. Only the wizard
                return(false);
            }

            if (user.UsePlexAdminAccount)
            {
                var settings = await _plexSettings.GetSettingsAsync();

                var authToken = settings.Servers.FirstOrDefault()?.PlexAuthToken ?? string.Empty;
                if (authToken.IsNullOrEmpty())
                {
                    _log.LogWarning("Could not find an auth token to create the plex user with");
                    return(false);
                }
                var plexUser = await _plexApi.GetAccount(authToken);

                var adminUser = new OmbiUser
                {
                    UserName       = plexUser.user.username,
                    UserType       = UserType.PlexUser,
                    Email          = plexUser.user.email,
                    ProviderUserId = plexUser.user.id
                };

                return(await SaveWizardUser(user, adminUser));
            }

            var userToCreate = new OmbiUser
            {
                UserName = user.Username,
                UserType = UserType.LocalUser
            };

            return(await SaveWizardUser(user, userToCreate));
        }
Beispiel #7
0
        private async Task <bool> SaveWizardUser(CreateUserWizardModel user, OmbiUser userToCreate)
        {
            IdentityResult result;

            // When creating the admin as the plex user, we do not pass in the password.
            if (user.Password.HasValue())
            {
                result = await UserManager.CreateAsync(userToCreate, user.Password);
            }
            else
            {
                result = await UserManager.CreateAsync(userToCreate);
            }
            if (result.Succeeded)
            {
                _log.LogInformation("Created User {0}", userToCreate.UserName);
                await CreateRoles();

                _log.LogInformation("Created the roles");
                var roleResult = await UserManager.AddToRoleAsync(userToCreate, OmbiRoles.Admin);

                if (!roleResult.Succeeded)
                {
                    LogErrors(roleResult);
                }
                else
                {
                    _log.LogInformation("Added the Admin role");
                }
            }
            if (!result.Succeeded)
            {
                LogErrors(result);
            }
            return(result.Succeeded);
        }