From 1b95ded829e8f182bbf9f6e396603f69263f8b64 Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Wed, 20 Dec 2023 15:03:02 -0500 Subject: [PATCH] Nullify `UserController` And fix some `.Any()`/`.Count` issues. --- .../Controllers/UserController.cs | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/Tgstation.Server.Host/Controllers/UserController.cs b/src/Tgstation.Server.Host/Controllers/UserController.cs index 8eed28d3f5..68bccfa169 100644 --- a/src/Tgstation.Server.Host/Controllers/UserController.cs +++ b/src/Tgstation.Server.Host/Controllers/UserController.cs @@ -22,8 +22,6 @@ using Tgstation.Server.Host.Models; using Tgstation.Server.Host.Security; using Tgstation.Server.Host.Utils; -#nullable disable - namespace Tgstation.Server.Host.Controllers { /// @@ -105,7 +103,7 @@ namespace Tgstation.Server.Host.Controllers return BadRequest(new ErrorMessageResponse(ErrorCode.ModelValidationFailure)); if ((model.Password != null && model.SystemIdentifier != null) - || (model.Password == null && model.SystemIdentifier == null && model.OAuthConnections?.Any() != true)) + || (model.Password == null && model.SystemIdentifier == null && (model.OAuthConnections?.Count > 0) != true)) return BadRequest(new ErrorMessageResponse(ErrorCode.UserMismatchPasswordSid)); if (model.Group != null && model.PermissionSet != null) @@ -146,14 +144,14 @@ namespace Tgstation.Server.Host.Controllers { return RequiresPosixSystemIdentity(ex); } - else if (!(model.Password?.Length == 0 && model.OAuthConnections?.Any() == true)) + else if (!(model.Password?.Length == 0 && (model.OAuthConnections?.Count > 0) == true)) { - var result = TrySetPassword(dbUser, model.Password, true); + var result = TrySetPassword(dbUser, model.Password!, true); if (result != null) return result; } - dbUser.CanonicalName = Models.User.CanonicalizeName(dbUser.Name); + dbUser.CanonicalName = Models.User.CanonicalizeName(dbUser.Name!); DatabaseContext.Users.Add(dbUser); @@ -206,7 +204,7 @@ namespace Tgstation.Server.Host.Controllers .Where(x => x.Id == model.Id) .Include(x => x.CreatedBy) .Include(x => x.OAuthConnections) - .Include(x => x.Group) + .Include(x => x.Group!) .ThenInclude(x => x.PermissionSet) .Include(x => x.PermissionSet) .FirstOrDefaultAsync(cancellationToken); @@ -256,7 +254,7 @@ namespace Tgstation.Server.Host.Controllers bool userWasDisabled; if (model.Enabled.HasValue) { - userWasDisabled = originalUser.Enabled.Value && !model.Enabled.Value; + userWasDisabled = originalUser.Require(x => x.Enabled) && !model.Enabled.Value; if (userWasDisabled) originalUser.LastPasswordUpdate = DateTimeOffset.UtcNow; @@ -266,7 +264,7 @@ namespace Tgstation.Server.Host.Controllers userWasDisabled = false; if (model.OAuthConnections != null - && (model.OAuthConnections.Count != originalUser.OAuthConnections.Count + && (model.OAuthConnections.Count != originalUser.OAuthConnections!.Count || !model.OAuthConnections.All(x => originalUser.OAuthConnections.Any(y => y.Provider == x.Provider && y.ExternalUserId == x.ExternalUserId)))) { if (originalUser.CanonicalName == Models.User.CanonicalizeName(DefaultCredentials.AdminUserName)) @@ -374,7 +372,7 @@ namespace Tgstation.Server.Host.Controllers .Include(x => x.CreatedBy) .Include(x => x.PermissionSet) .Include(x => x.OAuthConnections) - .Include(x => x.Group) + .Include(x => x.Group!) .ThenInclude(x => x.PermissionSet) .OrderBy(x => x.Id))), null, @@ -407,7 +405,7 @@ namespace Tgstation.Server.Host.Controllers .Where(x => x.Id == id) .Include(x => x.CreatedBy) .Include(x => x.OAuthConnections) - .Include(x => x.Group) + .Include(x => x.Group!) .ThenInclude(x => x.PermissionSet) .Include(x => x.PermissionSet) .FirstOrDefaultAsync(cancellationToken); @@ -428,8 +426,8 @@ namespace Tgstation.Server.Host.Controllers /// A resulting in a new on success, if the requested did not exist. async ValueTask CreateNewUserFromModel(Api.Models.Internal.UserApiBase model, CancellationToken cancellationToken) { - Models.PermissionSet permissionSet = null; - UserGroup group = null; + Models.PermissionSet? permissionSet = null; + UserGroup? group = null; if (model.Group != null) group = await DatabaseContext .Groups @@ -471,7 +469,7 @@ namespace Tgstation.Server.Host.Controllers /// The to check. /// If this is a new . /// if is valid, a otherwise. - BadRequestObjectResult CheckValidName(UserUpdateRequest model, bool newUser) + BadRequestObjectResult? CheckValidName(UserUpdateRequest model, bool newUser) { var userInvalidWithNullName = newUser && model.Name == null && model.SystemIdentifier == null; if (userInvalidWithNullName || (model.Name != null && String.IsNullOrWhiteSpace(model.Name))) @@ -490,7 +488,7 @@ namespace Tgstation.Server.Host.Controllers /// The new password. /// If this is for a new . /// on success, if is too short. - BadRequestObjectResult TrySetPassword(User dbUser, string newPassword, bool newUser) + BadRequestObjectResult? TrySetPassword(User dbUser, string newPassword, bool newUser) { newPassword ??= String.Empty; if (newPassword.Length < generalConfiguration.MinimumPasswordLength)