Skip to content

Commit

Permalink
Implement the ability to forcibly verify a user's email (#1022)
Browse files Browse the repository at this point in the history
* Implement the ability to forcibly verify a user's email

* Apply suggestions from code review
  • Loading branch information
sudokoko authored May 31, 2024
1 parent 262ada3 commit 643cb8e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,6 +92,31 @@ await this.database.SendNotification(targetedUser.UserId,
return this.Redirect($"/user/{targetedUser.UserId}");
}

/// <summary>
/// Forces the email verification of a user.
/// </summary>
[HttpGet("forceVerifyEmail")]
public async Task<IActionResult> 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<EmailVerificationTokenEntity> 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<IActionResult> SetUserPermissionLevel([FromRoute] int id, [FromForm] PermissionLevel role)
{
Expand Down
8 changes: 8 additions & 0 deletions ProjectLighthouse.Servers.Website/Pages/UserPage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,14 @@ else
</a>
}

@if (!Model.ProfileUser.EmailAddressVerified)
{
<a class="ui green button" href="/moderation/user/@Model.ProfileUser.UserId/forceVerifyEmail">
<i class="check icon"></i>
<span>Force Verify Email</span>
</a>
}

@if (Model.User.IsAdmin)
{
<div class="ui divider"></div>
Expand Down

0 comments on commit 643cb8e

Please sign in to comment.