Add WipeTokensForUserCommand and DeleteAllTokensMaintenanceJob

This commit is contained in:
jvyden 2021-11-27 15:34:59 -05:00
commit f8d6c90e62
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 68 additions and 4 deletions

View file

@ -0,0 +1,46 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Types;
using Microsoft.EntityFrameworkCore;
namespace LBPUnion.ProjectLighthouse.Maintenance.Commands
{
public class WipeTokensForUserCommand : ICommand
{
private readonly Database database = new();
public string Name() => "Wipe tokens for user";
public string[] Aliases()
=> new[]
{
"wipeTokens", "wipeToken", "deleteTokens", "deleteToken", "removeTokens", "removeToken",
};
public string Arguments() => "<username/userId>";
public int RequiredArgs() => 1;
public async Task Run(string[] args)
{
User? user = await this.database.Users.FirstOrDefaultAsync(u => u.Username == args[0]);
if (user == null)
{
try
{
user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == Convert.ToInt32(args[0]));
if (user == null) throw new Exception();
}
catch
{
Console.WriteLine($"Could not find user by parameter '{args[0]}'");
return;
}
}
this.database.GameTokens.RemoveRange(this.database.GameTokens.Where(t => t.UserId == user.UserId));
this.database.WebTokens.RemoveRange(this.database.WebTokens.Where(t => t.UserId == user.UserId));
await this.database.SaveChangesAsync();
Console.WriteLine($"Deleted all tokens for {user.Username} (id: {user.UserId}).");
}
}
}

View file

@ -0,0 +1,22 @@
using System;
using System.Threading.Tasks;
namespace LBPUnion.ProjectLighthouse.Maintenance.MaintenanceJobs
{
public class DeleteAllTokensMaintenanceJob : IMaintenanceJob
{
private readonly Database database = new();
public string Name() => "Delete ALL Tokens";
public string Description() => "Deletes ALL game tokens and web tokens.";
public async Task Run()
{
this.database.GameTokens.RemoveRange(this.database.GameTokens);
this.database.WebTokens.RemoveRange(this.database.WebTokens);
await this.database.SaveChangesAsync();
Console.WriteLine("Deleted ALL tokens.");
}
}
}

View file

@ -33,10 +33,6 @@
</EmbeddedResource> </EmbeddedResource>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="CommandLine"/>
</ItemGroup>
<Target Name="PreBuild" BeforeTargets="PreBuildEvent"> <Target Name="PreBuild" BeforeTargets="PreBuildEvent">
<Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;"/> <Exec Command="git describe --long --always --dirty --exclude=\* --abbrev=8 &gt; &quot;$(ProjectDir)/gitVersion.txt&quot;"/>
<Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;"/> <Exec Command="git branch --show-current &gt; &quot;$(ProjectDir)/gitBranch.txt&quot;"/>