Skip to content

Commit

Permalink
Ability to rename a user from the CLI (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvyden authored Mar 29, 2024
2 parents 568b48d + 3e08b82 commit 04f4ea0
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
19 changes: 19 additions & 0 deletions Refresh.GameServer/CommandLineManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ private class Options

[Option('r', "reallow_user", Required = false, HelpText = "Re-allow a user to register. Username option is requried if this is set.")]
public bool ReallowUser { get; set; }

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

internal void StartWithArgs(string[] args)
Expand Down Expand Up @@ -139,5 +142,21 @@ private void StartWithOptions(Options options)
Environment.Exit(1);
}
}

if (options.RenameUser != null)
{
if (options.Username != null)
{
this._server.RenameUserFromUsername(options.Username, options.RenameUser);
} else if (options.EmailAddress != null)
{
this._server.RenameUserFromEmailAddress(options.EmailAddress, options.RenameUser);
}
else
{
Console.WriteLine("No user/email was provided, cannot continue.");
Environment.Exit(1);
}
}
}
}
8 changes: 8 additions & 0 deletions Refresh.GameServer/Database/GameDatabaseContext.Tokens.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ public void RevokeAllTokensForUser(GameUser user)
this._realm.RemoveRange(this._realm.All<Token>().Where(t => t.User == user));
});
}

public void RevokeAllTokensForUser(GameUser user, TokenType type)
{
this._realm.Write(() =>
{
this._realm.RemoveRange(this._realm.All<Token>().Where(t => t.User == user && t._TokenType == (int)type));
});
}

public void AddIpVerificationRequest(GameUser user, string ipAddress)
{
Expand Down
17 changes: 17 additions & 0 deletions Refresh.GameServer/Database/GameDatabaseContext.Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,23 @@ public DatabaseList<GameUser> GetAllUsersWithRole(GameUserRole role)
return new DatabaseList<GameUser>(this._realm.All<GameUser>().Where(u => u._Role == roleByte));
}

public void RenameUser(GameUser user, string newUsername)
{
string oldUsername = user.Username;

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

this.AddNotification("Username Updated", $"An admin has updated your account's username from '{oldUsername}' to '{newUsername}'. " +
$"If there are any problems caused by this, please let us know.", user);

// Since ticket authentication is username-based, delete all game tokens for this user
// Future authentication is going to be invalid, and the client is going to be in a broken state anyways.
this.RevokeAllTokensForUser(user, TokenType.Game);
}

public void DeleteUser(GameUser user)
{
const string deletedReason = "This user's account has been deleted.";
Expand Down
20 changes: 20 additions & 0 deletions Refresh.GameServer/RefreshGameServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,26 @@ public bool ReallowUser(string username)
return context.ReallowUser(username);
}

public void RenameUserFromUsername(string username, string newUsername)
{
using GameDatabaseContext context = this.GetContext();

GameUser? user = context.GetUserByUsername(username);
if (user == null) throw new InvalidOperationException("Cannot find the user " + username);

context.RenameUser(user, newUsername);
}

public void RenameUserFromEmailAddress(string emailAddress, string newUsername)
{
using GameDatabaseContext context = this.GetContext();

GameUser? user = context.GetUserByEmailAddress(emailAddress);
if (user == null) throw new InvalidOperationException("Cannot find a user by emailAddress " + emailAddress);

context.RenameUser(user, newUsername);
}

public override void Dispose()
{
this._databaseProvider.Dispose();
Expand Down

0 comments on commit 04f4ea0

Please sign in to comment.