Better make sure users with no password can be created

This commit is contained in:
Jordan Brown
2020-12-16 11:12:07 -05:00
parent bcff5440c4
commit 7daeaed13c
2 changed files with 12 additions and 2 deletions
@@ -618,5 +618,11 @@ namespace Tgstation.Server.Api.Models
/// </summary>
[Description("The " + Routes.UserGroup + " endpoint cannot edit group members. Please update each member user individually.")]
UserGroupControllerCantEditMembers,
/// <summary>
/// Tried to remove the last <see cref="OAuthConnection"/> for a passwordless <see cref="User"/>.
/// </summary>
[Description("This user is passwordless and removing their oAuthConnections would leave them with no authentication method!")]
CannotRemoveLastAuthenticationOption,
}
}
@@ -121,7 +121,8 @@ namespace Tgstation.Server.Host.Controllers
if (model.OAuthConnections?.Any(x => x == null) == true)
return BadRequest(new ErrorMessage(ErrorCode.ModelValidationFailure));
if (!(model.Password == null ^ model.SystemIdentifier == null))
if ((model.Password != null && model.SystemIdentifier != null)
|| (model.Password == null && model.SystemIdentifier == null && model.OAuthConnections?.Any() != true))
return BadRequest(new ErrorMessage(ErrorCode.UserMismatchPasswordSid));
if (model.Group != null && model.PermissionSet != null)
@@ -155,7 +156,7 @@ namespace Tgstation.Server.Host.Controllers
{
return RequiresPosixSystemIdentity();
}
else if (!(model.Password?.Length == 0 && model.OAuthConnections.Count != 0))
else if (!(model.Password?.Length == 0 && model.OAuthConnections?.Any() == true))
{
var result = TrySetPassword(dbUser, model.Password, true);
if (result != null)
@@ -261,6 +262,9 @@ namespace Tgstation.Server.Host.Controllers
if (originalUser.CanonicalName == Models.User.CanonicalizeName(Api.Models.User.AdminName))
return BadRequest(new ErrorMessage(ErrorCode.AdminUserCannotOAuth));
if (model.OAuthConnections.Count == 0 && originalUser.PasswordHash == null && originalUser.SystemIdentifier == null)
return BadRequest(new ErrorMessage(ErrorCode.CannotRemoveLastAuthenticationOption));
originalUser.OAuthConnections.Clear();
foreach (var updatedConnection in model.OAuthConnections)
originalUser.OAuthConnections.Add(new Models.OAuthConnection