From 3fc08ccb510be22b38f7acd42e42bfc6f2084132 Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Sun, 22 Sep 2024 01:14:43 -0400 Subject: [PATCH] Fix cyclomatic complexity warning --- .../Authority/UserAuthority.cs | 112 ++++++++++++------ 1 file changed, 74 insertions(+), 38 deletions(-) diff --git a/src/Tgstation.Server.Host/Authority/UserAuthority.cs b/src/Tgstation.Server.Host/Authority/UserAuthority.cs index b670c01243..c3002ed288 100644 --- a/src/Tgstation.Server.Host/Authority/UserAuthority.cs +++ b/src/Tgstation.Server.Host/Authority/UserAuthority.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Linq; using System.Threading; using System.Threading.Tasks; @@ -159,6 +160,75 @@ namespace Tgstation.Server.Host.Authority this.generalConfigurationOptions = generalConfigurationOptions ?? throw new ArgumentNullException(nameof(generalConfigurationOptions)); } + /// + /// Checks if a should return a bad request . + /// + /// The to check. + /// If a zero-length indicates and OAuth only user. + /// The output failing , if any. + /// if checks failed and was populated, otherwise. + static bool BadCreateRequestChecks( + UserCreateRequest createRequest, + bool? needZeroLengthPasswordWithOAuthConnections, + [NotNullWhen(true)] out AuthorityResponse? failResponse) + { + if (createRequest.OAuthConnections?.Any(x => x == null) == true) + { + failResponse = BadRequest(ErrorCode.ModelValidationFailure); + return true; + } + + var hasNonNullPassword = createRequest.Password != null; + var hasNonNullSystemIdentifier = createRequest.SystemIdentifier != null; + var hasOAuthConnections = (createRequest.OAuthConnections?.Count > 0) == true; + if ((hasNonNullPassword && hasNonNullSystemIdentifier) + || (!hasNonNullPassword && !hasNonNullSystemIdentifier && !hasOAuthConnections)) + { + failResponse = BadRequest(ErrorCode.UserMismatchPasswordSid); + return true; + } + + var hasZeroLengthPassword = createRequest.Password?.Length == 0; + if (needZeroLengthPasswordWithOAuthConnections.HasValue) + { + if (needZeroLengthPasswordWithOAuthConnections.Value) + { + if (createRequest.OAuthConnections == null) + throw new InvalidOperationException($"Expected {nameof(UserCreateRequest.OAuthConnections)} to be set here!"); + + if (createRequest.OAuthConnections.Count == 0) + { + failResponse = BadRequest(ErrorCode.ModelValidationFailure); + return true; + } + } + else if (hasZeroLengthPassword) + { + failResponse = BadRequest(ErrorCode.ModelValidationFailure); + return true; + } + } + + if (createRequest.Group != null && createRequest.PermissionSet != null) + { + failResponse = BadRequest(ErrorCode.UserGroupAndPermissionSet); + return true; + } + + createRequest.Name = createRequest.Name?.Trim(); + if (createRequest.Name?.Length == 0) + createRequest.Name = null; + + if (!(createRequest.Name == null ^ createRequest.SystemIdentifier == null)) + { + failResponse = BadRequest(ErrorCode.UserMismatchNameSid); + return true; + } + + failResponse = CheckValidName(createRequest, true); + return failResponse != null; + } + /// public ValueTask> Read(CancellationToken cancellationToken) => ValueTask.FromResult(new AuthorityResponse(AuthenticationContext.User)); @@ -207,44 +277,8 @@ namespace Tgstation.Server.Host.Authority { ArgumentNullException.ThrowIfNull(createRequest); - if (createRequest.OAuthConnections?.Any(x => x == null) == true) - return BadRequest(ErrorCode.ModelValidationFailure); - - var hasNonNullPassword = createRequest.Password != null; - var hasNonNullSystemIdentifier = createRequest.SystemIdentifier != null; - var hasOAuthConnections = (createRequest.OAuthConnections?.Count > 0) == true; - if ((hasNonNullPassword && hasNonNullSystemIdentifier) - || (!hasNonNullPassword && !hasNonNullSystemIdentifier && !hasOAuthConnections)) - return BadRequest(ErrorCode.UserMismatchPasswordSid); - - var hasZeroLengthPassword = createRequest.Password?.Length == 0; - if (needZeroLengthPasswordWithOAuthConnections.HasValue) - { - if (needZeroLengthPasswordWithOAuthConnections.Value) - { - if (createRequest.OAuthConnections == null) - throw new InvalidOperationException($"Expected {nameof(UserCreateRequest.OAuthConnections)} to be set here!"); - - if (createRequest.OAuthConnections.Count == 0) - return BadRequest(ErrorCode.ModelValidationFailure); - } - else if (hasZeroLengthPassword) - return BadRequest(ErrorCode.ModelValidationFailure); - } - - if (createRequest.Group != null && createRequest.PermissionSet != null) - return BadRequest(ErrorCode.UserGroupAndPermissionSet); - - createRequest.Name = createRequest.Name?.Trim(); - if (createRequest.Name?.Length == 0) - createRequest.Name = null; - - if (!(createRequest.Name == null ^ createRequest.SystemIdentifier == null)) - return BadRequest(ErrorCode.UserMismatchNameSid); - - var fail = CheckValidName(createRequest, true); - if (fail != null) - return fail; + if (BadCreateRequestChecks(createRequest, needZeroLengthPasswordWithOAuthConnections, out var failResponse)) + return failResponse; var totalUsers = await DatabaseContext .Users @@ -275,6 +309,8 @@ namespace Tgstation.Server.Host.Authority } else { + var hasZeroLengthPassword = createRequest.Password?.Length == 0; + var hasOAuthConnections = (createRequest.OAuthConnections?.Count > 0) == true; if (!(needZeroLengthPasswordWithOAuthConnections != false && hasZeroLengthPassword && hasOAuthConnections)) // special case allow PasswordHash to be null by setting Password to "" if OAuthConnections are set { var result = TrySetPassword(dbUser, createRequest.Password!, true);