Fix startup wizard.

This commit is contained in:
Patrick Barron 2020-06-09 14:01:21 -04:00
parent ce737c31ec
commit d105bc728d
3 changed files with 41 additions and 1 deletions

View File

@ -95,6 +95,8 @@ namespace Jellyfin.Api.Controllers
[HttpGet("User")]
public StartupUserDto GetFirstUser()
{
// TODO: Remove this method when startup wizard no longer requires an existing user.
_userManager.Initialize();
var user = _userManager.Users.First();
return new StartupUserDto
{
@ -115,7 +117,7 @@ namespace Jellyfin.Api.Controllers
user.Username = startupUserDto.Name;
_userManager.UpdateUser(user);
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
if (!string.IsNullOrEmpty(startupUserDto.Password))
{

View File

@ -520,6 +520,39 @@ namespace Jellyfin.Server.Implementations.Users
_defaultPasswordResetProvider = _passwordResetProviders.OfType<DefaultPasswordResetProvider>().First();
}
/// <inheritdoc />
public void Initialize()
{
// TODO: Refactor the startup wizard so that it doesn't require a user to already exist.
var dbContext = _dbProvider.CreateContext();
if (dbContext.Users.Any())
{
return;
}
var defaultName = Environment.UserName;
if (string.IsNullOrWhiteSpace(defaultName))
{
defaultName = "MyJellyfinUser";
}
_logger.LogWarning("No users, creating one with username {UserName}", defaultName);
if (!IsValidUsername(defaultName))
{
throw new ArgumentException("Provided username is not valid!", defaultName);
}
var newUser = CreateUser(defaultName);
newUser.SetPermission(PermissionKind.IsAdministrator, true);
newUser.SetPermission(PermissionKind.EnableContentDeletion, true);
newUser.SetPermission(PermissionKind.EnableRemoteControlOfOtherUsers, true);
dbContext.Users.Add(newUser);
dbContext.SaveChanges();
}
/// <inheritdoc/>
public NameIdPair[] GetAuthenticationProviders()
{

View File

@ -52,6 +52,11 @@ namespace MediaBrowser.Controller.Library
/// <value>The users ids.</value>
IEnumerable<Guid> UsersIds { get; }
/// <summary>
/// Initializes the user manager and ensures that a user exists.
/// </summary>
void Initialize();
/// <summary>
/// Gets a user by Id.
/// </summary>