Start working on the new permission system

This commit is contained in:
Alexander Söderberg
2020-07-21 14:28:54 +02:00
parent 63ce3292aa
commit bfbb81030f
19 changed files with 637 additions and 34 deletions

View File

@ -34,6 +34,7 @@ import com.google.inject.TypeLiteral;
import com.plotsquared.bukkit.generator.BukkitPlotGenerator;
import com.plotsquared.bukkit.inject.BackupModule;
import com.plotsquared.bukkit.inject.BukkitModule;
import com.plotsquared.bukkit.inject.PermissionModule;
import com.plotsquared.bukkit.inject.WorldManagerModule;
import com.plotsquared.bukkit.listener.ChunkListener;
import com.plotsquared.bukkit.listener.EntitySpawnListener;
@ -248,8 +249,11 @@ import static com.plotsquared.core.util.ReflectionUtils.getRefClass;
// We create the injector after PlotSquared has been initialized, so that we have access
// to generated instances and settings
this.injector = Guice.createInjector(Stage.PRODUCTION, new WorldManagerModule(), new PlotSquaredModule(),
new BukkitModule(this), new BackupModule());
this.injector = Guice.createInjector(Stage.PRODUCTION, new PermissionModule(),
new WorldManagerModule(),
new PlotSquaredModule(),
new BukkitModule(this),
new BackupModule());
this.injector.injectMembers(this);
if (PremiumVerification.isPremium() && Settings.Enabled_Components.UPDATE_NOTIFICATIONS) {

View File

@ -0,0 +1,52 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.inject;
import com.google.inject.AbstractModule;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.plotsquared.bukkit.permissions.BukkitPermissionHandler;
import com.plotsquared.bukkit.permissions.VaultPermissionHandler;
import com.plotsquared.core.permissions.PermissionHandler;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.plugin.RegisteredServiceProvider;
public class PermissionModule extends AbstractModule {
@Provides @Singleton PermissionHandler providePermissionHandler() {
try {
RegisteredServiceProvider<Permission> permissionProvider =
Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
if (permissionProvider != null) {
return new VaultPermissionHandler();
}
} catch (final Exception ignored) {
}
return new BukkitPermissionHandler();
}
}

View File

@ -0,0 +1,84 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.permissions;
import com.plotsquared.bukkit.player.BukkitPlayer;
import com.plotsquared.core.permissions.ConsolePermissionProfile;
import com.plotsquared.core.permissions.PermissionHandler;
import com.plotsquared.core.permissions.PermissionProfile;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.OfflinePlotPlayer;
import com.plotsquared.core.player.PlotPlayer;
import org.bukkit.entity.Player;
import javax.annotation.Nonnull;
import java.lang.ref.WeakReference;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
public class BukkitPermissionHandler implements PermissionHandler {
@Override public void initialize() {
}
@Nonnull @Override public Optional<PermissionProfile> getPermissionProfile(
@Nonnull PlotPlayer<?> playerPlotPlayer) {
if (playerPlotPlayer instanceof BukkitPlayer) {
final BukkitPlayer bukkitPlayer = (BukkitPlayer) playerPlotPlayer;
return Optional.of(new BukkitPermissionProfile(bukkitPlayer.getPlatformPlayer()));
} else if (playerPlotPlayer instanceof ConsolePlayer) {
return Optional.of(ConsolePermissionProfile.INSTANCE);
}
return Optional.empty();
}
@Nonnull @Override public Optional<PermissionProfile> getPermissionProfile(
@Nonnull OfflinePlotPlayer offlinePlotPlayer) {
return Optional.empty();
}
@Nonnull @Override public Set<PermissionHandlerCapability> getCapabilities() {
return EnumSet.of(PermissionHandlerCapability.ONLINE_PERMISSIONS);
}
private static final class BukkitPermissionProfile implements PermissionProfile {
private final WeakReference<Player> playerReference;
private BukkitPermissionProfile(@Nonnull final Player player) {
this.playerReference = new WeakReference<>(player);
}
@Override public boolean hasPermission(@Nonnull final String permission) {
final Player player = this.playerReference.get();
return player != null && player.hasPermission(permission);
}
}
}

View File

@ -0,0 +1,102 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.permissions;
import com.plotsquared.bukkit.player.BukkitOfflinePlayer;
import com.plotsquared.bukkit.player.BukkitPlayer;
import com.plotsquared.core.permissions.ConsolePermissionProfile;
import com.plotsquared.core.permissions.PermissionHandler;
import com.plotsquared.core.permissions.PermissionProfile;
import com.plotsquared.core.player.ConsolePlayer;
import com.plotsquared.core.player.OfflinePlotPlayer;
import com.plotsquared.core.player.PlotPlayer;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.plugin.RegisteredServiceProvider;
import javax.annotation.Nonnull;
import java.util.EnumSet;
import java.util.Optional;
import java.util.Set;
public class VaultPermissionHandler implements PermissionHandler {
private Permission permissions;
@Override public void initialize() {
if (Bukkit.getServer().getPluginManager().getPlugin("Vault") == null) {
throw new IllegalStateException("Vault is not present on the server");
}
RegisteredServiceProvider<Permission> permissionProvider =
Bukkit.getServer().getServicesManager().getRegistration(Permission.class);
if (permissionProvider != null) {
this.permissions = permissionProvider.getProvider();
}
}
@Nonnull @Override public Optional<PermissionProfile> getPermissionProfile(
@Nonnull PlotPlayer<?> playerPlotPlayer) {
if (playerPlotPlayer instanceof BukkitPlayer) {
final BukkitPlayer bukkitPlayer = (BukkitPlayer) playerPlotPlayer;
return Optional.of(new VaultPermissionProfile(bukkitPlayer.getPlatformPlayer()));
} else if (playerPlotPlayer instanceof ConsolePlayer) {
return Optional.of(ConsolePermissionProfile.INSTANCE);
}
return Optional.empty();
}
@Nonnull @Override public Optional<PermissionProfile> getPermissionProfile(
@Nonnull OfflinePlotPlayer offlinePlotPlayer) {
if (offlinePlotPlayer instanceof BukkitOfflinePlayer) {
return Optional.of(new VaultPermissionProfile(((BukkitOfflinePlayer) offlinePlotPlayer).player));
}
return Optional.empty();
}
@Nonnull @Override public Set<PermissionHandlerCapability> getCapabilities() {
return EnumSet.of(PermissionHandlerCapability.ONLINE_PERMISSIONS, PermissionHandlerCapability.OFFLINE_PERMISSIONS);
}
private final class VaultPermissionProfile implements PermissionProfile {
private final OfflinePlayer offlinePlayer;
private VaultPermissionProfile(@Nonnull final OfflinePlayer offlinePlayer) {
this.offlinePlayer = offlinePlayer;
}
@Override public boolean hasPermission(@Nonnull final String permission) {
if (permissions == null) {
return false;
}
return permissions.playerHas(null, offlinePlayer, permission);
}
}
}

View File

@ -25,6 +25,9 @@
*/
package com.plotsquared.bukkit.player;
import com.plotsquared.core.permissions.NullPermissionProfile;
import com.plotsquared.core.permissions.PermissionHandler;
import com.plotsquared.core.permissions.PermissionProfile;
import com.plotsquared.core.player.OfflinePlotPlayer;
import org.bukkit.OfflinePlayer;
import javax.annotation.Nonnull;
@ -34,6 +37,7 @@ import java.util.UUID;
public class BukkitOfflinePlayer implements OfflinePlotPlayer {
public final OfflinePlayer player;
private final PermissionProfile permissionProfile;
/**
* Please do not use this method. Instead use BukkitUtil.getPlayer(Player),
@ -41,8 +45,11 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer {
*
* @param player
*/
public BukkitOfflinePlayer(OfflinePlayer player) {
public BukkitOfflinePlayer(@Nonnull final OfflinePlayer player, @Nonnull final
PermissionHandler permissionHandler) {
this.player = player;
this.permissionProfile = permissionHandler.getPermissionProfile(this)
.orElse(NullPermissionProfile.INSTANCE);
}
@Nonnull @Override public UUID getUUID() {
@ -60,4 +67,9 @@ public class BukkitOfflinePlayer implements OfflinePlotPlayer {
@Override public String getName() {
return this.player.getName();
}
@Override public boolean hasPermission(@Nonnull final String permission) {
return this.permissionProfile.hasPermission(permission);
}
}

View File

@ -32,6 +32,7 @@ import com.plotsquared.core.configuration.Captions;
import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.events.TeleportCause;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.permissions.PermissionHandler;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.PlotWeather;
import com.plotsquared.core.plot.world.PlotAreaManager;
@ -83,19 +84,21 @@ public class BukkitPlayer extends PlotPlayer<Player> {
* @param player Bukkit player instance
*/
public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher,
@Nonnull final Player player, @Nullable final EconHandler econHandler) {
this(plotAreaManager, eventDispatcher, player, false, econHandler);
@Nonnull final Player player, @Nullable final EconHandler econHandler, @Nonnull final PermissionHandler permissionHandler) {
this(plotAreaManager, eventDispatcher, player, false, econHandler, permissionHandler);
}
public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final EventDispatcher eventDispatcher,
@Nonnull final Player player, final boolean offline, @Nullable final EconHandler econHandler) {
this(plotAreaManager, eventDispatcher, player, offline, true, econHandler);
@Nonnull final Player player, final boolean offline, @Nullable final EconHandler econHandler,
@Nonnull final PermissionHandler permissionHandler) {
this(plotAreaManager, eventDispatcher, player, offline, true, econHandler, permissionHandler);
}
public BukkitPlayer(@Nonnull final PlotAreaManager plotAreaManager, @Nonnull final
EventDispatcher eventDispatcher, @Nonnull final Player player, final boolean offline,
final boolean realPlayer, @Nullable final EconHandler econHandler) {
super(plotAreaManager, eventDispatcher, econHandler);
final boolean realPlayer, @Nullable final EconHandler econHandler,
@Nonnull final PermissionHandler permissionHandler) {
super(plotAreaManager, eventDispatcher, econHandler, permissionHandler);
this.player = player;
this.offline = offline;
this.econHandler = econHandler;
@ -161,13 +164,6 @@ public class BukkitPlayer extends PlotPlayer<Player> {
}
}
@Override public boolean hasPermission(final String permission) {
if (this.offline && this.econHandler != null) {
return this.econHandler.hasPermission(getName(), permission);
}
return this.player.hasPermission(permission);
}
@Override public int hasPermissionRange(final String stub, final int range) {
if (hasPermission(Captions.PERMISSION_ADMIN.getTranslated())) {
return Integer.MAX_VALUE;

View File

@ -27,6 +27,7 @@ package com.plotsquared.bukkit.player;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.plotsquared.core.permissions.PermissionHandler;
import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.EconHandler;
import com.plotsquared.core.util.EventDispatcher;
@ -46,13 +47,16 @@ import java.util.UUID;
private final PlotAreaManager plotAreaManager;
private final EventDispatcher eventDispatcher;
private final EconHandler econHandler;
private final PermissionHandler permissionHandler;
@Inject public BukkitPlayerManager(@Nonnull final PlotAreaManager plotAreaManager,
@Nonnull final EventDispatcher eventDispatcher,
@Nullable final EconHandler econHandler) {
@Nullable final EconHandler econHandler,
@Nonnull final PermissionHandler permissionHandler) {
this.plotAreaManager = plotAreaManager;
this.eventDispatcher = eventDispatcher;
this.econHandler = econHandler;
this.permissionHandler = permissionHandler;
}
@Nonnull @Override public BukkitPlayer getPlayer(@Nonnull final Player object) {
@ -60,7 +64,7 @@ import java.util.UUID;
return getPlayer(object.getUniqueId());
} catch (final NoSuchPlayerException exception) {
return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, object,
object.isOnline(), false, this.econHandler);
object.isOnline(), false, this.econHandler, this.permissionHandler);
}
}
@ -69,18 +73,18 @@ import java.util.UUID;
if (player == null || !player.isOnline()) {
throw new NoSuchPlayerException(uuid);
}
return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, this.econHandler);
return new BukkitPlayer(this.plotAreaManager, this.eventDispatcher, player, this.econHandler, this.permissionHandler);
}
@Nullable @Override public BukkitOfflinePlayer getOfflinePlayer(@Nullable final UUID uuid) {
if (uuid == null) {
return null;
}
return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid));
return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(uuid), this.permissionHandler);
}
@Nonnull @Override public BukkitOfflinePlayer getOfflinePlayer(@Nonnull final String username) {
return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(username));
return new BukkitOfflinePlayer(Bukkit.getOfflinePlayer(username), this.permissionHandler);
}
}

View File

@ -131,7 +131,7 @@ import java.util.stream.Stream;
final Player player = OfflinePlayerUtil.loadPlayer(op);
player.loadData();
return new BukkitPlayer(PlotSquared.get().getPlotAreaManager(),
PlotSquared.get().getEventDispatcher(), player, true, PlotSquared.platform().getEconHandler());
PlotSquared.get().getEventDispatcher(), player, true, PlotSquared.platform().getEconHandler(), PlotSquared.platform().getPermissionHandler());
}
/**