Begin working on permissions implementation. I don't know where every usage is but I've done a generalized implementation so far for commands.

This commit is contained in:
Taah 2022-05-23 22:44:08 -07:00
parent feba260744
commit 9fba895088
12 changed files with 416 additions and 20 deletions

35
pom.xml
View File

@ -40,6 +40,16 @@
<repositories> <repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>networkmanager-repo</id>
<url>https://repo.networkmanager.xyz/repository/maven-public/</url>
</repository>
<repository> <repository>
<id>atlas-nexus-01-totalfreedom-development</id> <id>atlas-nexus-01-totalfreedom-development</id>
<url>https://nexus-01.core.atlas-media.co.uk/repository/totalfreedom-development/</url> <url>https://nexus-01.core.atlas-media.co.uk/repository/totalfreedom-development/</url>
@ -244,6 +254,31 @@
<artifactId>org.eclipse.sisu.inject</artifactId> <artifactId>org.eclipse.sisu.inject</artifactId>
<version>0.3.4</version> <version>0.3.4</version>
</dependency> </dependency>
<dependency>
<groupId>com.github.MilkBowl</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.bstats</groupId>
<artifactId>bstats</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>nl.chimpgamer.networkmanager</groupId>
<artifactId>api</artifactId>
<version>2.11.4</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>io.github.slimjar</groupId>
<artifactId>slimjar</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> </dependencies>
<pluginRepositories> <pluginRepositories>

View File

@ -3,6 +3,7 @@ package me.totalfreedom.totalfreedommod;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.util.Properties; import java.util.Properties;
import me.totalfreedom.totalfreedommod.admin.ActivityLog; import me.totalfreedom.totalfreedommod.admin.ActivityLog;
import me.totalfreedom.totalfreedommod.admin.AdminList; import me.totalfreedom.totalfreedommod.admin.AdminList;
import me.totalfreedom.totalfreedommod.banning.BanManager; import me.totalfreedom.totalfreedommod.banning.BanManager;
@ -35,6 +36,10 @@ import me.totalfreedom.totalfreedommod.fun.Trailer;
import me.totalfreedom.totalfreedommod.httpd.HTTPDaemon; import me.totalfreedom.totalfreedommod.httpd.HTTPDaemon;
import me.totalfreedom.totalfreedommod.permissions.PermissionConfig; import me.totalfreedom.totalfreedommod.permissions.PermissionConfig;
import me.totalfreedom.totalfreedommod.permissions.PermissionManager; import me.totalfreedom.totalfreedommod.permissions.PermissionManager;
import me.totalfreedom.totalfreedommod.permissions.handler.DefaultPermissionHandler;
import me.totalfreedom.totalfreedommod.permissions.handler.IPermissionHandler;
import me.totalfreedom.totalfreedommod.permissions.handler.NMPermissionHandler;
import me.totalfreedom.totalfreedommod.permissions.handler.VaultPermissionHandler;
import me.totalfreedom.totalfreedommod.player.PlayerList; import me.totalfreedom.totalfreedommod.player.PlayerList;
import me.totalfreedom.totalfreedommod.punishments.PunishmentList; import me.totalfreedom.totalfreedommod.punishments.PunishmentList;
import me.totalfreedom.totalfreedommod.rank.RankManager; import me.totalfreedom.totalfreedommod.rank.RankManager;
@ -132,6 +137,8 @@ public class TotalFreedomMod extends JavaPlugin
public WorldEditBridge web; public WorldEditBridge web;
public WorldGuardBridge wgb; public WorldGuardBridge wgb;
public IPermissionHandler permissionHandler;
public static TotalFreedomMod getPlugin() public static TotalFreedomMod getPlugin()
{ {
return plugin; return plugin;
@ -208,6 +215,20 @@ public class TotalFreedomMod extends JavaPlugin
// Metrics @ https://bstats.org/plugin/bukkit/TotalFreedomMod/2966 // Metrics @ https://bstats.org/plugin/bukkit/TotalFreedomMod/2966
new Metrics(this, 2966); new Metrics(this, 2966);
if (getServer().getPluginManager().isPluginEnabled("NetworkManager"))
{
FLog.info("Using NetworkManager's permission handling");
this.permissionHandler = new NMPermissionHandler(this);
} else if (getServer().getPluginManager().isPluginEnabled("Vault"))
{
FLog.info("Using Vault's permission handling");
this.permissionHandler = new VaultPermissionHandler(this);
} else
{
FLog.info("Using Bukkit's native permission handling");
this.permissionHandler = new DefaultPermissionHandler();
}
} }
@Override @Override
@ -255,8 +276,7 @@ public class TotalFreedomMod extends JavaPlugin
date = props.getProperty("buildDate", "unknown"); date = props.getProperty("buildDate", "unknown");
// Need to do this or it will display ${git.commit.id.abbrev} // Need to do this or it will display ${git.commit.id.abbrev}
head = props.getProperty("buildHead", "unknown").replace("${git.commit.id.abbrev}", "unknown"); head = props.getProperty("buildHead", "unknown").replace("${git.commit.id.abbrev}", "unknown");
} } catch (Exception ex)
catch (Exception ex)
{ {
FLog.severe("Could not load build properties! Did you compile with NetBeans/Maven?"); FLog.severe("Could not load build properties! Did you compile with NetBeans/Maven?");
FLog.severe(ex); FLog.severe(ex);

View File

@ -2,17 +2,22 @@ package me.totalfreedom.totalfreedommod.admin;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import me.totalfreedom.totalfreedommod.FreedomService; import me.totalfreedom.totalfreedommod.FreedomService;
import me.totalfreedom.totalfreedommod.config.ConfigEntry; import me.totalfreedom.totalfreedommod.config.ConfigEntry;
import me.totalfreedom.totalfreedommod.permissions.handler.NMPermissionHandler;
import me.totalfreedom.totalfreedommod.permissions.handler.VaultPermissionHandler;
import me.totalfreedom.totalfreedommod.rank.Rank; import me.totalfreedom.totalfreedommod.rank.Rank;
import me.totalfreedom.totalfreedommod.util.FLog; import me.totalfreedom.totalfreedommod.util.FLog;
import me.totalfreedom.totalfreedommod.util.FUtil; import me.totalfreedom.totalfreedommod.util.FUtil;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class AdminList extends FreedomService public class AdminList extends FreedomService
@ -55,8 +60,7 @@ public class AdminList extends FreedomService
allAdmins.add(admin); allAdmins.add(admin);
} }
} }
} } catch (SQLException e)
catch (SQLException e)
{ {
FLog.severe("Failed to load admin list: " + e.getMessage()); FLog.severe("Failed to load admin list: " + e.getMessage());
} }
@ -121,21 +125,41 @@ public class AdminList extends FreedomService
{ {
return true; return true;
} }
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("AdminList#isAdmin: Returning false because there is no permissions plugin that supports groups on the server");
return false;
Admin admin = getAdmin(player); /*Admin admin = getAdmin(player);
return admin != null && admin.isActive(); return admin != null && admin.isActive();*/
} }
public boolean isSeniorAdmin(CommandSender sender) public boolean isSeniorAdmin(CommandSender sender)
{ {
Admin admin = getAdmin(sender); //TODO: BukkitTelnet checks, but fuck that plugin
if (sender instanceof ConsoleCommandSender)
{
return true;
}
if (sender instanceof Player player && (plugin.permissionHandler instanceof NMPermissionHandler || plugin.permissionHandler instanceof VaultPermissionHandler))
{
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");
return false;
// return plugin.permissionHandler.hasPermission(sender, "totalfreedommod.admin");
/*Admin admin = getAdmin(sender);
if (admin == null) if (admin == null)
{ {
return false; return false;
} }
return admin.getRank().ordinal() >= Rank.SENIOR_ADMIN.ordinal(); return admin.getRank().ordinal() >= Rank.SENIOR_ADMIN.ordinal();*/
} }
public Admin getAdmin(CommandSender sender) public Admin getAdmin(CommandSender sender)
@ -271,13 +295,13 @@ public class AdminList extends FreedomService
ResultSet currentSave = plugin.sql.getAdminByUuid(admin.getUuid()); ResultSet currentSave = plugin.sql.getAdminByUuid(admin.getUuid());
for (Map.Entry<String, Object> entry : admin.toSQLStorable().entrySet()) for (Map.Entry<String, Object> entry : admin.toSQLStorable().entrySet())
{ {
Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue()); if (storedValue != null && !storedValue.equals(entry.getValue()) || storedValue == null && entry.getValue() != null || entry.getValue() == null) Object storedValue = plugin.sql.getValue(currentSave, entry.getKey(), entry.getValue());
if (storedValue != null && !storedValue.equals(entry.getValue()) || storedValue == null && entry.getValue() != null || entry.getValue() == null)
{ {
plugin.sql.setAdminValue(admin, entry.getKey(), entry.getValue()); plugin.sql.setAdminValue(admin, entry.getKey(), entry.getValue());
} }
} }
} } catch (SQLException e)
catch (SQLException e)
{ {
FLog.severe("Failed to save admin: " + e.getMessage()); FLog.severe("Failed to save admin: " + e.getMessage());
} }

View File

@ -10,9 +10,12 @@ public @interface CommandPermissions
Rank level() default Rank.NON_OP; Rank level() default Rank.NON_OP;
String permission() default "";
SourceType source() default SourceType.BOTH; SourceType source() default SourceType.BOTH;
boolean blockHostConsole() default false; boolean blockHostConsole() default false;
int cooldown() default 0; int cooldown() default 0;
} }

View File

@ -43,6 +43,8 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
private final String usage; private final String usage;
private final String aliases; private final String aliases;
private final Rank level; private final Rank level;
private final String permission;
private final SourceType source; private final SourceType source;
private final boolean blockHostConsole; private final boolean blockHostConsole;
private final int cooldown; private final int cooldown;
@ -59,6 +61,7 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
this.usage = params.usage(); this.usage = params.usage();
this.aliases = params.aliases(); this.aliases = params.aliases();
this.level = perms.level(); this.level = perms.level();
this.permission = perms.permission().isEmpty() ? "totalfreedommod." + this.name.toLowerCase() : perms.permission();
this.source = perms.source(); this.source = perms.source();
this.blockHostConsole = perms.blockHostConsole(); this.blockHostConsole = perms.blockHostConsole();
this.cooldown = perms.cooldown(); this.cooldown = perms.cooldown();
@ -162,8 +165,48 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
} }
} }
@Deprecated
protected void checkRank(@Deprecated Rank rank, String permission)
{
//TODO: Not bothering with BukkitTelnet now.
if (sender instanceof Player player)
{
if (!plugin.permissionHandler.hasPermission(player, permission))
{
noPerms();
return;
}
}
if (!plugin.rm.getRank(sender).isAtLeast(rank))
{
noPerms();
}
}
protected void checkRank(String permission)
{
//TODO: Not bothering with BukkitTelnet now.
if (sender instanceof Player player)
{
if (!plugin.permissionHandler.hasPermission(player, permission))
{
noPerms();
}
}
}
@Deprecated
protected void checkRank(Rank rank) protected void checkRank(Rank rank)
{ {
//TODO: Not bothering with BukkitTelnet now.
if (sender instanceof Player player)
{
if (!plugin.permissionHandler.hasPermission(player, this.permission))
{
noPerms();
return;
}
}
if (!plugin.rm.getRank(sender).isAtLeast(rank)) if (!plugin.rm.getRank(sender).isAtLeast(rank))
{ {
noPerms(); noPerms();

View File

@ -73,6 +73,9 @@ public enum ConfigEntry
SERVER_WHITELIST_MOTD(String.class, "server.motds.whitelist"), SERVER_WHITELIST_MOTD(String.class, "server.motds.whitelist"),
SERVER_FULL_MOTD(String.class, "server.motds.full"), SERVER_FULL_MOTD(String.class, "server.motds.full"),
// //
PERMISSIONS_GROUPS_ADMIN(String.class, "permissions.groups.admin"),
PERMISSIONS_GROUPS_SENIOR(String.class, "permissions.groups.senior_admin"),
DISCORD_TOKEN(String.class, "discord.token"), DISCORD_TOKEN(String.class, "discord.token"),
DISCORD_REPORT_CHANNEL_ID(String.class, "discord.report_channel_id"), DISCORD_REPORT_CHANNEL_ID(String.class, "discord.report_channel_id"),
DISCORD_CHAT_CHANNEL_ID(String.class, "discord.chat_channel_id"), DISCORD_CHAT_CHANNEL_ID(String.class, "discord.chat_channel_id"),

View File

@ -144,4 +144,5 @@ public class Module_list extends HTTPDModule
{ {
return "TotalFreedom - Online Players"; return "TotalFreedom - Online Players";
} }
} }

View File

@ -0,0 +1,43 @@
package me.totalfreedom.totalfreedommod.permissions.handler;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Taah
* @project TotalFreedomMod
* @since 9:10 PM [23-05-2022]
*/
public class DefaultPermissionHandler implements IPermissionHandler
{
@Override
public boolean hasPermission(@NotNull OfflinePlayer player, @Nullable String permission)
{
if (permission == null)
{
return true;
}
throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for permissions!");
}
@Override
public boolean hasPermission(@NotNull Player player, @Nullable String permission)
{
return permission == null || player.hasPermission(permission);
}
@Override
public boolean inGroup(@NotNull OfflinePlayer player, @Nullable String groupName)
{
throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for groups!");
}
@Override
public boolean inGroup(@NotNull Player player, @Nullable String groupName)
{
throw new UnsupportedOperationException("Unable to use Bukkit's native permission system for groups!");
}
}

View File

@ -0,0 +1,20 @@
package me.totalfreedom.totalfreedommod.permissions.handler;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Taah
* @project TotalFreedomMod
* @since 9:05 PM [23-05-2022]
*/
public interface IPermissionHandler
{
boolean hasPermission(@NotNull OfflinePlayer player, @Nullable String permission);
boolean hasPermission(@NotNull Player player, @Nullable String permission);
boolean inGroup(@NotNull OfflinePlayer player, @Nullable String groupName);
boolean inGroup(@NotNull Player player, @Nullable String groupName);
}

View File

@ -0,0 +1,95 @@
package me.totalfreedom.totalfreedommod.permissions.handler;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.milkbowl.vault.permission.Permission;
import nl.chimpgamer.networkmanager.api.NetworkManagerPlugin;
import nl.chimpgamer.networkmanager.api.NetworkManagerProvider;
import nl.chimpgamer.networkmanager.api.models.permissions.PermissionPlayer;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Taah
* @project TotalFreedomMod
* @since 9:10 PM [23-05-2022]
*/
public class NMPermissionHandler implements IPermissionHandler
{
private NetworkManagerPlugin plugin;
public NMPermissionHandler(TotalFreedomMod plugin)
{
if (plugin.permissionHandler != null)
{
return;
}
if (!Bukkit.getPluginManager().isPluginEnabled("NetworkManager"))
{
plugin.permissionHandler = new DefaultPermissionHandler();
return;
}
this.plugin = NetworkManagerProvider.get();
}
@Override
public boolean hasPermission(@NotNull OfflinePlayer player, @Nullable String permission)
{
if (permission == 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;
}
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;
}
return has;
}
@Override
public boolean hasPermission(@NotNull Player player, @Nullable String permission)
{
return hasPermission((OfflinePlayer) player, permission);
}
@Override
public boolean inGroup(@NotNull OfflinePlayer player, @Nullable String groupName)
{
if (groupName == null)
{
FLog.debug("NM Perms: Setting permission access to false, group is null");
return false;
}
PermissionPlayer permissionPlayer = this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId());
if (permissionPlayer == null)
{
FLog.debug("NM Perms: Setting permission access to false, player not found in NM for '" + player.getUniqueId() + "'");
return false;
}
if (permissionPlayer.getGroups().isEmpty())
{
FLog.debug("NM Perms: Setting permission access to false, player groups are empty for '" + player.getUniqueId() + "'");
return false;
}
return permissionPlayer.getGroups().stream().anyMatch(group -> group.getName().equals(groupName));
}
@Override
public boolean inGroup(@NotNull Player player, @Nullable String groupName)
{
return this.inGroup((OfflinePlayer) player, groupName);
}
}

View File

@ -0,0 +1,103 @@
package me.totalfreedom.totalfreedommod.permissions.handler;
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
import me.totalfreedom.totalfreedommod.util.FLog;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* @author Taah
* @project TotalFreedomMod
* @since 9:10 PM [23-05-2022]
*/
public class VaultPermissionHandler implements IPermissionHandler
{
private Permission permissions;
public VaultPermissionHandler(TotalFreedomMod plugin)
{
if (plugin.permissionHandler != null)
{
return;
}
if (!Bukkit.getPluginManager().isPluginEnabled("Vault"))
{
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();
plugin.permissionHandler = this;
}
@Override
public boolean hasPermission(@NotNull OfflinePlayer player, @Nullable String permission)
{
if (this.permissions == null)
{
FLog.debug("Can't use Vault permissions system, there is no plugin using Vault permissions.");
return true;
}
return permission == null || this.permissions.playerHas(null, player, permission);
}
@Override
public boolean hasPermission(@NotNull Player player, @Nullable String permission)
{
if (this.permissions == null)
{
FLog.debug("Can't use Vault permissions system, there is no plugin using Vault permissions.");
return true;
}
return permission == null || this.permissions.playerHas(player, permission);
}
@Override
public boolean inGroup(@NotNull OfflinePlayer player, @Nullable String groupName)
{
if (this.permissions == null)
{
FLog.debug("Can't use Vault permissions system for groups, there is no plugin using Vault permissions.");
return true;
}
if (groupName == null)
{
FLog.debug("Vault Perms: Group name is null, returning false for group check");
return false;
}
return this.permissions.playerInGroup(null, player, groupName);
}
@Override
public boolean inGroup(@NotNull Player player, @Nullable String groupName)
{
if (this.permissions == null)
{
FLog.debug("Can't use Vault permissions system for groups, there is no plugin using Vault permissions.");
return false;
}
if (groupName == null)
{
FLog.debug("Vault Perms: Group name is null, returning false for group check");
return false;
}
return this.permissions.playerInGroup(player, groupName);
}
public Permission getPermissions()
{
return permissions;
}
}

View File

@ -54,6 +54,12 @@ server:
# What to display at the bottom of the tab list # What to display at the bottom of the tab list
tablist_footer: '' tablist_footer: ''
# Permissions System
permissions:
groups:
admin: "Administrator"
senior_admin: "Senior-Administrator"
# Discord # Discord
discord: discord:
# If you do not have a token, make a bot account and get one at https://discordapp.com/developers/applications/me # If you do not have a token, make a bot account and get one at https://discordapp.com/developers/applications/me