Custom Permission Handler (#1282)

Co-authored-by: Melledy <52122272+Melledy@users.noreply.github.com>
This commit is contained in:
4Benj_ 2022-06-16 23:00:03 +08:00 committed by GitHub
parent 6d59159b8b
commit bb07d9ea41
6 changed files with 75 additions and 16 deletions

View File

@ -6,6 +6,8 @@ import java.util.Calendar;
import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.auth.DefaultAuthentication; import emu.grasscutter.auth.DefaultAuthentication;
import emu.grasscutter.command.CommandMap; import emu.grasscutter.command.CommandMap;
import emu.grasscutter.command.DefaultPermissionHandler;
import emu.grasscutter.command.PermissionHandler;
import emu.grasscutter.game.managers.energy.EnergyManager; import emu.grasscutter.game.managers.energy.EnergyManager;
import emu.grasscutter.game.managers.stamina.StaminaManager; import emu.grasscutter.game.managers.stamina.StaminaManager;
import emu.grasscutter.plugin.PluginManager; import emu.grasscutter.plugin.PluginManager;
@ -58,6 +60,7 @@ public final class Grasscutter {
private static GameServer gameServer; private static GameServer gameServer;
private static PluginManager pluginManager; private static PluginManager pluginManager;
private static AuthenticationSystem authenticationSystem; private static AuthenticationSystem authenticationSystem;
private static PermissionHandler permissionHandler;
public static final Reflections reflector = new Reflections("emu.grasscutter"); public static final Reflections reflector = new Reflections("emu.grasscutter");
public static ConfigContainer config; public static ConfigContainer config;
@ -114,8 +117,9 @@ public final class Grasscutter {
// Initialize database. // Initialize database.
DatabaseManager.initialize(); DatabaseManager.initialize();
// Initialize the default authentication system. // Initialize the default systems.
authenticationSystem = new DefaultAuthentication(); authenticationSystem = new DefaultAuthentication();
permissionHandler = new DefaultPermissionHandler();
// Create server instances. // Create server instances.
httpServer = new HttpServer(); httpServer = new HttpServer();
@ -287,6 +291,10 @@ public final class Grasscutter {
return authenticationSystem; return authenticationSystem;
} }
public static PermissionHandler getPermissionHandler() {
return permissionHandler;
}
public static int getCurrentDayOfWeek() { public static int getCurrentDayOfWeek() {
return day; return day;
} }
@ -346,6 +354,14 @@ public final class Grasscutter {
Grasscutter.authenticationSystem = authenticationSystem; Grasscutter.authenticationSystem = authenticationSystem;
} }
/**
* Sets the permission handler for the server.
* @param permissionHandler The permission handler to use.
*/
public static void setPermissionHandler(PermissionHandler permissionHandler) {
Grasscutter.permissionHandler = permissionHandler;
}
/* /*
* Enums for the configuration. * Enums for the configuration.
*/ */

View File

@ -14,6 +14,7 @@ public final class CommandMap {
private final Map<String, Command> annotations = new HashMap<>(); private final Map<String, Command> annotations = new HashMap<>();
private final Map<String, Integer> targetPlayerIds = new HashMap<>(); private final Map<String, Integer> targetPlayerIds = new HashMap<>();
private static final String consoleId = "console"; private static final String consoleId = "console";
public CommandMap() { public CommandMap() {
this(false); this(false);
} }
@ -202,22 +203,10 @@ public final class CommandMap {
} }
} }
// Check for permission. // Check for permissions.
if (player != null) { if (!Grasscutter.getPermissionHandler().checkPermission(player, targetPlayer, this.annotations.get(label).permission(), this.annotations.get(label).permissionTargeted())) {
String permissionNode = this.annotations.get(label).permission();
String permissionNodeTargeted = this.annotations.get(label).permissionTargeted();
Account account = player.getAccount();
if (player != targetPlayer) { // Additional permission required for targeting another player
if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) {
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
return; return;
} }
}
if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
return;
}
}
// Check if command has unfulfilled constraints on targetPlayer // Check if command has unfulfilled constraints on targetPlayer
Command.TargetRequirement targetRequirement = this.annotations.get(label).targetRequirement(); Command.TargetRequirement targetRequirement = this.annotations.get(label).targetRequirement();

View File

@ -0,0 +1,32 @@
package emu.grasscutter.command;
import emu.grasscutter.game.Account;
import emu.grasscutter.game.player.Player;
public class DefaultPermissionHandler implements PermissionHandler {
@Override
public boolean EnablePermissionCommand() {
return true;
}
@Override
public boolean checkPermission(Player player, Player targetPlayer, String permissionNode, String permissionNodeTargeted) {
if(player == null) {
return true;
}
Account account = player.getAccount();
if (player != targetPlayer) { // Additional permission required for targeting another player
if (!permissionNodeTargeted.isEmpty() && !account.hasPermission(permissionNodeTargeted)) {
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
return false;
}
}
if (!permissionNode.isEmpty() && !account.hasPermission(permissionNode)) {
CommandHandler.sendTranslatedMessage(player, "commands.generic.permission_error");
return false;
}
return true;
}
}

View File

@ -0,0 +1,8 @@
package emu.grasscutter.command;
import emu.grasscutter.game.player.Player;
public interface PermissionHandler {
public boolean EnablePermissionCommand();
public boolean checkPermission(Player player, Player targetPlayer, String permissionNode, String permissionNodeTargeted);
}

View File

@ -20,6 +20,11 @@ public final class PermissionCommand implements CommandHandler {
return; return;
} }
if(!Grasscutter.getPermissionHandler().EnablePermissionCommand()) {
CommandHandler.sendTranslatedMessage(sender, "commands.generic.permission_error");
return;
}
String action = args.get(0); String action = args.get(0);
String permission = args.get(1); String permission = args.get(1);

View File

@ -4,6 +4,7 @@ import emu.grasscutter.Grasscutter;
import emu.grasscutter.auth.AuthenticationSystem; import emu.grasscutter.auth.AuthenticationSystem;
import emu.grasscutter.command.Command; import emu.grasscutter.command.Command;
import emu.grasscutter.command.CommandHandler; import emu.grasscutter.command.CommandHandler;
import emu.grasscutter.command.PermissionHandler;
import emu.grasscutter.game.player.Player; import emu.grasscutter.game.player.Player;
import emu.grasscutter.server.game.GameServer; import emu.grasscutter.server.game.GameServer;
import emu.grasscutter.server.http.HttpServer; import emu.grasscutter.server.http.HttpServer;
@ -97,4 +98,12 @@ public final class ServerHook {
public void setAuthSystem(AuthenticationSystem authSystem) { public void setAuthSystem(AuthenticationSystem authSystem) {
Grasscutter.setAuthenticationSystem(authSystem); Grasscutter.setAuthenticationSystem(authSystem);
} }
/**
* Sets the server's permission handler.
* @param permHandler An instance of the permission handler.
*/
public void setPermissionHandler(PermissionHandler permHandler) {
Grasscutter.setPermissionHandler(permHandler);
}
} }