From 1e037748c1d511fdc3449ff6261a55a049b4e96d Mon Sep 17 00:00:00 2001
From: AkjaHAsLk1IALk0MasH <46046453+AgaSpace@users.noreply.github.com>
Date: Sun, 14 May 2023 12:13:56 +0700
Subject: [PATCH] Updated the `UserManager.SetUserGroup`.
Added an exception `UserGroupUpdateLockedException`, which appears when a hook locks a group change.
Added an overload for `UserManager.SetUserGroup`, with the `TSPlayer` parameter (author)
---
TShockAPI/DB/UserManager.cs | 51 ++++++++++++++++++++++++++++++++++++-
1 file changed, 50 insertions(+), 1 deletion(-)
diff --git a/TShockAPI/DB/UserManager.cs b/TShockAPI/DB/UserManager.cs
index 7b8fe4a2..6411b289 100644
--- a/TShockAPI/DB/UserManager.cs
+++ b/TShockAPI/DB/UserManager.cs
@@ -25,6 +25,7 @@ using MySql.Data.MySqlClient;
using System.Text.RegularExpressions;
using BCrypt.Net;
using System.Security.Cryptography;
+using TShockAPI.Hooks;
namespace TShockAPI.DB
{
@@ -166,7 +167,41 @@ namespace TShockAPI.DB
if (null == grp)
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);
+ }
+ }
+ ///
+ /// Sets the group for a given username
+ ///
+ /// Who changes the group
+ /// The user account
+ /// The user account group to be set
+ 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);
try
@@ -627,6 +662,20 @@ namespace TShockAPI.DB
}
}
+ /// The UserGroupUpdateLockedException used when the user group update failed and the request failed as a result..
+ [Serializable]
+ public class UserGroupUpdateLockedException : UserAccountManagerException
+ {
+ /// Creates a new UserGroupUpdateLockedException object.
+ /// The name of the user who failed to change the group.
+ /// New UserGroupUpdateLockedException object with a message containing the name of the user account that failed to change the group.
+ public UserGroupUpdateLockedException(string name) :
+ base(GetString($"The user {name} group could not be updated"))
+ {
+ }
+ }
+
+
/// A GroupNotExistsException, used when a group does not exist.
[Serializable]
public class GroupNotExistsException : UserAccountManagerException