Properly check for permissions using NM,

add prefixes support to chats
Fix up the list module a bit more
This commit is contained in:
Taah 2022-05-24 18:32:27 -07:00
parent 54b803e09b
commit f63362a9fb
9 changed files with 112 additions and 26 deletions

View File

@ -127,10 +127,10 @@ public class AdminList extends FreedomService
}
if (plugin.permissionHandler instanceof NMPermissionHandler || plugin.permissionHandler instanceof VaultPermissionHandler)
{
FLog.debug("Using " + plugin.permissionHandler.getClass().getSimpleName() + " for AdminList#isAdmin");
return plugin.permissionHandler.inGroup(player, ConfigEntry.PERMISSIONS_GROUPS_SENIOR.getString());
// FLog.debug("Using " + plugin.permissionHandler.getClass().getSimpleName() + " for AdminList#isAdmin");
return plugin.permissionHandler.inGroup(player, ConfigEntry.PERMISSIONS_GROUPS_ADMIN.getString());
}
FLog.debug("AdminList#isAdmin: Returning false because there is no permissions plugin that supports groups on the server");
// FLog.debug("AdminList#isAdmin: Returning false because there is no permissions plugin that supports groups on the server");
return false;
/*Admin admin = getAdmin(player);
@ -147,10 +147,10 @@ public class AdminList extends FreedomService
}
if (sender instanceof Player player && (plugin.permissionHandler instanceof NMPermissionHandler || plugin.permissionHandler instanceof VaultPermissionHandler))
{
FLog.debug("Using " + plugin.permissionHandler.getClass().getSimpleName() + " for AdminList#isSeniorAdmin");
// FLog.debug("Using " + plugin.permissionHandler.getClass().getSimpleName() + " for AdminList#isSeniorAdmin");
return plugin.permissionHandler.inGroup(player, ConfigEntry.PERMISSIONS_GROUPS_SENIOR.getString());
}
FLog.debug("AdminList#isSeniorAdmin: Returning false because there is no permissions plugin that supports groups on the server");
// FLog.debug("AdminList#isSeniorAdmin: Returning false because there is no permissions plugin that supports groups on the server");
return false;
// return plugin.permissionHandler.hasPermission(sender, "totalfreedommod.admin");
/*Admin admin = getAdmin(sender);

View File

@ -197,16 +197,15 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
protected boolean checkPermissionsSilent(String permission)
{
//TODO: Not bothering with BukkitTelnet now.
// FLog.debug("Using checkPermissionsSilent");
if (sender instanceof ConsoleCommandSender)
{
return true;
}
if (sender instanceof Player player)
{
if (!plugin.permissionHandler.hasPermission(player, permission))
{
return false;
}
// FLog.debug("Checking player permission for " + player.getUniqueId() + " - " + permission);
return plugin.permissionHandler.hasPermission(player, permission);
}
return false;
}
@ -214,9 +213,11 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
@Deprecated
protected void checkRank(Rank rank)
{
// FLog.debug("Using checkRank");
//TODO: Not bothering with BukkitTelnet now.
if (sender instanceof Player player)
{
// FLog.debug("Checking player permission for " + player.getUniqueId() + " - " + this.permission);
if (!plugin.permissionHandler.hasPermission(player, this.permission))
{
noPerms();
@ -430,7 +431,9 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
return true;
}*/
if (!checkPermissionsSilent(perms.permission()))
FLog.debug("Result of check perms: " + (!checkPermissionsSilent(cmd.permission)));
if (!checkPermissionsSilent(cmd.permission))
{
msg(NO_PERMISSION);
return true;

View File

@ -76,7 +76,6 @@ public enum ConfigEntry
PERMISSIONS_GROUPS_ADMIN(String.class, "permissions.groups.admin"),
PERMISSIONS_GROUPS_SENIOR(String.class, "permissions.groups.senior_admin"),
PERMISSIONS_GROUPS_DEFAULT(String.class, "permissions.groups.default"),
DISCORD_TOKEN(String.class, "discord.token"),
DISCORD_REPORT_CHANNEL_ID(String.class, "discord.report_channel_id"),

View File

@ -6,6 +6,7 @@ import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.httpd.NanoHTTPD;
import me.totalfreedom.totalfreedommod.permissions.handler.DefaultPermissionHandler;
import me.totalfreedom.totalfreedommod.permissions.handler.IPermissionHandler;
import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
@ -28,9 +29,11 @@ public class Module_list extends HTTPDModule
{
if (params.get("json") != null && params.get("json").equals("true"))
{
FLog.debug("Swapping to JSON View!");
final JSONObject responseObject = new JSONObject();
if (!(plugin.permissionHandler instanceof DefaultPermissionHandler))
{
FLog.debug("Displaying custom list on HTTPD!");
IPermissionHandler handler = plugin.permissionHandler;
final JSONObject players = new JSONObject();

View File

@ -47,6 +47,12 @@ public class DefaultPermissionHandler implements IPermissionHandler
throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for groups!");
}
@Override
public String getPrefix(@NotNull Player player)
{
throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for groups!");
}
@Override
public String[] getGroups()
{

View File

@ -20,6 +20,7 @@ public interface IPermissionHandler
String getPrimaryGroup(@NotNull Player player);
String getPrefix(@NotNull Player player);
String[] getGroups();
}

View File

@ -1,6 +1,9 @@
package me.totalfreedom.totalfreedommod.permissions.handler;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.milkbowl.vault.permission.Permission;
import nl.chimpgamer.networkmanager.api.NetworkManagerPlugin;
@ -14,6 +17,8 @@ import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Comparator;
/**
* @author Taah
@ -43,20 +48,29 @@ public class NMPermissionHandler implements IPermissionHandler
{
if (permission == null)
{
FLog.debug("Permission was null!");
return true;
}
PermissionPlayer permissionPlayer = this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId());
if (permissionPlayer == null)
{
FLog.debug("Unable to find permissions player in NetworkManager. Returning true.");
return true;
FLog.debug("Unable to find permissions player in NetworkManager. Returning false.");
return false;
}
if (permissionPlayer.getAllPermissions() == null)
{
FLog.debug("Screw you NetworkManager for telling me all the permissions for player is null.");
return false;
}
Boolean has = permissionPlayer.hasPermission(permission);
if (has == null)
{
FLog.debug("NetworkManager is idiotic and has a chance of returning null on a Boolean object. Returning true.");
return true;
// FLog.debug("NetworkManager is idiotic and has a chance of returning null on a Boolean object. Returning false.");
return false;
}
// FLog.debug("Player has perm? " + has);
// FLog.debug("Player permissions for: " + permissionPlayer.getUuid() + "\n" + new GsonBuilder().setPrettyPrinting().create().toJson(permissionPlayer.getAllPermissions()));
// permissionPlayer.getPermissions().forEach(permission1 -> FLog.debug(String.format("%s:%s", permission1.getPermissionString(), permission1.hasExpired())));
return has;
}
@ -85,6 +99,9 @@ public class NMPermissionHandler implements IPermissionHandler
FLog.debug("NM Perms: Setting permission access to false, player groups are empty for '" + player.getUniqueId() + "'");
return false;
}
// FLog.debug("Group Name requested for: " + player.getUniqueId() + " - " + groupName);
// FLog.debug("Player has? " + (permissionPlayer.getGroups().stream().anyMatch(group -> group.getName().equals(groupName))));
// permissionPlayer.getGroups().forEach(group -> FLog.debug("Player group: " + group.getName()));
return permissionPlayer.getGroups().stream().anyMatch(group -> group.getName().equals(groupName));
}
@ -102,13 +119,32 @@ public class NMPermissionHandler implements IPermissionHandler
@Override
public String getPrimaryGroup(@NotNull Player player)
{
PermissionPlayer permissionPlayer = this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId());
if (permissionPlayer == null || permissionPlayer.getPrimaryGroup() == null)
{
FLog.warning("NM Perms: Couldn't find player's primary group due to them not be found our list");
return ConfigEntry.PERMISSIONS_GROUPS_DEFAULT.getString();
}
// permissionPlayer.getGroups().forEach(group -> FLog.debug(String.format("Group for %s: %s", permissionPlayer.getUuid(), group.getName())));
// FLog.debug(String.format("%s Primary Group: %s", permissionPlayer.getUuid(), permissionPlayer.getGroups().stream().sorted(Comparator.comparingInt(Group::getRank)).map(Group::getName).findFirst().orElse(ConfigEntry.PERMISSIONS_GROUPS_DEFAULT.getString())));
return permissionPlayer.getGroups().stream().sorted((o1, o2) -> o1.getRank() - o2.getRank()).map(Group::getName).findFirst().orElse(ConfigEntry.PERMISSIONS_GROUPS_DEFAULT.getString());
}
@Override
public String getPrefix(@NotNull Player player)
{
PermissionPlayer permissionPlayer = this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId());
if (permissionPlayer == null)
{
FLog.warning("NM Perms: Couldn't find player's primary group due to them not be found our list");
return "default";
return "[Error Loading Player]";
}
return this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId()).getPrimaryGroup().getName();
Group group = this.plugin.getPermissionManager().getGroup(getPrimaryGroup(player));
if (group == null)
{
return "[Error Finding Group]";
}
return group.getPrefix(null);
}
}

View File

@ -2,6 +2,7 @@ package me.totalfreedom.totalfreedommod.permissions.handler;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.milkbowl.vault.chat.Chat;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
@ -19,6 +20,7 @@ import org.jetbrains.annotations.Nullable;
public class VaultPermissionHandler implements IPermissionHandler
{
private Permission permissions;
private Chat chat;
public VaultPermissionHandler(TotalFreedomMod plugin)
{
@ -31,14 +33,10 @@ public class VaultPermissionHandler implements IPermissionHandler
plugin.permissionHandler = new DefaultPermissionHandler();
return;
}
RegisteredServiceProvider<Permission> rsp = Bukkit.getServicesManager().getRegistration(Permission.class);
if (rsp == null)
{
FLog.warning("Switching back to Bukkit's default permissions from Vault's due to no permission system found.");
plugin.permissionHandler = new DefaultPermissionHandler();
return;
}
this.permissions = rsp.getProvider();
this.permissions = setupPerms(plugin);
this.chat = setupChat(plugin);
plugin.permissionHandler = this;
}
@ -113,8 +111,42 @@ public class VaultPermissionHandler implements IPermissionHandler
return this.permissions.getPrimaryGroup(player);
}
@Override
public String getPrefix(@NotNull Player player)
{
if (this.chat == null)
{
return "[No Chat Plugin]";
}
return this.chat.getPlayerPrefix(player);
}
public Permission getPermissions()
{
return permissions;
}
private Permission setupPerms(TotalFreedomMod plugin)
{
RegisteredServiceProvider<Permission> rsp = Bukkit.getServicesManager().getRegistration(Permission.class);
if (rsp == null)
{
FLog.warning("Switching back to Bukkit's default permissions from Vault's due to no permission system found.");
plugin.permissionHandler = new DefaultPermissionHandler();
return null;
}
return rsp.getProvider();
}
private Chat setupChat(TotalFreedomMod plugin)
{
RegisteredServiceProvider<Chat> rsp = Bukkit.getServicesManager().getRegistration(Chat.class);
if (rsp == null)
{
// FLog.warning("Switching back to Bukkit's default permissions from Vault's due to no permission system found.");
// plugin.permissionHandler = new DefaultPermissionHandler();
return null;
}
return rsp.getProvider();
}
}

View File

@ -4,6 +4,7 @@ import java.util.Objects;
import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.permissions.handler.DefaultPermissionHandler;
import me.totalfreedom.totalfreedommod.player.FPlayer;
import me.totalfreedom.totalfreedommod.player.PlayerData;
import me.totalfreedom.totalfreedommod.util.FUtil;
@ -168,7 +169,12 @@ public class RankManager extends FreedomService
fPlayer.setTag(null);
player.setPlayerListName(null);
}
fPlayer.setTag(getTag(player, display.getColoredTag()));
if (!(plugin.permissionHandler instanceof DefaultPermissionHandler))
{
fPlayer.setTag(getTag(player, plugin.permissionHandler.getPrefix(player)));
} else {
fPlayer.setTag(getTag(player, display.getColoredTag()));
}
updatePlayerTeam(player);
plugin.pem.setPermissions(player);
}