Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added AccountGroupUpdate hook, when trying to change a user group. #2947

Open
wants to merge 8 commits into
base: general-devel
Choose a base branch
from

Conversation

AgaSpace
Copy link
Contributor

@AgaSpace AgaSpace commented May 14, 2023

Added a hook to change the user group. The hook is in AccountHooks and is named AccountGroupUpdate.
The event takes AccountGroupUpdateEventArgs as an argument, but it can be represented as AccountGroupUpdateByPlayerEventArgs in case a user group is updated by some player.

Examples of use:

public void OnAccountGroupUpdate(AccountGroupUpdateEventArgs args)
{
	if (args.AccountName == "Zoom L1" && args is AccountGroupUpdateByPlayerEventArgs)
	{
		TSPlayer author = ((AccountGroupUpdateByPlayerEventArgs)args).Player;
		if (author.Account.Name != "Zoom L1")
			args.Handled = true;
	}
	else
	{
		TShock.Log.ConsoleInfo($"The user group {args.AccountName} has been updated.");
	}
}
public void OnAccountGroupUpdate(AccountGroupUpdateEventArgs args)
{
	if (args is AccountGroupUpdateByPlayerEventArgs)
	{
		TSPlayer author = ((AccountGroupUpdateByPlayerEventArgs)args).Player;
		UserAccount account = TShock.UserAccounts.GetUserAccountByName(args.AccountName);
		if (account != null) // Must be used, because the user can be null.
		{
			if (account.Group.ToLower() == "superadmin" && author.Group is not SuperAdminGroup)
				args.Handled = true;
		}
	}
}

Added an exception `UserGroupUpdateLockedException`, which appears when a hook locks a group change.
Added an overload for `UserManager.SetUserGroup`, with the `TSPlayer` parameter (author)
/// <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($"The user {name} group could not be updated"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception message is confusing, The user John group could not be updated

@AgaSpace AgaSpace requested a review from Arthri June 10, 2023 06:59
Arthri
Arthri previously approved these changes Jun 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants