Work on the module list to support permissions

This commit is contained in:
Taah 2022-05-23 23:15:12 -07:00
parent 9fba895088
commit 54b803e09b
9 changed files with 125 additions and 15 deletions

View File

@ -279,6 +279,12 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20220320</version>
</dependency>
</dependencies>
<pluginRepositories>

View File

@ -182,8 +182,7 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
noPerms();
}
}
protected void checkRank(String permission)
protected void checkPermissions(String permission)
{
//TODO: Not bothering with BukkitTelnet now.
if (sender instanceof Player player)
@ -195,6 +194,23 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
}
}
protected boolean checkPermissionsSilent(String permission)
{
//TODO: Not bothering with BukkitTelnet now.
if (sender instanceof ConsoleCommandSender)
{
return true;
}
if (sender instanceof Player player)
{
if (!plugin.permissionHandler.hasPermission(player, permission))
{
return false;
}
}
return false;
}
@Deprecated
protected void checkRank(Rank rank)
{
@ -408,7 +424,13 @@ public abstract class FreedomCommand implements CommandExecutor, TabCompleter
public boolean func2()
{
if (!plugin.rm.getRank(sender).isAtLeast(perms.level()))
/*if (!plugin.rm.getRank(sender).isAtLeast(perms.level()))
{
msg(NO_PERMISSION);
return true;
}*/
if (!checkPermissionsSilent(perms.permission()))
{
msg(NO_PERMISSION);
return true;

View File

@ -76,6 +76,8 @@ 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"),
DISCORD_CHAT_CHANNEL_ID(String.class, "discord.chat_channel_id"),

View File

@ -4,12 +4,15 @@ import me.totalfreedom.totalfreedommod.admin.Admin;
import me.totalfreedom.totalfreedommod.admin.AdminList;
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.FUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Arrays;
import java.util.Collection;
public class Module_list extends HTTPDModule
@ -26,6 +29,29 @@ public class Module_list extends HTTPDModule
if (params.get("json") != null && params.get("json").equals("true"))
{
final JSONObject responseObject = new JSONObject();
if (!(plugin.permissionHandler instanceof DefaultPermissionHandler))
{
IPermissionHandler handler = plugin.permissionHandler;
final JSONObject players = new JSONObject();
Arrays.stream(handler.getGroups()).forEach(s ->
{
JSONArray array = new JSONArray();
Bukkit.getOnlinePlayers().stream().filter(player -> !plugin.al.isVanished(player.getName())).filter(player -> handler.getPrimaryGroup(player).equalsIgnoreCase(s)).forEach(player ->
{
array.put(player.getName());
});
players.put(s.toLowerCase(), array);
});
responseObject.put("players", players);
responseObject.put("online", Bukkit.getOnlinePlayers().stream().filter(player -> !plugin.al.isVanished(player.getName())).count());
responseObject.put("max", Bukkit.getMaxPlayers());
final NanoHTTPD.Response response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_JSON, responseObject.toString(4));
response.addHeader("Access-Control-Allow-Origin", "*");
return response;
}
final JSONArray owners = new JSONArray();
final JSONArray executives = new JSONArray();
@ -44,27 +70,27 @@ public class Module_list extends HTTPDModule
if (plugin.pl.getData(player).isMasterBuilder())
{
masterbuilders.add(player.getName());
masterbuilders.put(player.getName());
}
if (FUtil.DEVELOPER_NAMES.contains(player.getName()))
{
developers.add(player.getName());
developers.put(player.getName());
}
if (ConfigEntry.SERVER_EXECUTIVES.getList().contains(player.getName()) && !FUtil.DEVELOPERS.contains(player.getName()))
{
executives.add(player.getName());
executives.put(player.getName());
}
if (ConfigEntry.SERVER_OWNERS.getList().contains(player.getName()))
{
owners.add(player.getName());
owners.put(player.getName());
}
if (!plugin.al.isAdmin(player) && hasSpecialTitle(player))
{
operators.add(player.getName());
operators.put(player.getName());
}
if (hasSpecialTitle(player) && plugin.al.isAdmin(player) && !plugin.al.isVanished(player.getName()))
@ -74,12 +100,12 @@ public class Module_list extends HTTPDModule
{
case ADMIN:
{
admins.add(player.getName());
admins.put(player.getName());
break;
}
case SENIOR_ADMIN:
{
senioradmins.add(player.getName());
senioradmins.put(player.getName());
break;
}
default:
@ -102,11 +128,11 @@ public class Module_list extends HTTPDModule
responseObject.put("online", FUtil.getFakePlayerCount());
responseObject.put("max", server.getMaxPlayers());
final NanoHTTPD.Response response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_JSON, responseObject.toString());
final NanoHTTPD.Response response = new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, NanoHTTPD.MIME_JSON, responseObject.toString(4));
response.addHeader("Access-Control-Allow-Origin", "*");
return response;
}
else
} else
{
final StringBuilder body = new StringBuilder();

View File

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

View File

@ -17,4 +17,9 @@ public interface IPermissionHandler
boolean inGroup(@NotNull OfflinePlayer player, @Nullable String groupName);
boolean inGroup(@NotNull Player player, @Nullable String groupName);
String getPrimaryGroup(@NotNull Player player);
String[] getGroups();
}

View File

@ -5,6 +5,7 @@ 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.Group;
import nl.chimpgamer.networkmanager.api.models.permissions.PermissionPlayer;
import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer;
@ -92,4 +93,22 @@ public class NMPermissionHandler implements IPermissionHandler
{
return this.inGroup((OfflinePlayer) player, groupName);
}
@Override
public String[] getGroups()
{
return this.plugin.getPermissionManager().getGroups().values().stream().map(Group::getName).toArray(String[]::new);
}
@Override
public String getPrimaryGroup(@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 this.plugin.getPermissionManager().getPermissionPlayer(player.getUniqueId()).getPrimaryGroup().getName();
}
}

View File

@ -96,6 +96,23 @@ public class VaultPermissionHandler implements IPermissionHandler
return this.permissions.playerInGroup(player, groupName);
}
@Override
public String[] getGroups()
{
if (this.permissions == null)
{
FLog.debug("Can't use Vault permissions system for group listing, there is no plugin using Vault permissions.");
return new String[0];
}
return this.permissions.getGroups();
}
@Override
public String getPrimaryGroup(@NotNull Player player)
{
return this.permissions.getPrimaryGroup(player);
}
public Permission getPermissions()
{
return permissions;

View File

@ -59,6 +59,7 @@ permissions:
groups:
admin: "Administrator"
senior_admin: "Senior-Administrator"
default: "Guest"
# Discord
discord: