From 70d07b830df016c403ab3161b395a5cf1718123f Mon Sep 17 00:00:00 2001 From: Jxiced Date: Fri, 21 Feb 2025 21:19:20 +0000 Subject: [PATCH 1/3] Prevent whitespaces in username during wizard setup. --- Jellyfin.Server.Implementations/Users/UserManager.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.cs index fba8923f89..91cf9c972f 100644 --- a/Jellyfin.Server.Implementations/Users/UserManager.cs +++ b/Jellyfin.Server.Implementations/Users/UserManager.cs @@ -176,6 +176,10 @@ namespace Jellyfin.Server.Implementations.Users /// public async Task UpdateUserAsync(User user) { + ArgumentNullException.ThrowIfNull(user); + + ThrowIfInvalidUsername(user.Username); + var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { From 7aa96dfc202db23fba7b402bd17d4854f9249d27 Mon Sep 17 00:00:00 2001 From: Jxiced Date: Sat, 22 Feb 2025 22:34:41 +0000 Subject: [PATCH 2/3] Update contributors. --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index ae1a2fd71e..271f1c1971 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -195,6 +195,7 @@ - [Kenneth Cochran](https://github.com/kennethcochran) - [benedikt257](https://github.com/benedikt257) - [revam](https://github.com/revam) + - [Jxiced](https://github.com/Jxiced) # Emby Contributors From e7bc86ebb8496615e0b3f73eb4f13ab4c0913dc8 Mon Sep 17 00:00:00 2001 From: Jxiced Date: Sun, 23 Feb 2025 22:16:35 +0000 Subject: [PATCH 3/3] Move throw into interface to use in wizard, check for null and invalid username. --- Jellyfin.Api/Controllers/StartupController.cs | 9 +++++---- Jellyfin.Server.Implementations/Users/UserManager.cs | 7 ++----- MediaBrowser.Controller/Library/IUserManager.cs | 6 ++++++ 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Jellyfin.Api/Controllers/StartupController.cs b/Jellyfin.Api/Controllers/StartupController.cs index a6bc84311f..b3b900ceb9 100644 --- a/Jellyfin.Api/Controllers/StartupController.cs +++ b/Jellyfin.Api/Controllers/StartupController.cs @@ -1,3 +1,4 @@ +using System; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; @@ -130,16 +131,16 @@ public class StartupController : BaseJellyfinApiController [ProducesResponseType(StatusCodes.Status204NoContent)] public async Task UpdateStartupUser([FromBody] StartupUserDto startupUserDto) { + ArgumentNullException.ThrowIfNull(startupUserDto.Name); + _userManager.ThrowIfInvalidUsername(startupUserDto.Name); + var user = _userManager.Users.First(); if (string.IsNullOrWhiteSpace(startupUserDto.Password)) { return BadRequest("Password must not be empty"); } - if (startupUserDto.Name is not null) - { - user.Username = startupUserDto.Name; - } + user.Username = startupUserDto.Name; await _userManager.UpdateUserAsync(user).ConfigureAwait(false); diff --git a/Jellyfin.Server.Implementations/Users/UserManager.cs b/Jellyfin.Server.Implementations/Users/UserManager.cs index 91cf9c972f..5a11fadc53 100644 --- a/Jellyfin.Server.Implementations/Users/UserManager.cs +++ b/Jellyfin.Server.Implementations/Users/UserManager.cs @@ -176,10 +176,6 @@ namespace Jellyfin.Server.Implementations.Users /// public async Task UpdateUserAsync(User user) { - ArgumentNullException.ThrowIfNull(user); - - ThrowIfInvalidUsername(user.Username); - var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); await using (dbContext.ConfigureAwait(false)) { @@ -737,7 +733,8 @@ namespace Jellyfin.Server.Implementations.Users _users[user.Id] = user; } - internal static void ThrowIfInvalidUsername(string name) + /// + public void ThrowIfInvalidUsername(string name) { if (!string.IsNullOrWhiteSpace(name) && ValidUsernameRegex().IsMatch(name)) { diff --git a/MediaBrowser.Controller/Library/IUserManager.cs b/MediaBrowser.Controller/Library/IUserManager.cs index 1c115be857..a4bd0f69ea 100644 --- a/MediaBrowser.Controller/Library/IUserManager.cs +++ b/MediaBrowser.Controller/Library/IUserManager.cs @@ -33,6 +33,12 @@ namespace MediaBrowser.Controller.Library /// The users ids. IEnumerable UsersIds { get; } + /// + /// Checks if the user's username is valid. + /// + /// The user's username. + void ThrowIfInvalidUsername(string name); + /// /// Initializes the user manager and ensures that a user exists. ///