Allow RegistrationTokens to have a username pre-selected (#524)

* Add prefilled username to registration token

* call toString on query to work with EF

* Fix typo in RegisterForm.cshtml

* Only show username notice if the user's username hasn't been chosen already

* Add confirmation message to DeleteUserCommand
This commit is contained in:
Josh 2022-10-31 20:48:09 -05:00 committed by GitHub
parent bfe81c3461
commit c7195df74f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 66 additions and 12 deletions

View file

@ -68,7 +68,8 @@ public class UserEndpoints : ApiEndpointController
}
[HttpPost("user/inviteToken")]
public async Task<IActionResult> CreateUserInviteToken()
[HttpPost("user/inviteToken/{username}")]
public async Task<IActionResult> CreateUserInviteToken([FromRoute] string? username)
{
if (!Configuration.ServerConfiguration.Instance.Authentication.PrivateRegistration &&
!Configuration.ServerConfiguration.Instance.Authentication.RegistrationEnabled)
@ -86,6 +87,7 @@ public class UserEndpoints : ApiEndpointController
{
Created = DateTime.Now,
Token = CryptoHelper.GenerateAuthToken(),
Username = username,
};
this.database.RegistrationTokens.Add(token);

View file

@ -33,7 +33,10 @@
</div>
}
@if (Model.Username == null)
{
<p><b>@Model.Translate(RegisterStrings.UsernameNotice)</b></p>
}
<form class="ui form" onsubmit="return onSubmit(this)" method="post">
@Html.AntiForgeryToken()
@ -41,7 +44,14 @@
<div class="field">
<label>Username</label>
<div class="ui left icon input">
<input type="text" name="username" id="text" placeholder="Username" pattern="^[a-zA-Z0-9_.-]*$" minlength="3" maxlength="16">
@{
string extra = "";
if (Model.Username != null)
{
extra = "value=" + Model.Username + " readonly";
}
}
<input type="text" name="username" id="text" placeholder="Username" pattern="^[a-zA-Z0-9_.-]*$" minlength="3" maxlength="16" @extra>
<i class="user icon"></i>
</div>
</div>

View file

@ -7,7 +7,6 @@ using LBPUnion.ProjectLighthouse.Localization.StringLists;
using LBPUnion.ProjectLighthouse.PlayerData;
using LBPUnion.ProjectLighthouse.PlayerData.Profiles;
using LBPUnion.ProjectLighthouse.Servers.Website.Pages.Layouts;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
@ -20,6 +19,8 @@ public class RegisterForm : BaseLayout
public string? Error { get; private set; }
public string? Username { get; set; }
[UsedImplicitly]
[SuppressMessage("ReSharper", "SpecifyStringComparison")]
public async Task<IActionResult> OnPost(string username, string password, string confirmPassword, string emailAddress)
@ -30,6 +31,8 @@ public class RegisterForm : BaseLayout
{
if (!this.Database.IsRegistrationTokenValid(this.Request.Query["token"]))
return this.StatusCode(403, this.Translate(ErrorStrings.TokenInvalid));
username = (await this.Database.RegistrationTokens.FirstAsync(r => r.Token == this.Request.Query["token"].ToString())).Username;
}
else
{
@ -119,6 +122,8 @@ public class RegisterForm : BaseLayout
{
if (!this.Database.IsRegistrationTokenValid(this.Request.Query["token"]))
return this.StatusCode(403, this.Translate(ErrorStrings.TokenInvalid));
this.Username = this.Database.RegistrationTokens.First(r => r.Token == this.Request.Query["token"].ToString()).Username;
}
else
{

View file

@ -34,5 +34,6 @@ public class DeleteUserCommand : ICommand
}
await this.database.RemoveUser(user);
logger.LogSuccess($"Successfully deleted user {user.Username}", LogArea.Command);
}
}

View file

@ -1,4 +1,5 @@
using System;
using System.Buffers.Binary;
using System.IO;
namespace LBPUnion.ProjectLighthouse.Extensions;
@ -15,19 +16,19 @@ public static class BinaryReaderExtensions
return b;
}
public static ushort ReadUInt16BE(this BinaryReader binRdr) => BitConverter.ToUInt16(binRdr.ReadBytesRequired(sizeof(ushort)).Reverse(), 0);
public static ushort ReadUInt16BE(this BinaryReader binRdr) => BinaryPrimitives.ReadUInt16BigEndian(binRdr.ReadBytesRequired(sizeof(ushort)));
public static short ReadInt16BE(this BinaryReader binRdr) => BitConverter.ToInt16(binRdr.ReadBytesRequired(sizeof(short)).Reverse(), 0);
public static short ReadInt16BE(this BinaryReader binRdr) => BinaryPrimitives.ReadInt16BigEndian(binRdr.ReadBytesRequired(sizeof(short)));
public static uint ReadUInt32BE(this BinaryReader binRdr) => BitConverter.ToUInt32(binRdr.ReadBytesRequired(sizeof(uint)).Reverse(), 0);
public static uint ReadUInt32BE(this BinaryReader binRdr) => BinaryPrimitives.ReadUInt32BigEndian(binRdr.ReadBytesRequired(sizeof(uint)));
public static int ReadInt32BE(this BinaryReader binRdr) => BitConverter.ToInt32(binRdr.ReadBytesRequired(sizeof(int)).Reverse(), 0);
public static int ReadInt32BE(this BinaryReader binRdr) => BinaryPrimitives.ReadInt32BigEndian(binRdr.ReadBytesRequired(sizeof(int)));
public static ulong ReadUInt64BE(this BinaryReader binRdr) => BitConverter.ToUInt32(binRdr.ReadBytesRequired(sizeof(ulong)).Reverse(), 0);
public static ulong ReadUInt64BE(this BinaryReader binRdr) => BinaryPrimitives.ReadUInt64BigEndian(binRdr.ReadBytesRequired(sizeof(ulong)));
public static long ReadInt64BE(this BinaryReader binRdr) => BitConverter.ToInt32(binRdr.ReadBytesRequired(sizeof(long)).Reverse(), 0);
public static long ReadInt64BE(this BinaryReader binRdr) => BinaryPrimitives.ReadInt64BigEndian(binRdr.ReadBytesRequired(sizeof(long)));
public static byte[] ReadBytesRequired(this BinaryReader binRdr, int byteCount)
private static byte[] ReadBytesRequired(this BinaryReader binRdr, int byteCount)
{
byte[] result = binRdr.ReadBytes(byteCount);

View file

@ -0,0 +1,30 @@
using LBPUnion.ProjectLighthouse;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace ProjectLighthouse.Migrations
{
[DbContext(typeof(Database))]
[Migration("20221016234831_AddUsernameToRegistrationToken")]
public partial class AddUsernameToRegistrationToken : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Username",
table: "RegistrationTokens",
type: "longtext",
nullable: true)
.Annotation("MySql:CharSet", "utf8mb4");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Username",
table: "RegistrationTokens");
}
}
}

View file

@ -11,6 +11,8 @@ namespace LBPUnion.ProjectLighthouse.PlayerData
public string Token { get; set; }
public DateTime Created { get; set; }
public string Username { get; set; }
}
}

View file

@ -22,7 +22,7 @@ public class Score
public Slot Slot { get; set; }
[XmlIgnore]
public int? ChildSlotId { get; set; }
public int ChildSlotId { get; set; }
[XmlElement("type")]
public int Type { get; set; }

View file

@ -873,6 +873,9 @@ namespace ProjectLighthouse.Migrations
b.Property<string>("Token")
.HasColumnType("longtext");
b.Property<string>("Username")
.HasColumnType("longtext");
b.HasKey("TokenId");
b.ToTable("RegistrationTokens");