From 643cb8e816b33a959c4cd2778e5caf2ae03109ee Mon Sep 17 00:00:00 2001 From: sudokoko Date: Fri, 31 May 2024 18:10:05 -0400 Subject: [PATCH] Implement the ability to forcibly verify a user's email (#1022) * Implement the ability to forcibly verify a user's email * Apply suggestions from code review --- .../Controllers/Admin/AdminUserController.cs | 26 +++++++++++++++++++ .../Pages/UserPage.cshtml | 8 ++++++ 2 files changed, 34 insertions(+) diff --git a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs index 84b024ff5..426269448 100644 --- a/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs +++ b/ProjectLighthouse.Servers.Website/Controllers/Admin/AdminUserController.cs @@ -3,6 +3,7 @@ using LBPUnion.ProjectLighthouse.Files; using LBPUnion.ProjectLighthouse.Logging; using LBPUnion.ProjectLighthouse.Types.Entities.Profile; +using LBPUnion.ProjectLighthouse.Types.Entities.Token; using LBPUnion.ProjectLighthouse.Types.Logging; using LBPUnion.ProjectLighthouse.Types.Moderation.Cases; using LBPUnion.ProjectLighthouse.Types.Users; @@ -91,6 +92,31 @@ await this.database.SendNotification(targetedUser.UserId, return this.Redirect($"/user/{targetedUser.UserId}"); } + /// + /// Forces the email verification of a user. + /// + [HttpGet("forceVerifyEmail")] + public async Task ForceVerifyEmail([FromRoute] int id) + { + UserEntity? user = this.database.UserFromWebRequest(this.Request); + if (user == null || !user.IsModerator) return this.NotFound(); + + UserEntity? targetedUser = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == id); + if (targetedUser == null) return this.NotFound(); + if (user.EmailAddressVerified) return this.NotFound(); + + List tokens = await this.database.EmailVerificationTokens + .Where(t => t.UserId == targetedUser.UserId) + .ToListAsync(); + this.database.EmailVerificationTokens.RemoveRange(tokens); + + targetedUser.EmailAddressVerified = true; + + await this.database.SaveChangesAsync(); + + return this.Redirect($"/user/{targetedUser.UserId}"); + } + [HttpPost("/admin/user/{id:int}/setPermissionLevel")] public async Task SetUserPermissionLevel([FromRoute] int id, [FromForm] PermissionLevel role) { diff --git a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml index d675e8c1f..e1b6fe788 100644 --- a/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml +++ b/ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml @@ -333,6 +333,14 @@ else } + @if (!Model.ProfileUser.EmailAddressVerified) + { + + + Force Verify Email + + } + @if (Model.User.IsAdmin) {