Merge branch 'general-devel' into pr3055

This commit is contained in:
Lucas Nicodemus 2025-01-26 13:42:01 +09:00 committed by GitHub
commit 5da5f35c15
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 106 additions and 5 deletions

View file

@ -1176,7 +1176,7 @@ namespace TShockAPI
try try
{ {
TShock.UserAccounts.SetUserGroup(account, args.Parameters[2]); TShock.UserAccounts.SetUserGroup(args.Player, account, args.Parameters[2]);
TShock.Log.ConsoleInfo(GetString("{0} changed account {1} to group {2}.", args.Player.Name, account.Name, args.Parameters[2])); TShock.Log.ConsoleInfo(GetString("{0} changed account {1} to group {2}.", args.Player.Name, account.Name, args.Parameters[2]));
args.Player.SendSuccessMessage(GetString("Account {0} has been changed to group {1}.", account.Name, args.Parameters[2])); args.Player.SendSuccessMessage(GetString("Account {0} has been changed to group {1}.", account.Name, args.Parameters[2]));
@ -1193,6 +1193,10 @@ namespace TShockAPI
{ {
args.Player.SendErrorMessage(GetString($"User {account.Name} does not exist.")); args.Player.SendErrorMessage(GetString($"User {account.Name} does not exist."));
} }
catch (UserGroupUpdateLockedException)
{
args.Player.SendErrorMessage(GetString("Hook blocked the attempt to change the user group."));
}
catch (UserAccountManagerException e) catch (UserAccountManagerException e)
{ {
args.Player.SendErrorMessage(GetString($"User {account.Name} could not be added. Check console for details.")); args.Player.SendErrorMessage(GetString($"User {account.Name} could not be added. Check console for details."));

View file

@ -25,6 +25,7 @@ using MySql.Data.MySqlClient;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using BCrypt.Net; using BCrypt.Net;
using System.Security.Cryptography; using System.Security.Cryptography;
using TShockAPI.Hooks;
namespace TShockAPI.DB namespace TShockAPI.DB
{ {
@ -166,7 +167,41 @@ namespace TShockAPI.DB
if (null == grp) if (null == grp)
throw new GroupNotExistsException(group); throw new GroupNotExistsException(group);
if (_database.Query("UPDATE Users SET UserGroup = @0 WHERE Username = @1;", group, account.Name) == 0) if (AccountHooks.OnAccountGroupUpdate(account, ref grp))
throw new UserGroupUpdateLockedException(account.Name);
if (_database.Query("UPDATE Users SET UserGroup = @0 WHERE Username = @1;", grp.Name, account.Name) == 0)
throw new UserAccountNotExistException(account.Name);
try
{
// Update player group reference for any logged in player
foreach (var player in TShock.Players.Where(p => p != null && p.Account != null && p.Account.Name == account.Name))
{
player.Group = grp;
}
}
catch (Exception ex)
{
throw new UserAccountManagerException(GetString("SetUserGroup SQL returned an error"), ex);
}
}
/// <summary>
/// Sets the group for a given username
/// </summary>
/// <param name="author">Who changes the group</param>
/// <param name="account">The user account</param>
/// <param name="group">The user account group to be set</param>
public void SetUserGroup(TSPlayer author, UserAccount account, string group)
{
Group grp = TShock.Groups.GetGroupByName(group);
if (null == grp)
throw new GroupNotExistsException(group);
if (AccountHooks.OnAccountGroupUpdate(account, author, ref grp))
throw new UserGroupUpdateLockedException(account.Name);
if (_database.Query("UPDATE Users SET UserGroup = @0 WHERE Username = @1;", grp.Name, account.Name) == 0)
throw new UserAccountNotExistException(account.Name); throw new UserAccountNotExistException(account.Name);
try try
@ -619,7 +654,7 @@ namespace TShockAPI.DB
public class UserAccountNotExistException : UserAccountManagerException public class UserAccountNotExistException : UserAccountManagerException
{ {
/// <summary>Creates a new UserAccountNotExistException object, with the user account name in the message.</summary> /// <summary>Creates a new UserAccountNotExistException object, with the user account name in the message.</summary>
/// <param name="name">The user account name to be pasesd in the message.</param> /// <param name="name">The user account name to be passed in the message.</param>
/// <returns>A new UserAccountNotExistException object with a message containing the user account name that does not exist.</returns> /// <returns>A new UserAccountNotExistException object with a message containing the user account name that does not exist.</returns>
public UserAccountNotExistException(string name) public UserAccountNotExistException(string name)
: base(GetString($"User account {name} does not exist")) : base(GetString($"User account {name} does not exist"))
@ -627,6 +662,20 @@ namespace TShockAPI.DB
} }
} }
/// <summary>The UserGroupUpdateLockedException used when the user group update failed and the request failed as a result.</summary>.
[Serializable]
public class UserGroupUpdateLockedException : UserAccountManagerException
{
/// <summary>Creates a new UserGroupUpdateLockedException object.</summary>
/// <param name="name">The name of the user who failed to change the group.</param>
/// <returns>New UserGroupUpdateLockedException object with a message containing the name of the user account that failed to change the group.</returns>
public UserGroupUpdateLockedException(string name) :
base(GetString($"Unable to update group of user {name}."))
{
}
}
/// <summary>A GroupNotExistsException, used when a group does not exist.</summary> /// <summary>A GroupNotExistsException, used when a group does not exist.</summary>
[Serializable] [Serializable]
public class GroupNotExistsException : UserAccountManagerException public class GroupNotExistsException : UserAccountManagerException

View file

@ -16,6 +16,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
using System.ComponentModel;
using TShockAPI.DB; using TShockAPI.DB;
namespace TShockAPI.Hooks namespace TShockAPI.Hooks
{ {
@ -39,6 +40,31 @@ namespace TShockAPI.Hooks
} }
} }
public class AccountGroupUpdateEventArgs : HandledEventArgs
{
public string AccountName { get; private set; }
public Group Group { get; set; }
public AccountGroupUpdateEventArgs(string accountName, Group group)
{
this.AccountName = accountName;
this.Group = group;
}
}
public class AccountGroupUpdateByPlayerEventArgs : AccountGroupUpdateEventArgs
{
/// <summary>
/// The player who updated the user's group
/// </summary>
public TSPlayer Player { get; private set; }
public AccountGroupUpdateByPlayerEventArgs(TSPlayer player, string accountName, Group group) : base(accountName, group)
{
this.Player = player;
}
}
public class AccountHooks public class AccountHooks
{ {
public delegate void AccountCreateD(AccountCreateEventArgs e); public delegate void AccountCreateD(AccountCreateEventArgs e);
@ -62,5 +88,25 @@ namespace TShockAPI.Hooks
AccountDelete(new AccountDeleteEventArgs(u)); AccountDelete(new AccountDeleteEventArgs(u));
} }
public delegate void AccountGroupUpdateD(AccountGroupUpdateEventArgs e);
public static event AccountGroupUpdateD AccountGroupUpdate;
public static bool OnAccountGroupUpdate(UserAccount account, TSPlayer author, ref Group group)
{
AccountGroupUpdateEventArgs args = new AccountGroupUpdateByPlayerEventArgs(author, account.Name, group);
AccountGroupUpdate?.Invoke(args);
group = args.Group;
return args.Handled;
}
public static bool OnAccountGroupUpdate(UserAccount account, ref Group group)
{
AccountGroupUpdateEventArgs args = new AccountGroupUpdateEventArgs(account.Name, group);
AccountGroupUpdate?.Invoke(args);
group = args.Group;
return args.Handled;
}
} }
} }

View file

@ -555,7 +555,8 @@ namespace TShockAPI
{ {
try try
{ {
TShock.UserAccounts.SetUserGroup(account, group); TShock.UserAccounts.SetUserGroup(new TSRestPlayer(args.TokenData.Username, TShock.Groups.GetGroupByName(args.TokenData.UserGroupName)),
account, group);
response.Add("group-response", "Group updated successfully"); response.Add("group-response", "Group updated successfully");
} }
catch (Exception e) catch (Exception e)

@ -1 +1 @@
Subproject commit 8a3fffd71db401736ea80619122c70c449c10ff3 Subproject commit d4bb7e3a21e875cfeb23bcf5cf847c85d9470ccf

View file

@ -89,6 +89,7 @@ Use past tense when adding new entries; sign your name off when you add or chang
* Fixed unable to transfer long response body for REST API. (@sgkoishi, #2925) * Fixed unable to transfer long response body for REST API. (@sgkoishi, #2925)
* Fixed the `/wind` command not being very helpful. (@punchready) * Fixed the `/wind` command not being very helpful. (@punchready)
* Fixed /help, /me, and /p commands can't work in non-English languages. (@ACaiCat) * Fixed /help, /me, and /p commands can't work in non-English languages. (@ACaiCat)
* Added a hook `AccountHooks.AccountGroupUpdate`, which is called when you change the user group. (@AgaSpace)
## TShock 5.2.1 ## TShock 5.2.1
* Updated `TSPlayer.GodMode`. (@AgaSpace) * Updated `TSPlayer.GodMode`. (@AgaSpace)