Skip to content

Commit

Permalink
Add an option to disable bypass permission cache
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMolkaPL authored and wizjany committed Jul 4, 2020
1 parent 36e42ee commit a97db0e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
Expand Up @@ -76,6 +76,7 @@ public abstract class ConfigurationManager {
public boolean migrateRegionsToUuid;
public boolean keepUnresolvedNames;
public boolean particleEffects;
public boolean disablePermissionCache;

@Unreported public Map<String, String> hostKeys = new HashMap<>();
public boolean hostKeysAllowFMLClients;
Expand Down
Expand Up @@ -63,6 +63,7 @@ public void load() {
usePlayerMove = config.getBoolean("use-player-move-event", true);
usePlayerTeleports = config.getBoolean("use-player-teleports", true);
particleEffects = config.getBoolean("use-particle-effects", true);
disablePermissionCache = config.getBoolean("disable-permission-cache", false);

deopOnJoin = config.getBoolean("security.deop-everyone-on-join", false);
blockInGameOp = config.getBoolean("security.block-in-game-op-command", false);
Expand Down
Expand Up @@ -48,6 +48,7 @@
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;
import java.util.logging.Level;

import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -57,11 +58,14 @@ public abstract class AbstractSessionManager implements SessionManager {
public static final int RUN_DELAY = 20;
public static final long SESSION_LIFETIME = 10;

private static final Predicate<WorldPlayerTuple> BYPASS_PERMISSION_TEST = tuple -> {
return tuple.getPlayer().hasPermission("worldguard.region.bypass." + tuple.getWorld().getName());
};

private final LoadingCache<WorldPlayerTuple, Boolean> bypassCache = CacheBuilder.newBuilder()
.maximumSize(1000)
.expireAfterWrite(2, TimeUnit.SECONDS)
.build(CacheLoader.from(tuple ->
tuple.getPlayer().hasPermission("worldguard.region.bypass." + tuple.getWorld().getName())));
.build(CacheLoader.from(BYPASS_PERMISSION_TEST::test));

private final LoadingCache<CacheKey, Session> sessions = CacheBuilder.newBuilder()
.expireAfterAccess(SESSION_LIFETIME, TimeUnit.MINUTES)
Expand Down Expand Up @@ -134,8 +138,13 @@ public boolean unregisterHandler(Handler.Factory<? extends Handler> factory) {
@Override
public boolean hasBypass(LocalPlayer player, World world) {
Session sess = getIfPresent(player);
return sess != null && !sess.hasBypassDisabled()
&& bypassCache.getUnchecked(new WorldPlayerTuple(world, player));
if (sess == null || sess.hasBypassDisabled()) {
return false;
}

WorldPlayerTuple tuple = new WorldPlayerTuple(world, player);
boolean disablePermissionCache = WorldGuard.getInstance().getPlatform().getGlobalStateManager().disablePermissionCache;
return disablePermissionCache ? BYPASS_PERMISSION_TEST.test(tuple) : bypassCache.getUnchecked(tuple);
}

@Override
Expand Down

0 comments on commit a97db0e

Please sign in to comment.