Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support username casing change in Admin UI #9748

Draft
wants to merge 11 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -123,19 +123,24 @@ public async Task<ActionResult> ChangeUsername(string oldUsername, string newUse
return Json(HttpStatusCode.BadRequest, "New username validation failed.", JsonRequestBehavior.AllowGet);
}

var newAccountForOldUsername = new User()
{
Username = account.Username,
EmailAllowed = false,
IsDeleted = true,
CreatedUtc = _dateTimeProvider.UtcNow
};

account.Username = newUsername;

await _auditingService.SaveAuditRecordAsync(new UserAuditRecord(account, AuditedUserAction.ChangeUsername));

_userRepository.InsertOnCommit(newAccountForOldUsername);
if (account.Username.Equals(newUsername, StringComparison.OrdinalIgnoreCase) == false)
mariaghiondea marked this conversation as resolved.
Show resolved Hide resolved
RiadGahlouz marked this conversation as resolved.
Show resolved Hide resolved
{
// We're doing a full username change and not just a casing change so we need to lock the old username
var newAccountForOldUsername = new User()
{
Username = account.Username,
EmailAllowed = false,
IsDeleted = true,
CreatedUtc = _dateTimeProvider.UtcNow
};

_userRepository.InsertOnCommit(newAccountForOldUsername);
}

account.Username = newUsername;

await _entitiesContext.SaveChangesAsync();

Expand All @@ -144,11 +149,13 @@ public async Task<ActionResult> ChangeUsername(string oldUsername, string newUse

private ValidateUsernameResult ValidateUsername(string username)
{
var result = new ValidateUsernameResult();
result.IsFormatValid = UsernameValidationRegex.IsMatch(username);
result.IsAvailable = _userService.FindByUsername(username, includeDeleted: true) == null;
var foundUser = _userService.FindByUsername(username, includeDeleted: true);

return result;
return new ValidateUsernameResult()
{
IsFormatValid = UsernameValidationRegex.IsMatch(username),
IsAvailable = foundUser == null || foundUser.Username != username // The username check is in the event where we found a user in the DB but we're doing a cAsIng change
RiadGahlouz marked this conversation as resolved.
Show resolved Hide resolved
};
}
}
}