Skip to content

Commit

Permalink
Ability to deny/allow commands without permission plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
hevav committed Jan 23, 2023
1 parent 5493d0e commit 5709af5
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 20 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.6
1.1.7
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
}

setGroup("net.elytrium")
setVersion("1.1.6")
setVersion("1.1.7")

java {
setSourceCompatibility(JavaVersion.VERSION_11)
Expand Down Expand Up @@ -221,4 +221,6 @@ String getCurrentShortRevision() {

setStandardOutput(outputStream)
}

return outputStream.toString().trim()
}
32 changes: 32 additions & 0 deletions src/main/java/net/elytrium/limboauth/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import net.elytrium.commons.kyori.serialization.Serializers;
import net.elytrium.limboapi.api.chunk.Dimension;
import net.elytrium.limboapi.api.player.GameMode;
import net.elytrium.limboauth.command.CommandPermissionState;
import net.elytrium.limboauth.dependencies.DatabaseLibrary;
import net.elytrium.limboauth.migration.MigrationHash;
import net.kyori.adventure.bossbar.BossBar;
Expand Down Expand Up @@ -286,6 +287,37 @@ public Title.Times toTimes() {
}
}

@Create
public MAIN.COMMAND_PERMISSION_STATE COMMAND_PERMISSION_STATE;

@Comment({
"Available values: FALSE, TRUE, PERMISSION",
" FALSE - the command will be disallowed",
" TRUE - the command will be allowed if player has false permission state",
" PERMISSION - the command will be allowed if player has true permission state"
})
public static class COMMAND_PERMISSION_STATE {
@Comment("Permission: limboauth.commands.changepassword")
public CommandPermissionState CHANGE_PASSWORD = CommandPermissionState.PERMISSION;
@Comment("Permission: limboauth.commands.destroysession")
public CommandPermissionState DESTROY_SESSION = CommandPermissionState.PERMISSION;
@Comment("Permission: limboauth.commands.premium")
public CommandPermissionState PREMIUM = CommandPermissionState.PERMISSION;
@Comment("Permission: limboauth.commands.totp")
public CommandPermissionState TOTP = CommandPermissionState.PERMISSION;
@Comment("Permission: limboauth.commands.unregister")
public CommandPermissionState UNREGISTER = CommandPermissionState.PERMISSION;

@Comment("Permission: limboauth.admin.forcechangepassword")
public CommandPermissionState FORCE_CHANGE_PASSWORD = CommandPermissionState.PERMISSION;
@Comment("Permission: limboauth.admin.forceunregister")
public CommandPermissionState FORCE_UNREGISTER = CommandPermissionState.PERMISSION;
@Comment("Permission: limboauth.admin.reload")
public CommandPermissionState RELOAD = CommandPermissionState.PERMISSION;
@Comment("Permission: limboauth.admin.help")
public CommandPermissionState HELP = CommandPermissionState.TRUE;
}

/*
@Create
public Settings.MAIN.EVENTS_PRIORITIES EVENTS_PRIORITIES;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.j256.ormlite.stmt.UpdateBuilder;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player;
import java.sql.SQLException;
import net.elytrium.commons.kyori.serialization.Serializer;
Expand Down Expand Up @@ -110,6 +109,7 @@ public void execute(SimpleCommand.Invocation invocation) {

@Override
public boolean hasPermission(SimpleCommand.Invocation invocation) {
return invocation.source().getPermissionValue("limboauth.commands.changepassword") == Tristate.TRUE;
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.CHANGE_PASSWORD
.hasPermission(invocation.source(), "limboauth.commands.changepassword");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2021 - 2023 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.limboauth.command;

import com.velocitypowered.api.permission.PermissionSubject;
import com.velocitypowered.api.permission.Tristate;
import java.util.function.BiFunction;

public enum CommandPermissionState {
FALSE((source, permission) -> false),
TRUE((source, permission) -> source.getPermissionValue(permission) != Tristate.FALSE),
PERMISSION(PermissionSubject::hasPermission);

private final BiFunction<PermissionSubject, String, Boolean> hasPermissionFunction;

CommandPermissionState(BiFunction<PermissionSubject, String, Boolean> hasPermissionFunction) {
this.hasPermissionFunction = hasPermissionFunction;
}

public boolean hasPermission(PermissionSubject permissionSubject, String permission) {
return this.hasPermissionFunction.apply(permissionSubject, permission);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player;
import net.elytrium.commons.kyori.serialization.Serializer;
import net.elytrium.limboauth.LimboAuth;
Expand Down Expand Up @@ -55,6 +54,7 @@ public void execute(SimpleCommand.Invocation invocation) {

@Override
public boolean hasPermission(SimpleCommand.Invocation invocation) {
return invocation.source().getPermissionValue("limboauth.commands.destroysession") == Tristate.TRUE;
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.DESTROY_SESSION
.hasPermission(invocation.source(), "imboauth.commands.destroysession");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ public void execute(SimpleCommand.Invocation invocation) {

@Override
public boolean hasPermission(SimpleCommand.Invocation invocation) {
return invocation.source().hasPermission("limboauth.admin.forcechangepassword");
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.FORCE_CHANGE_PASSWORD
.hasPermission(invocation.source(), "limboauth.admin.forcechangepassword");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void execute(SimpleCommand.Invocation invocation) {

@Override
public boolean hasPermission(SimpleCommand.Invocation invocation) {
return invocation.source().hasPermission("limboauth.admin.forceunregister");
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.FORCE_UNREGISTER
.hasPermission(invocation.source(), "limboauth.admin.forceunregister");
}
}
21 changes: 15 additions & 6 deletions src/main/java/net/elytrium/limboauth/command/LimboAuthCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ public void execute(SimpleCommand.Invocation invocation) {
}
}

@Override
public boolean hasPermission(Invocation invocation) {
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.HELP
.hasPermission(invocation.source(), "limboauth.commands.help");
}

private void showHelp(CommandSource source) {
HELP_MESSAGE.forEach(source::sendMessage);

Expand All @@ -108,23 +114,26 @@ private void showHelp(CommandSource source) {
}

private enum Subcommand {
RELOAD("Reload config.", (LimboAuthCommand parent, CommandSource source, String[] args) -> {
parent.plugin.reload();
source.sendMessage(LimboAuth.getSerializer().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD));
});
RELOAD("Reload config.", Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.RELOAD,
(LimboAuthCommand parent, CommandSource source, String[] args) -> {
parent.plugin.reload();
source.sendMessage(LimboAuth.getSerializer().deserialize(Settings.IMP.MAIN.STRINGS.RELOAD));
});

private final String command;
private final String description;
private final CommandPermissionState permissionState;
private final SubcommandExecutor executor;

Subcommand(String description, SubcommandExecutor executor) {
Subcommand(String description, CommandPermissionState permissionState, SubcommandExecutor executor) {
this.permissionState = permissionState;
this.command = this.name().toLowerCase(Locale.ROOT);
this.description = description;
this.executor = executor;
}

public boolean hasPermission(CommandSource source) {
return source.hasPermission("limboauth.admin." + this.command);
return this.permissionState.hasPermission(source, "limboauth.admin." + this.command);
}

public Component getMessageLine() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.j256.ormlite.dao.Dao;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player;
import java.sql.SQLException;
import java.util.Locale;
Expand Down Expand Up @@ -107,6 +106,7 @@ public void execute(SimpleCommand.Invocation invocation) {

@Override
public boolean hasPermission(SimpleCommand.Invocation invocation) {
return invocation.source().getPermissionValue("limboauth.commands.premium") == Tristate.TRUE;
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.PREMIUM
.hasPermission(invocation.source(), "limboauth.commands.premium");
}
}
4 changes: 2 additions & 2 deletions src/main/java/net/elytrium/limboauth/command/TotpCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import com.j256.ormlite.stmt.UpdateBuilder;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player;
import dev.samstevens.totp.qr.QrData;
import dev.samstevens.totp.recovery.RecoveryCodeGenerator;
Expand Down Expand Up @@ -187,6 +186,7 @@ public void execute(SimpleCommand.Invocation invocation) {

@Override
public boolean hasPermission(SimpleCommand.Invocation invocation) {
return invocation.source().getPermissionValue("limboauth.commands.totp") == Tristate.TRUE;
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.TOTP
.hasPermission(invocation.source(), "limboauth.commands.totp");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import com.j256.ormlite.dao.Dao;
import com.velocitypowered.api.command.CommandSource;
import com.velocitypowered.api.command.SimpleCommand;
import com.velocitypowered.api.permission.Tristate;
import com.velocitypowered.api.proxy.Player;
import java.sql.SQLException;
import java.util.Locale;
Expand Down Expand Up @@ -102,6 +101,7 @@ public void execute(SimpleCommand.Invocation invocation) {

@Override
public boolean hasPermission(SimpleCommand.Invocation invocation) {
return invocation.source().getPermissionValue("limboauth.commands.unregister") == Tristate.TRUE;
return Settings.IMP.MAIN.COMMAND_PERMISSION_STATE.UNREGISTER
.hasPermission(invocation.source(), "limboauth.commands.unregister");
}
}

0 comments on commit 5709af5

Please sign in to comment.