Skip to content

Commit

Permalink
Allow case-renaming Users (#2073)
Browse files Browse the repository at this point in the history
* allow renaming users into themselves with different letter case

* add renaming unit tests
  • Loading branch information
Masterjun3 authored Feb 12, 2025
1 parent 225ede3 commit 573c20c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 15 deletions.
15 changes: 15 additions & 0 deletions TASVideos.Core/Services/UserManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,19 @@ public async Task PermaBanUser(int userId)
user.BannedUntil = DateTime.UtcNow.AddYears(100);
await db.TrySaveChanges();
}

public async Task<bool> CanRenameUser(string oldUserName, string newUserName)
{
if (string.IsNullOrWhiteSpace(newUserName))
{
return false;
}

var users = await db.Users
.Select(user => user.UserName)
.Where(userName => userName == oldUserName || userName == newUserName)
.ToListAsync();

return users.Count == 1 && users[0] == oldUserName;
}
}
11 changes: 9 additions & 2 deletions TASVideos/Pages/Users/Edit.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,16 @@ public async Task<IActionResult> OnPost()
? user.UserName
: null;

if (UserToEdit.UserName is not null)
if (userNameChange is not null)
{
user.UserName = UserToEdit.UserName;
if (await userManager.CanRenameUser(user.UserName, UserToEdit.UserName!))
{
user.UserName = UserToEdit.UserName!;
}
else
{
userNameChange = null;
}
}

user.TimeZoneId = UserToEdit.TimeZone;
Expand Down
12 changes: 3 additions & 9 deletions TASVideos/Pages/Users/List.cshtml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace TASVideos.Pages.Users;

[AllowAnonymous]
public class ListModel(ApplicationDbContext db, ICacheService cache) : BasePageModel
public class ListModel(ApplicationDbContext db, ICacheService cache, UserManager userManager) : BasePageModel
{
[FromQuery]
public PagingModel Search { get; set; } = new();
Expand Down Expand Up @@ -37,15 +37,9 @@ public async Task<IActionResult> OnGetSearch(string partial)
return Json(matches);
}

public async Task<IActionResult> OnGetVerifyUniqueUserName(string userName)
public async Task<IActionResult> OnGetCanRenameUser(string oldUserName, string newUserName)
{
if (string.IsNullOrWhiteSpace(userName))
{
return Json(false);
}

var exists = await db.Users.Exists(userName);
return Json(exists);
return Json(await userManager.CanRenameUser(oldUserName, newUserName));
}

private async ValueTask<IEnumerable<string>> GetUsersByPartial(string partialUserName)
Expand Down
8 changes: 4 additions & 4 deletions TASVideos/wwwroot/js/user-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function showCheckNameBtn() {
}

function hideCheckNameBtn() {
checkUserBtn.classList.add("div-none");
checkUserBtn.classList.add("d-none");
checkUserBtn.parentNode.classList.remove('col-sm-2');
userNameDiv.classList.remove('col-sm-10');
userNameDiv.classList.add('col-sm-12');
Expand Down Expand Up @@ -62,14 +62,14 @@ checkUserBtn.onclick = function () {
return;
}

fetch(`/Users/List?handler=VerifyUniqueUserName&userName=${userNameBox.value}`)
fetch(`/Users/List?handler=CanRenameUser&oldUserName=${originalUserNameBox.value}&newUserName=${userNameBox.value}`)
.then(handleFetchErrors)
.then(r => r.text())
.then(d => {
if (d === "true") {
markUserNameBad();
} else {
markUserNameGood();
} else {
markUserNameBad();
}
});
};
15 changes: 15 additions & 0 deletions tests/TASVideos.Core.Tests/Services/UserManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -316,5 +316,20 @@ public async Task UserNameChanged()
Assert.AreEqual(2, _db.Publications.Count(s => s.Title.Contains(newName)));
}

[TestMethod]
[DataRow("OldUser", "NewUser", "OtherExistingUser", true)]
[DataRow("User", "user", "OtherExistingUser", true)]
[DataRow("User", "CoolUser", "CoolUser", false)]
[DataRow("User", "coolUser", "CoolUser", false)]
public async Task CanRenameUser(string oldUserName, string newUserName, string otherExistingUserName, bool expected)
{
_db.AddUser(oldUserName);
_db.AddUser(otherExistingUserName);
await _db.SaveChangesAsync();

var actual = await _userManager.CanRenameUser(oldUserName, newUserName);
Assert.AreEqual(expected, actual);
}

public void Dispose() => _userManager.Dispose();
}

0 comments on commit 573c20c

Please sign in to comment.