Initial start on permissions

This commit is contained in:
Konicai 2023-06-20 15:35:01 -04:00
parent 2435a33414
commit e3532fa4f9
No known key found for this signature in database
GPG Key ID: 710D09287708C823
7 changed files with 226 additions and 3 deletions

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event.lifecycle;
import org.geysermc.event.Event;
import org.geysermc.event.PostOrder;
import org.geysermc.geyser.api.permission.PermissionChecker;
/**
* Fired by any permission manager implementations that wish to add support for custom permission checking.
* This event is not guaranteed to be fired.
* <p>
* Subscribing to this event with an earlier {@link PostOrder} and registering a {@link PermissionChecker}
* will result in that checker having a higher priority than others.
*/
public interface GeyserRegisterPermissionCheckersEvent extends Event {
void register(PermissionChecker checker);
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.event.lifecycle;
import org.geysermc.event.Event;
import org.geysermc.geyser.api.util.TriState;
/**
* Fired by any implementations that want to gather permission nodes and defaults
*/
public interface GeyserRegisterPermissionsEvent extends Event {
/**
* Registers a permission node with the permission system being used by Geyser's command manager.
*
* @param permission the permission node to register
* @param def the default value of the node
*/
void register(String permission, TriState def);
}

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.api.permission;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.api.command.CommandSource;
import org.geysermc.geyser.api.util.TriState;
public interface PermissionChecker {
TriState hasPermission(@NonNull CommandSource source, @NonNull String permission);
}

View File

@ -220,7 +220,9 @@ public class GeyserStandaloneBootstrap implements GeyserBootstrap {
geyser = GeyserImpl.load(PlatformType.STANDALONE, this);
GeyserImpl.start();
geyserCommandManager = new GeyserCommandManager(geyser, new GeyserStandaloneCommandManager());
GeyserStandaloneCommandManager cloud = new GeyserStandaloneCommandManager(geyser);
geyserCommandManager = new GeyserCommandManager(geyser, cloud);
cloud.gatherPermissions();
if (gui != null) {
gui.setupInterface(geyserLogger, geyserCommandManager);

View File

@ -30,18 +30,65 @@ import cloud.commandframework.execution.CommandExecutionCoordinator;
import cloud.commandframework.internal.CommandRegistrationHandler;
import cloud.commandframework.meta.CommandMeta;
import cloud.commandframework.meta.SimpleCommandMeta;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.geysermc.geyser.GeyserImpl;
import org.geysermc.geyser.api.event.lifecycle.GeyserRegisterPermissionCheckersEvent;
import org.geysermc.geyser.api.event.lifecycle.GeyserRegisterPermissionsEvent;
import org.geysermc.geyser.api.permission.PermissionChecker;
import org.geysermc.geyser.api.util.TriState;
import org.geysermc.geyser.command.GeyserCommandSource;
import org.geysermc.geyser.util.FileUtils;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
public class GeyserStandaloneCommandManager extends CommandManager<GeyserCommandSource> {
protected GeyserStandaloneCommandManager() {
private final GeyserImpl geyser;
private final List<PermissionChecker> permissionCheckers = new ArrayList<>();
private final Set<String> basePermissions = new ObjectOpenHashSet<>();
public GeyserStandaloneCommandManager(GeyserImpl geyser) {
super(CommandExecutionCoordinator.simpleCoordinator(), CommandRegistrationHandler.nullCommandRegistrationHandler());
this.geyser = geyser;
// allow any extensions to customize permissions
geyser.getEventBus().fire((GeyserRegisterPermissionCheckersEvent) permissionCheckers::add);
// must still implement a basic permission system
try {
File permissionsFile = geyser.getBootstrap().getConfigFolder().resolve("permissions.yml").toFile();
FileUtils.fileOrCopiedFromResource(permissionsFile, "permissions.yml", geyser.getBootstrap());
PermissionConfiguration config = FileUtils.loadConfig(permissionsFile, PermissionConfiguration.class);
basePermissions.addAll(config.getDefaultPermissions());
} catch (Exception e) {
geyser.getLogger().warning("Failed to load permissions.yml");
e.printStackTrace();
}
}
public void gatherPermissions() {
geyser.getEventBus().fire((GeyserRegisterPermissionsEvent) (permission, def) -> {
if (def == TriState.TRUE) {
basePermissions.add(permission);
} else if (def == TriState.FALSE) {
basePermissions.remove(permission);
}
});
}
@Override
public boolean hasPermission(@NonNull GeyserCommandSource sender, @NonNull String permission) {
return false; // todo: commands
for (PermissionChecker checker : permissionCheckers) {
Boolean result = checker.hasPermission(sender, permission).toBoolean();
if (result != null) {
return result;
}
}
return basePermissions.contains(permission);
}
@Override

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2019-2023 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.platform.standalone;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import java.util.Collections;
import java.util.Set;
@Getter
@JsonIgnoreProperties(ignoreUnknown = true)
@SuppressWarnings("FieldMayBeFinal") // Jackson requires that the fields are not final
public class PermissionConfiguration {
@JsonProperty("default-permissions")
private Set<String> defaultPermissions = Collections.emptySet();
}

View File

@ -94,6 +94,18 @@ public class FileUtils {
return file;
}
/**
* Open the specified file or copy if from resources
*
* @param file File to open
* @param name Name of the resource get if needed
* @return File handle of the specified file
* @throws IOException if the file failed to copy from resource
*/
public static File fileOrCopiedFromResource(File file, String name, GeyserBootstrap bootstrap) throws IOException {
return fileOrCopiedFromResource(file, name, Function.identity(), bootstrap);
}
/**
* Writes the given data to the specified file on disk
*