-
-
Notifications
You must be signed in to change notification settings - Fork 941
Integrate with the official Fabric Permissions API #2990
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
Merged
+188
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
...t-fabric/src/main/java/com/sk89q/worldedit/fabric/internal/FabricPermissionsProvider.java
This file contains hidden or 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,111 @@ | ||
| /* | ||
| * WorldEdit, a Minecraft world manipulation toolkit | ||
| * Copyright (C) sk89q <http://www.sk89q.com> | ||
| * Copyright (C) WorldEdit team and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU 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 General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.sk89q.worldedit.fabric.internal; | ||
|
|
||
| import com.sk89q.worldedit.coremc.CoreMcPermissionsProvider; | ||
| import net.fabricmc.fabric.api.permission.v1.PermissionNode; | ||
| import net.minecraft.resources.Identifier; | ||
| import net.minecraft.server.level.ServerPlayer; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.HexFormat; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| final class FabricPermissionsProvider implements CoreMcPermissionsProvider { | ||
| private final CoreMcPermissionsProvider fallback; | ||
| private final Map<String, PermissionNode<Boolean>> registeredPermissions = new ConcurrentHashMap<>(); | ||
|
|
||
| FabricPermissionsProvider(CoreMcPermissionsProvider fallback) { | ||
| this.fallback = fallback; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean hasPermission(ServerPlayer player, String permission) { | ||
| PermissionNode<Boolean> node = registeredPermissions.get(permission); | ||
| if (node == null) { | ||
| node = createNode(permission); | ||
| } | ||
|
|
||
| if (node == null) { | ||
| // This permission node doesn't have the namespace/path format expected by the official Fabric perms API, | ||
| // so delegate to the fallback in the hopes it can handle it. | ||
| return fallback.hasPermission(player, permission); | ||
| } | ||
|
|
||
| Boolean result = player.checkPermission(node); | ||
| return result != null ? result : fallback.hasPermission(player, permission); | ||
| } | ||
|
|
||
| @Override | ||
| public void registerPermission(String permission) { | ||
| if (registeredPermissions.containsKey(permission)) { | ||
| // Short-circuit if already registered. | ||
| return; | ||
| } | ||
|
|
||
| PermissionNode<Boolean> node = createNode(permission); | ||
| if (node != null) { | ||
| registeredPermissions.putIfAbsent(permission, node); | ||
| } | ||
| fallback.registerPermission(permission); | ||
| } | ||
|
|
||
| private static @Nullable PermissionNode<Boolean> createNode(String permission) { | ||
| Identifier identifier = identifierFor(permission); | ||
| // It's theoretically possible some of our permission nodes don't have a namespace and path, | ||
| // so return null here and allow a fallback later on. | ||
| return identifier != null ? PermissionNode.of(identifier) : null; | ||
| } | ||
|
|
||
| static @Nullable Identifier identifierFor(String permission) { | ||
| int namespaceSeparator = permission.indexOf('.'); | ||
| if (namespaceSeparator < 1 || namespaceSeparator == permission.length() - 1) { | ||
| return null; | ||
| } | ||
|
|
||
| // Our permission nodes are in a.b.c.d format, Fabric expects modid:b.c.d, so we convert it here. | ||
| return Identifier.fromNamespaceAndPath( | ||
| encodeIdentifierPart(permission.substring(0, namespaceSeparator), false), | ||
| encodeIdentifierPart(permission.substring(namespaceSeparator + 1), true) | ||
| ); | ||
| } | ||
|
|
||
| private static String encodeIdentifierPart(String value, boolean allowSlash) { | ||
| // Fabric uses Identifier which has a stricter character set, so escape anything they cannot represent directly. | ||
| byte[] bytes = value.toLowerCase(Locale.ROOT).getBytes(StandardCharsets.UTF_8); | ||
| StringBuilder encoded = new StringBuilder(bytes.length); | ||
| for (byte rawByte : bytes) { | ||
| int valueByte = Byte.toUnsignedInt(rawByte); | ||
| if ((valueByte >= 'a' && valueByte <= 'z') | ||
| || (valueByte >= '0' && valueByte <= '9') | ||
| || valueByte == '.' || valueByte == '-' | ||
| || (allowSlash && valueByte == '/')) { | ||
| encoded.append((char) valueByte); | ||
| } else { | ||
| encoded.append('_'); | ||
| encoded.append(HexFormat.of().toHexDigits(rawByte)); | ||
| } | ||
| } | ||
| return encoded.toString(); | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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
74 changes: 74 additions & 0 deletions
74
...bric/src/test/java/com/sk89q/worldedit/fabric/internal/FabricPermissionsProviderTest.java
This file contains hidden or 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,74 @@ | ||
| /* | ||
| * WorldEdit, a Minecraft world manipulation toolkit | ||
| * Copyright (C) sk89q <http://www.sk89q.com> | ||
| * Copyright (C) WorldEdit team and contributors | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU 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 General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
| */ | ||
|
|
||
| package com.sk89q.worldedit.fabric.internal; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
|
||
| class FabricPermissionsProviderTest { | ||
|
|
||
| @Test | ||
| void mapsStandardPermission() { | ||
| assertEquals( | ||
| "worldedit:region.set", | ||
| FabricPermissionsProvider.identifierFor("worldedit.region.set").toString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void normalizesCase() { | ||
| assertEquals( | ||
| "worldedit:scripting.execute.build.js", | ||
| FabricPermissionsProvider.identifierFor("WorldEdit.scripting.execute.Build.js").toString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void escapesInvalidCharactersAndUnderscores() { | ||
| assertEquals( | ||
| "worldedit:scripting.execute.my_20script_5fv1_21", | ||
| FabricPermissionsProvider.identifierFor("worldedit.scripting.execute.my script_v1!").toString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void escapesUtf8Bytes() { | ||
| assertEquals( | ||
| "worldedit:scripting.execute.caf_c3_a9", | ||
| FabricPermissionsProvider.identifierFor("worldedit.scripting.execute.café").toString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void preservesPathSeparators() { | ||
| assertEquals( | ||
| "worldedit:scripting.execute/tools/build.js", | ||
| FabricPermissionsProvider.identifierFor("worldedit.scripting.execute/tools/build.js").toString() | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsInvalidPermissionNodes() { | ||
| assertNull(FabricPermissionsProvider.identifierFor("worldedit")); | ||
| assertNull(FabricPermissionsProvider.identifierFor("worldedit.")); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.