-
Notifications
You must be signed in to change notification settings - Fork 307
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3137 from Multiverse/ben/mv5/command-permissions
Implement more customisable way of doing permission checking
- Loading branch information
Showing
6 changed files
with
123 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/org/mvplugins/multiverse/core/commandtools/MVCommandPermissions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package org.mvplugins.multiverse.core.commandtools; | ||
|
||
import co.aikar.commands.CommandIssuer; | ||
import co.aikar.commands.annotation.CommandPermission; | ||
import io.vavr.control.Option; | ||
import jakarta.inject.Inject; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jvnet.hk2.annotations.Service; | ||
import org.mvplugins.multiverse.core.permissions.CorePermissionsChecker; | ||
|
||
import java.util.HashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.function.Predicate; | ||
|
||
/** | ||
* Maps permission checking to custom logic for commands, to allow more complex permission checking. | ||
*/ | ||
@Service | ||
public class MVCommandPermissions { | ||
private final Map<String, Predicate<CommandIssuer>> permissionsCheckMap; | ||
|
||
@Inject | ||
MVCommandPermissions(@NotNull CorePermissionsChecker permissionsChecker) { | ||
permissionsCheckMap = new HashMap<>(); | ||
|
||
registerPermissionChecker("mvteleport", issuer -> permissionsChecker.hasAnyTeleportPermission(issuer.getIssuer())); | ||
registerPermissionChecker("mvspawn", issuer -> permissionsChecker.hasAnySpawnPermission(issuer.getIssuer())); | ||
} | ||
|
||
/** | ||
* Registers a custom permission checker callback. Use `@id-name` in {@link CommandPermission} decorator to use | ||
* the callback instead of the default permission string checking. | ||
* | ||
* @param id The permission id | ||
* @param checker The permission checker callback | ||
*/ | ||
public void registerPermissionChecker(String id, Predicate<CommandIssuer> checker) { | ||
permissionsCheckMap.put(prepareId(id), checker); | ||
} | ||
|
||
private static @NotNull String prepareId(String id) { | ||
return (id.startsWith("@") ? "" : "@") + id.toLowerCase(Locale.ENGLISH); | ||
} | ||
|
||
/** | ||
* Check if the issuer has the given permission. | ||
* @param issuer The issuer | ||
* @param permission The permission to check | ||
* @return True if the issuer has the permission | ||
*/ | ||
boolean hasPermission(CommandIssuer issuer, String permission) { | ||
return Option.of(permissionsCheckMap.get(permission)) | ||
.map(checker -> checker.test(issuer)) | ||
.getOrElse(() -> issuer.hasPermission(permission)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters