From 867e3ff79bb080c975d4919482992c1bfd28055d Mon Sep 17 00:00:00 2001 From: Telesphoreo Date: Mon, 30 May 2022 20:55:28 -0500 Subject: [PATCH] Closes #8 --- .../dev/plex/request/impl/AdminsEndpoint.java | 41 +++++++++++++++---- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java index 37c4254..9912de6 100644 --- a/src/main/java/dev/plex/request/impl/AdminsEndpoint.java +++ b/src/main/java/dev/plex/request/impl/AdminsEndpoint.java @@ -1,6 +1,6 @@ package dev.plex.request.impl; -import com.google.common.collect.Lists; +import com.google.gson.Gson; import com.google.gson.GsonBuilder; import dev.plex.HTTPDModule; import dev.plex.Plex; @@ -13,16 +13,38 @@ import dev.plex.request.MappingHeaders; import dev.plex.util.PlexLog; import dev.plex.util.adapter.ZonedDateTimeSerializer; import jakarta.servlet.http.HttpServletRequest; -import java.time.ZonedDateTime; -import java.util.stream.Collectors; - import jakarta.servlet.http.HttpServletResponse; +import java.time.ZonedDateTime; +import java.util.List; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; public class AdminsEndpoint extends AbstractServlet { - private static final String TITLE = "Admins - Plex HTTPD"; + private static final Gson GSON = + new GsonBuilder() + .registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()) + .setPrettyPrinting() + .create(); + + private List getAuthenticatedResponse(List admins) + { + return admins + .stream().peek(plexPlayer -> + { + plexPlayer.setPunishments(null); + plexPlayer.setNotes(null); + plexPlayer.setPermissions(null); + plexPlayer.setCommandSpy(false); + plexPlayer.setVanished(false); + }).toList(); + } + + private List getUnauthenticatedResponse(List admins) + { + return getAuthenticatedResponse(admins).stream().peek(plexPlayer -> + plexPlayer.setIps(null)).toList(); + } @GetMapping(endpoint = "/api/admins/") @MappingHeaders(headers = "content-type;application/json") @@ -34,10 +56,11 @@ public class AdminsEndpoint extends AbstractServlet return adminsHTML("An IP address could not be detected. Please ensure you are connecting using IPv4."); } final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); + final List admins = Plex.get().getAdminList().getAllAdminPlayers(); if (player == null) { // This likely means they've never joined the server before. That's okay. We can just not return IPs. - return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); + return GSON.toJson(getUnauthenticatedResponse(admins)); } if (Plex.get().getSystem().equalsIgnoreCase("ranks")) { @@ -45,7 +68,7 @@ public class AdminsEndpoint extends AbstractServlet if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) { // Don't return IPs either if the person is not an Admin or above. - return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); + return GSON.toJson(getUnauthenticatedResponse(admins)); } } else if (Plex.get().getSystem().equalsIgnoreCase("permissions")) @@ -55,10 +78,10 @@ public class AdminsEndpoint extends AbstractServlet if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access")) { // If the person doesn't have permission, don't return IPs - return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())); + return GSON.toJson(getUnauthenticatedResponse(admins)); } } - return new GsonBuilder().registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()); + return GSON.toJson(getAuthenticatedResponse(admins)); } private String adminsHTML(String message)