Skip to content

Commit

Permalink
Ability to fully delete (and soft-delete) users from the CLI (#532)
Browse files Browse the repository at this point in the history
Closes #518.

Also contains some minor tweaks to how the CLI works in general.
Commit-by-commit review is probably best but it's only 54 lines of
changes.
  • Loading branch information
jvyden authored Jun 18, 2024
2 parents 1e9c799 + 75c3545 commit 3b2c746
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
31 changes: 28 additions & 3 deletions Refresh.GameServer/CommandLineManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,27 @@ private class Options

[Option("rename-user", HelpText = "Changes a user's username. (old) username or Email option is required if this is set.")]
public string? RenameUser { get; set; }

[Option("delete-user", HelpText = "Deletes a user's account, removing their data but keeping a record of their sign up.")]
public bool DeleteUser { get; set; }

[Option("fully-delete-user", HelpText = "Fully deletes a user, entirely removing the row and allowing people to register with that username once again. Not recommended.")]
public bool FullyDeleteUser { get; set; }
}

internal void StartWithArgs(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(this.StartWithOptions);
try
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed(this.StartWithOptions);
}
catch (Exception e)
{
Fail($"An internal error occurred: {e}", 139);
}

Console.WriteLine("The operation completed successfully.");
}

[DoesNotReturn]
Expand All @@ -73,7 +88,7 @@ private static void Fail(string reason, int code = 1)

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(reason);
Console.WriteLine($"Failed with exit code {code}");
Console.WriteLine($"The operation failed with exit code {code}");
Console.ForegroundColor = oldColor;

Environment.Exit(code);
Expand Down Expand Up @@ -162,5 +177,15 @@ private void StartWithOptions(Options options)
GameUser user = this.GetUserOrFail(options);
this._server.RenameUser(user, options.RenameUser);
}
else if (options.DeleteUser)
{
GameUser user = this.GetUserOrFail(options);
this._server.DeleteUser(user);
}
else if (options.FullyDeleteUser)
{
GameUser user = this.GetUserOrFail(options);
this._server.FullyDeleteUser(user);
}
}
}
11 changes: 11 additions & 0 deletions Refresh.GameServer/Database/GameDatabaseContext.Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,17 @@ public void DeleteUser(GameUser user)
});
}

public void FullyDeleteUser(GameUser user)
{
// do an initial cleanup of everything before deleting the row
this.DeleteUser(user);

this._realm.Write(() =>
{
this._realm.Remove(user);
});
}

public void ResetUserPlanets(GameUser user)
{
this._realm.Write(() =>
Expand Down
12 changes: 12 additions & 0 deletions Refresh.GameServer/RefreshGameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ public void RenameUser(GameUser user, string newUsername)
using GameDatabaseContext context = this.GetContext();
context.RenameUser(user, newUsername);
}

public void DeleteUser(GameUser user)
{
using GameDatabaseContext context = this.GetContext();
context.DeleteUser(user);
}

public void FullyDeleteUser(GameUser user)
{
using GameDatabaseContext context = this.GetContext();
context.FullyDeleteUser(user);
}

public override void Dispose()
{
Expand Down

0 comments on commit 3b2c746

Please sign in to comment.