Skip to content

Commit

Permalink
Improved offline permission query system
Browse files Browse the repository at this point in the history
  • Loading branch information
DevLeoko committed Oct 25, 2020
1 parent 69f3eda commit 922c9e4
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import me.leoko.advancedban.manager.DatabaseManager;
import me.leoko.advancedban.manager.PunishmentManager;
import me.leoko.advancedban.manager.UUIDManager;
import me.leoko.advancedban.utils.Permissionable;
import me.leoko.advancedban.utils.Punishment;
import me.leoko.advancedban.utils.tabcompletion.TabCompleter;
import org.bstats.bukkit.Metrics;
Expand Down Expand Up @@ -183,16 +184,12 @@ public boolean hasPerms(Object player, String perms) {
}

@Override
public boolean hasOfflinePerms(String name, String perms) {
if (permissionVault == null)
return false;

public Permissionable getOfflinePermissionPlayer(String name) {
OfflinePlayer player = Bukkit.getOfflinePlayer(name);
if(permissionVault == null || player == null || !player.hasPlayedBefore())
return permission -> false;

if (player == null || !player.hasPlayedBefore())
return false;

return permissionVault.apply(player, perms);
return permission -> permissionVault.apply(player, permission);
}

@SuppressWarnings("deprecation")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
import me.leoko.advancedban.bungee.event.PunishmentEvent;
import me.leoko.advancedban.bungee.event.RevokePunishmentEvent;
import me.leoko.advancedban.bungee.listener.CommandReceiverBungee;
import me.leoko.advancedban.bungee.utils.LuckPermsPermissionProvider;
import me.leoko.advancedban.bungee.utils.OfflinePermissionProvider;
import me.leoko.advancedban.bungee.utils.LuckPermsOfflineUser;
import me.leoko.advancedban.manager.DatabaseManager;
import me.leoko.advancedban.manager.PunishmentManager;
import me.leoko.advancedban.manager.UUIDManager;
import me.leoko.advancedban.utils.Permissionable;
import me.leoko.advancedban.utils.Punishment;
import me.leoko.advancedban.utils.tabcompletion.TabCompleter;
import net.md_5.bungee.api.ChatColor;
Expand Down Expand Up @@ -52,13 +52,14 @@ public class BungeeMethods implements MethodInterface {
private Configuration layouts;
private Configuration mysql;

private OfflinePermissionProvider offlinePermissionProvider;
private final boolean luckPermsSupport;

public BungeeMethods() {
if (ProxyServer.getInstance().getPluginManager().getPlugin("LuckPerms") != null) {
offlinePermissionProvider = new LuckPermsPermissionProvider();
luckPermsSupport = true;
log("[AdvancedBan] Offline permission support through LuckPerms active");
} else {
luckPermsSupport = false;
log("[AdvancedBan] No offline permission support through LuckPerms");
}
}
Expand Down Expand Up @@ -185,22 +186,10 @@ public boolean hasPerms(Object player, String perms) {
}

@Override
public void requestOfflinePermissionPlayer(String name) {
if(offlinePermissionProvider != null) {
offlinePermissionProvider.requestOfflinePermissionPlayer(name);
}
}
public Permissionable getOfflinePermissionPlayer(String name) {
if(luckPermsSupport) return new LuckPermsOfflineUser(name);

@Override
public void releaseOfflinePermissionPlayer(String name) {
if(offlinePermissionProvider != null) {
offlinePermissionProvider.releaseOfflinePermissionPlayer(name);
}
}

@Override
public boolean hasOfflinePerms(String name, String perms) {
return offlinePermissionProvider != null && offlinePermissionProvider.hasOfflinePerms(name, perms);
return permission -> false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package me.leoko.advancedban.bungee.utils;

import me.leoko.advancedban.utils.Permissionable;
import net.luckperms.api.LuckPermsProvider;
import net.luckperms.api.model.user.User;
import net.luckperms.api.model.user.UserManager;

import java.util.UUID;

public class LuckPermsOfflineUser implements Permissionable {
private User permissionUser;

public LuckPermsOfflineUser(String name) {
final UserManager userManager = LuckPermsProvider.get().getUserManager();
final UUID uuid = userManager.lookupUniqueId(name).join();
if (uuid != null) {
this.permissionUser = userManager.loadUser(uuid).join();
}
}

@Override
public boolean hasPermission(String permission) {
return permissionUser != null && permissionUser.getCachedData().getPermissionData().checkPermission(permission).asBoolean();
}
}

This file was deleted.

This file was deleted.

24 changes: 5 additions & 19 deletions core/src/main/java/me/leoko/advancedban/MethodInterface.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.leoko.advancedban;

import me.leoko.advancedban.utils.Permissionable;
import me.leoko.advancedban.utils.Punishment;
import me.leoko.advancedban.utils.tabcompletion.TabCompleter;

Expand Down Expand Up @@ -151,27 +152,12 @@ public interface MethodInterface {
boolean hasPerms(Object player, String perms);

/**
* Load necessary permission data for an offline permission check
* Get a Permissionable player that can be checked for offline permissions.
*
* @param name the player's name
* @param name the name of the user
* @return the offline permission user
*/
default void requestOfflinePermissionPlayer(String name){}

/**
* Release cached permission data of a player after all requests are done
*
* @param name the player's name
*/
default void releaseOfflinePermissionPlayer(String name){}

/**
* Check if an offline player has the given permission.
*
* @param name server intern identifier for player
* @param perms the perms
* @return the boolean
*/
boolean hasOfflinePerms(String name, String perms);
Permissionable getOfflinePermissionPlayer(String name);

/**
* Check whether player is online.
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/me/leoko/advancedban/utils/Permissionable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package me.leoko.advancedban.utils;

/**
* The Permissionable interface defines a way to query whether the implementing object has a certain permission.
*/
@FunctionalInterface
public interface Permissionable {
/**
* Checks whether the implementing object has the given permission
*
* @param permission the permission to check
* @return the result
*/
boolean hasPermission(String permission);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import me.leoko.advancedban.manager.PunishmentManager;
import me.leoko.advancedban.manager.TimeManager;
import me.leoko.advancedban.utils.Command;
import me.leoko.advancedban.utils.Permissionable;
import me.leoko.advancedban.utils.Punishment;
import me.leoko.advancedban.utils.PunishmentType;

Expand Down Expand Up @@ -118,17 +119,17 @@ private static boolean processExempt(String name, String target, Object sender,
MethodInterface mi = Universal.get().getMethods();
String dataName = name.toLowerCase();

boolean onlineExempt = false;
boolean exempt = false;
if (mi.isOnline(dataName)) {
Object onlineTarget = mi.getPlayer(dataName);
onlineExempt = canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasPerms(onlineTarget, perms), type.getName());
exempt = canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasPerms(onlineTarget, perms), type.getName());
} else {
final Permissionable offlinePermissionPlayer = mi.getOfflinePermissionPlayer(name);
exempt = Universal.get().isExemptPlayer(dataName) ||
canNotPunish((perms) -> mi.hasPerms(sender, perms), offlinePermissionPlayer::hasPermission, type.getName());
}

mi.requestOfflinePermissionPlayer(name);
boolean offlineExempt = !onlineExempt && (Universal.get().isExemptPlayer(dataName) || canNotPunish((perms) -> mi.hasPerms(sender, perms), (perms) -> mi.hasOfflinePerms(name, perms), type.getName()));
mi.releaseOfflinePermissionPlayer(name);

if (onlineExempt || offlineExempt) {
if (exempt) {
MessageManager.sendMessage(sender, type.getBasic().getName() + ".Exempt",
true, "NAME", name);
return true;
Expand Down
10 changes: 5 additions & 5 deletions core/src/test/java/me/leoko/advancedban/TestMethods.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
package me.leoko.advancedban;

import me.leoko.advancedban.utils.Permissionable;
import me.leoko.advancedban.utils.Punishment;
import me.leoko.advancedban.utils.tabcompletion.TabCompleter;

import static org.junit.jupiter.api.Assertions.fail;

import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static org.junit.jupiter.api.Assertions.fail;

/**
* Created by Leo on 07.08.2017.
*/
public class TestMethods implements MethodInterface {

private final File dataFolder;

public TestMethods(File dataFolder){
Expand Down Expand Up @@ -128,8 +128,8 @@ public boolean hasPerms(Object player, String perms) {
}

@Override
public boolean hasOfflinePerms(String name, String perms) {
return false;
public Permissionable getOfflinePermissionPlayer(String name) {
return permission -> false;
}

@Override
Expand Down

0 comments on commit 922c9e4

Please sign in to comment.