From 2af9b59a4fdac171bd785dad166733579bddeddb Mon Sep 17 00:00:00 2001 From: James Gunn Date: Tue, 16 Jan 2024 09:59:41 +0000 Subject: [PATCH] Remove autocomplete from Merge User UI (#781) --- .../Pages/Admin/MergeUser/SelectUser.cshtml | 44 ++----------------- .../Admin/MergeUser/SelectUser.cshtml.cs | 22 ++++------ .../Admin/MergeUser/SelectUserTests.cs | 8 ++-- 3 files changed, 17 insertions(+), 57 deletions(-) diff --git a/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml b/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml index 7d8f6c3b3..f05f6b3bd 100644 --- a/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml +++ b/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml @@ -5,35 +5,6 @@ ViewBag.Title = Html.DisplayNameFor(m => m.UserIdToMerge); } -@section Styles -{ - -} - -@section Scripts -{ - - -} - @section BeforeContent { } @@ -41,18 +12,11 @@
- - + +

@ViewBag.Title

- -

Which user do you want to merge with @Model.UserId.

-
- - @foreach (var userId in Model.UserIds ?? Enumerable.Empty()) - { - @userId - } -
+ + Continue
diff --git a/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml.cs b/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml.cs index 7d624c863..9300bf896 100644 --- a/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml.cs +++ b/dotnet-authserver/src/TeacherIdentity.AuthServer/Pages/Admin/MergeUser/SelectUser.cshtml.cs @@ -18,11 +18,9 @@ public Merge(TeacherIdentityServerDbContext dbContext) _dbContext = dbContext; } - public ICollection? UserIds { get; set; } - [BindProperty] - [Display(Name = "Select which user to merge")] - [Required(ErrorMessage = "Select which user you want to merge")] + [Display(Name = "Enter the user ID to merge")] + [Required(ErrorMessage = "Enter the user ID to merge")] public Guid? UserIdToMerge { get; set; } [FromRoute] @@ -34,14 +32,17 @@ public void OnGet() public IActionResult OnPost() { - if (!ModelState.IsValid) + if (!UserIdToMerge.HasValue) { - return this.PageWithErrors(); + ModelState.AddModelError(nameof(UserIdToMerge), "Enter the user ID of the user to merge."); + } + else if (UserIdToMerge.Value == UserId) + { + ModelState.AddModelError(nameof(UserIdToMerge), "User cannot be merged with itself."); } - if (UserIds == null || !UserIds.Contains((Guid)UserIdToMerge!)) + if (!ModelState.IsValid) { - ModelState.AddModelError(nameof(UserIdToMerge), "You must select a user ID from the given list"); return this.PageWithErrors(); } @@ -58,11 +59,6 @@ public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingConte return; } - UserIds = _dbContext.Users - .Where(u => u.UserId != UserId && u.UserType == UserType.Default) - .Select(u => u.UserId) - .ToArray(); - await next(); } } diff --git a/dotnet-authserver/tests/TeacherIdentity.AuthServer.Tests/EndpointTests/Admin/MergeUser/SelectUserTests.cs b/dotnet-authserver/tests/TeacherIdentity.AuthServer.Tests/EndpointTests/Admin/MergeUser/SelectUserTests.cs index 6920ea884..1a84e491b 100644 --- a/dotnet-authserver/tests/TeacherIdentity.AuthServer.Tests/EndpointTests/Admin/MergeUser/SelectUserTests.cs +++ b/dotnet-authserver/tests/TeacherIdentity.AuthServer.Tests/EndpointTests/Admin/MergeUser/SelectUserTests.cs @@ -97,7 +97,7 @@ public async Task Post_EmptyUserIdToMerge_ReturnsError() var response = await HttpClient.SendAsync(request); // Assert - await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "Select which user you want to merge"); + await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "Enter the user ID to merge"); } [Fact] @@ -105,13 +105,13 @@ public async Task Post_InvalidUserIdToMerge_ReturnsError() { // Arrange var user = await TestData.CreateUser(userType: UserType.Default, hasTrn: false); - var invalidUserToMerge = await TestData.CreateUser(userType: UserType.Staff, hasTrn: false); + var invalidUserToMergeId = user.UserId; var request = new HttpRequestMessage(HttpMethod.Post, $"/admin/users/{user.UserId}/merge") { Content = new FormUrlEncodedContentBuilder() { - { "UserIdToMerge", invalidUserToMerge.UserId } + { "UserIdToMerge", invalidUserToMergeId } } }; @@ -119,7 +119,7 @@ public async Task Post_InvalidUserIdToMerge_ReturnsError() var response = await HttpClient.SendAsync(request); // Assert - await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "You must select a user ID from the given list"); + await AssertEx.HtmlResponseHasError(response, "UserIdToMerge", "User cannot be merged with itself."); } [Fact]