Skip to content

Commit

Permalink
UserCLI: Fix minor corner cases
Browse files Browse the repository at this point in the history
- Fix "user remove" returning "No user selected" when removing a
  non-current account
- Fix "user update" incorrectly returning "No user selected" when
  updating a different than current account
- Fix "user switch" incorrectly clearing selected user + minor wording
  in returned messages
  • Loading branch information
lookeypl committed Sep 1, 2024
1 parent 2182500 commit 1c4b277
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion LukeBot/LukeBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public bool IsUsernameValid(string username)
{
lock (mUsersLock)
{
return (username.Length == 0) || mUsers.ContainsKey(username);
return username.Length != 0 && mUsers.ContainsKey(username);
}
}

Expand Down
42 changes: 26 additions & 16 deletions LukeBot/UserCLIProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,15 @@ void HandleRemoveUserCommand(UserRemoveCommand args, out string msg)

try
{
if (mCLI.GetCurrentUser() == args.Name)
mCLI.SetCurrentUser("");
try
{
if (mCLI.GetCurrentUser() == args.Name)
mCLI.SetCurrentUser("");
}
catch (NoUserSelectedException)
{
// noop
}

mLukeBot.RemoveUser(args.Name);
msg = "User " + args.Name + " removed.";
Expand All @@ -133,23 +140,24 @@ void HandleSwitchUserCommand(UserSwitchCommand args, out string msg)
{
try
{
if (!mLukeBot.IsUsernameValid(args.Name))
bool hasUsername = (args.Name != null && args.Name.Length > 0);
if (hasUsername && !mLukeBot.IsUsernameValid(args.Name))
throw new System.ArgumentException("Unknown/invalid username.");

mCLI.SetCurrentUser(args.Name);
}
catch (System.Exception e)
{
msg = "Failed to select user " + args.Name + ": " + e.Message;
}

try
{
msg = "Switched to user " + mCLI.GetCurrentUser();
try
{
msg = "Switched to user " + mCLI.GetCurrentUser();
}
catch (NoUserSelectedException)
{
msg = "Cleared current user";
}
}
catch (NoUserSelectedException)
catch (System.Exception e)
{
msg = "Cleared selected user";
msg = "Failed to switch to user " + args.Name + ": " + e.Message;
}
}

Expand All @@ -158,7 +166,8 @@ void HandlePasswordUserCommand(UserPasswordCommand args, out string msg)
try
{
UserContext user;
if (args.Name == null || args.Name.Length == 0)
bool currentUser = (args.Name == null || args.Name.Length == 0);
if (currentUser)
user = mLukeBot.GetUser(mCLI.GetCurrentUser());
else
user = mLukeBot.GetUser(args.Name);
Expand Down Expand Up @@ -186,14 +195,15 @@ void HandleUpdateUserCommand(UserUpdateCommand args, out string msg)
try
{
UserContext user;
if (args.Name == null || args.Name.Length == 0)
bool currentUser = (args.Name == null || args.Name.Length == 0);
if (currentUser)
user = mLukeBot.GetUser(mCLI.GetCurrentUser());
else
user = mLukeBot.GetUser(args.Name);

user.SetPermissionLevel(args.PermissionLevel);

if (user.Username == mCLI.GetCurrentUser())
if (currentUser)
mCLI.RefreshUserData();

mCLI.Message("Permission level set to " + args.PermissionLevel.ToString());
Expand Down

0 comments on commit 1c4b277

Please sign in to comment.