From 509240db396eac0c69bd7ed8e09b71f630a8de72 Mon Sep 17 00:00:00 2001 From: Henry Asbridge Date: Sun, 20 Jul 2025 19:55:01 +0100 Subject: [PATCH] Add command to remove user's 2fa configuration. (#1094) * Add command to remove user's 2fa configuration. * Use collection expression, because I'm metal like that --- .../Commands/RemoveTwoFactorCommand.cs | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 ProjectLighthouse/Administration/Maintenance/Commands/RemoveTwoFactorCommand.cs diff --git a/ProjectLighthouse/Administration/Maintenance/Commands/RemoveTwoFactorCommand.cs b/ProjectLighthouse/Administration/Maintenance/Commands/RemoveTwoFactorCommand.cs new file mode 100644 index 00000000..8a49660b --- /dev/null +++ b/ProjectLighthouse/Administration/Maintenance/Commands/RemoveTwoFactorCommand.cs @@ -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() => ""; + public int RequiredArgs() => 1; + + public async Task Run(IServiceProvider provider, string[] args, Logger logger) + { + DatabaseContext database = provider.GetRequiredService(); + 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); + } +} \ No newline at end of file