Refactor startup user creation and add HomeSection config
Refactored user initialization to always ensure at least one user exists at startup. If no users are present, a default admin user (admin/jellyfin) is created automatically. Updated StartupController endpoints to handle cases where no user exists. Simplified UserManager.InitializeAsync logic. Added HomeSectionConfiguration for EF Core entity mapping. Includes minor project and assembly info updates.
This commit is contained in:
@@ -9,6 +9,7 @@ using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Jellyfin.Api.Constants;
|
||||
using Jellyfin.Api.Models.StartupDtos;
|
||||
using Jellyfin.Data;
|
||||
using MediaBrowser.Common.Api;
|
||||
using MediaBrowser.Common.Net;
|
||||
using MediaBrowser.Controller.Configuration;
|
||||
@@ -111,15 +112,18 @@ public class StartupController : BaseJellyfinApiController
|
||||
[HttpGet("User")]
|
||||
[HttpGet("FirstUser", Name = "GetFirstUser_2")]
|
||||
[ProducesResponseType(StatusCodes.Status200OK)]
|
||||
public async Task<StartupUserDto> GetFirstUser()
|
||||
public Task<StartupUserDto?> GetFirstUser()
|
||||
{
|
||||
// TODO: Remove this method when startup wizard no longer requires an existing user.
|
||||
await _userManager.InitializeAsync().ConfigureAwait(false);
|
||||
var user = _userManager.Users.First();
|
||||
return new StartupUserDto
|
||||
var user = _userManager.Users.FirstOrDefault();
|
||||
if (user is null)
|
||||
{
|
||||
return Task.FromResult<StartupUserDto?>(null);
|
||||
}
|
||||
|
||||
return Task.FromResult<StartupUserDto?>(new StartupUserDto
|
||||
{
|
||||
Name = user.Username
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -135,20 +139,33 @@ public class StartupController : BaseJellyfinApiController
|
||||
[ProducesResponseType(StatusCodes.Status204NoContent)]
|
||||
public async Task<ActionResult> UpdateStartupUser([FromBody] StartupUserDto startupUserDto)
|
||||
{
|
||||
await _userManager.InitializeAsync().ConfigureAwait(false);
|
||||
var user = _userManager.Users.First();
|
||||
if (string.IsNullOrWhiteSpace(startupUserDto.Password))
|
||||
{
|
||||
return BadRequest("Password must not be empty");
|
||||
}
|
||||
|
||||
if (startupUserDto.Name is not null)
|
||||
var user = _userManager.Users.FirstOrDefault();
|
||||
|
||||
// If no user exists, create the initial admin user
|
||||
if (user is null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(startupUserDto.Name))
|
||||
{
|
||||
return BadRequest("Username must not be empty when creating initial user");
|
||||
}
|
||||
|
||||
user = await _userManager.CreateUserAsync(startupUserDto.Name).ConfigureAwait(false);
|
||||
user.SetPermission(Jellyfin.Database.Implementations.Enums.PermissionKind.IsAdministrator, true);
|
||||
user.SetPermission(Jellyfin.Database.Implementations.Enums.PermissionKind.EnableContentDeletion, true);
|
||||
user.SetPermission(Jellyfin.Database.Implementations.Enums.PermissionKind.EnableRemoteControlOfOtherUsers, true);
|
||||
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
|
||||
}
|
||||
else if (startupUserDto.Name is not null)
|
||||
{
|
||||
user.Username = startupUserDto.Name;
|
||||
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
await _userManager.UpdateUserAsync(user).ConfigureAwait(false);
|
||||
|
||||
if (!string.IsNullOrEmpty(startupUserDto.Password))
|
||||
{
|
||||
await _userManager.ChangePassword(user, startupUserDto.Password).ConfigureAwait(false);
|
||||
|
||||
Reference in New Issue
Block a user