Add command to remove user's 2fa configuration. (#1094)
Some checks failed
Upload Translations to Crowdin / crowdin-sync (push) Has been cancelled
Build Docker Image / Build and Publish (push) Has been cancelled
Qodana / qodana (push) Has been cancelled
Continuous Integration / Build & Test (push) Has been cancelled

* Add command to remove user's 2fa configuration.

* Use collection expression, because I'm metal like that
This commit is contained in:
Henry Asbridge 2025-07-20 19:55:01 +01:00 committed by GitHub
parent 92ce4c5af5
commit 509240db39
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -0,0 +1,47 @@
#nullable enable
using System;
using System.Threading.Tasks;
using LBPUnion.ProjectLighthouse.Database;
using LBPUnion.ProjectLighthouse.Logging;
using LBPUnion.ProjectLighthouse.Types.Entities.Profile;
using LBPUnion.ProjectLighthouse.Types.Logging;
using LBPUnion.ProjectLighthouse.Types.Maintenance;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace LBPUnion.ProjectLighthouse.Administration.Maintenance.Commands;
public class RemoveTwoFactorCommand : ICommand
{
public string Name() => "Remove Two Factor Authentication";
public string[] Aliases()
=>
[
"removeTwoFactor", "rm2fa",
];
public string Arguments() => "<username/userId>";
public int RequiredArgs() => 1;
public async Task Run(IServiceProvider provider, string[] args, Logger logger)
{
DatabaseContext database = provider.GetRequiredService<DatabaseContext>();
UserEntity? user = await database.Users.FirstOrDefaultAsync(u => u.Username == args[0]);
if (user == null)
{
_ = int.TryParse(args[0], out int userId);
user = await database.Users.FirstOrDefaultAsync(u => u.UserId == userId);
if (user == null)
{
logger.LogError($"Could not find user by parameter '{args[0]}'", LogArea.Command);
return;
}
}
user.TwoFactorSecret = "";
user.TwoFactorBackup = "";
await database.SaveChangesAsync();
logger.LogSuccess($"The two factor authentication for user {user.Username} (id: {user.UserId}) has been removed.", LogArea.Command);
}
}