From 9d3f7de2bb7758dd527bff8da01ccb120b336dde Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Mon, 13 Apr 2020 16:34:12 -0400 Subject: [PATCH] Fix issues with updating users --- src/Tgstation.Server.Api/Models/ErrorCode.cs | 6 ++--- .../Controllers/UserController.cs | 24 +++++++++++-------- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/Tgstation.Server.Api/Models/ErrorCode.cs b/src/Tgstation.Server.Api/Models/ErrorCode.cs index a5b4db757f..808790fa81 100644 --- a/src/Tgstation.Server.Api/Models/ErrorCode.cs +++ b/src/Tgstation.Server.Api/Models/ErrorCode.cs @@ -111,10 +111,10 @@ namespace Tgstation.Server.Api.Models UserColonInName, /// - /// Attempted to create a with a whitespace . + /// Attempted to create a with a or whitespace . /// - [Description("User names cannot be whitespace!")] - UserWhitespaceName, + [Description("User's name is missing or invalid whitespace!")] + UserMissingName, /// /// Attempted to change a while it was . diff --git a/src/Tgstation.Server.Host/Controllers/UserController.cs b/src/Tgstation.Server.Host/Controllers/UserController.cs index c672a23912..d83e3dc3eb 100644 --- a/src/Tgstation.Server.Host/Controllers/UserController.cs +++ b/src/Tgstation.Server.Host/Controllers/UserController.cs @@ -65,13 +65,15 @@ namespace Tgstation.Server.Host.Controllers /// Check if a given has a valid specified. /// /// The to check. + /// If this is a new . /// if is valid, a otherwise. - BadRequestObjectResult CheckValidName(UserUpdate model) + BadRequestObjectResult CheckValidName(UserUpdate model, bool newUser) { - if(String.IsNullOrWhiteSpace(model.Name)) - return BadRequest(new ErrorMessage(ErrorCode.UserWhitespaceName)); + var userInvalidWithNullName = newUser && model.Name == null; + if (userInvalidWithNullName || (model.Name != null && String.IsNullOrWhiteSpace(model.Name))) + return BadRequest(new ErrorMessage(ErrorCode.UserMissingName)); - model.Name = model.Name.Trim(); + model.Name = model.Name?.Trim(); if (model.Name != null && model.Name.Contains(':', StringComparison.InvariantCulture)) return BadRequest(new ErrorMessage(ErrorCode.UserColonInName)); return null; @@ -82,15 +84,17 @@ namespace Tgstation.Server.Host.Controllers /// /// The user to update. /// The new password. + /// If this is for a new . /// on success, if is too short. - BadRequestObjectResult TrySetPassword(Models.User dbUser, string newPassword) + BadRequestObjectResult TrySetPassword(Models.User dbUser, string newPassword, bool newUser) { + newPassword = newPassword ?? String.Empty; if (newPassword.Length < generalConfiguration.MinimumPasswordLength) return BadRequest(new ErrorMessage(ErrorCode.UserPasswordLength) { AdditionalData = $"Required password length: {generalConfiguration.MinimumPasswordLength}" }); - cryptographySuite.SetUserPassword(dbUser, newPassword, true); + cryptographySuite.SetUserPassword(dbUser, newPassword, newUser); return null; } @@ -122,7 +126,7 @@ namespace Tgstation.Server.Host.Controllers if (!(model.Name == null ^ model.SystemIdentifier == null)) return BadRequest(new ErrorMessage(ErrorCode.UserMismatchNameSid)); - var fail = CheckValidName(model); + var fail = CheckValidName(model, true); if (fail != null) return fail; @@ -155,7 +159,7 @@ namespace Tgstation.Server.Host.Controllers } else { - var result = TrySetPassword(dbUser, model.Password); + var result = TrySetPassword(dbUser, model.Password, true); if (result != null) return result; } @@ -217,7 +221,7 @@ namespace Tgstation.Server.Host.Controllers if (model.Password != null) { - var result = TrySetPassword(originalUser, model.Password); + var result = TrySetPassword(originalUser, model.Password, false); if (result != null) return result; } @@ -229,7 +233,7 @@ namespace Tgstation.Server.Host.Controllers originalUser.AdministrationRights = RightsHelper.Clamp(model.AdministrationRights ?? originalUser.AdministrationRights.Value); originalUser.Enabled = model.Enabled ?? originalUser.Enabled.Value; - var fail = CheckValidName(model); + var fail = CheckValidName(model, false); if (fail != null) return fail;