Move throw into interface to use in wizard, check for null and invalid username.

This commit is contained in:
Jxiced 2025-02-23 22:16:35 +00:00
parent 7aa96dfc20
commit e7bc86ebb8
3 changed files with 13 additions and 9 deletions

View File

@ -1,3 +1,4 @@
using System;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -130,16 +131,16 @@ public class StartupController : BaseJellyfinApiController
[ProducesResponseType(StatusCodes.Status204NoContent)] [ProducesResponseType(StatusCodes.Status204NoContent)]
public async Task<ActionResult> UpdateStartupUser([FromBody] StartupUserDto startupUserDto) public async Task<ActionResult> UpdateStartupUser([FromBody] StartupUserDto startupUserDto)
{ {
ArgumentNullException.ThrowIfNull(startupUserDto.Name);
_userManager.ThrowIfInvalidUsername(startupUserDto.Name);
var user = _userManager.Users.First(); var user = _userManager.Users.First();
if (string.IsNullOrWhiteSpace(startupUserDto.Password)) if (string.IsNullOrWhiteSpace(startupUserDto.Password))
{ {
return BadRequest("Password must not be empty"); 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); await _userManager.UpdateUserAsync(user).ConfigureAwait(false);

View File

@ -176,10 +176,6 @@ namespace Jellyfin.Server.Implementations.Users
/// <inheritdoc/> /// <inheritdoc/>
public async Task UpdateUserAsync(User user) public async Task UpdateUserAsync(User user)
{ {
ArgumentNullException.ThrowIfNull(user);
ThrowIfInvalidUsername(user.Username);
var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false); var dbContext = await _dbProvider.CreateDbContextAsync().ConfigureAwait(false);
await using (dbContext.ConfigureAwait(false)) await using (dbContext.ConfigureAwait(false))
{ {
@ -737,7 +733,8 @@ namespace Jellyfin.Server.Implementations.Users
_users[user.Id] = user; _users[user.Id] = user;
} }
internal static void ThrowIfInvalidUsername(string name) /// <inheritdoc/>
public void ThrowIfInvalidUsername(string name)
{ {
if (!string.IsNullOrWhiteSpace(name) && ValidUsernameRegex().IsMatch(name)) if (!string.IsNullOrWhiteSpace(name) && ValidUsernameRegex().IsMatch(name))
{ {

View File

@ -33,6 +33,12 @@ namespace MediaBrowser.Controller.Library
/// <value>The users ids.</value> /// <value>The users ids.</value>
IEnumerable<Guid> UsersIds { get; } IEnumerable<Guid> UsersIds { get; }
/// <summary>
/// Checks if the user's username is valid.
/// </summary>
/// <param name="name">The user's username.</param>
void ThrowIfInvalidUsername(string name);
/// <summary> /// <summary>
/// Initializes the user manager and ensures that a user exists. /// Initializes the user manager and ensures that a user exists.
/// </summary> /// </summary>