This commit is contained in:
Telesphoreo 2022-05-30 20:55:28 -05:00
parent 8bef589b7a
commit 867e3ff79b

View file

@ -1,6 +1,6 @@
package dev.plex.request.impl; package dev.plex.request.impl;
import com.google.common.collect.Lists; import com.google.gson.Gson;
import com.google.gson.GsonBuilder; import com.google.gson.GsonBuilder;
import dev.plex.HTTPDModule; import dev.plex.HTTPDModule;
import dev.plex.Plex; import dev.plex.Plex;
@ -13,16 +13,38 @@ import dev.plex.request.MappingHeaders;
import dev.plex.util.PlexLog; import dev.plex.util.PlexLog;
import dev.plex.util.adapter.ZonedDateTimeSerializer; import dev.plex.util.adapter.ZonedDateTimeSerializer;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import java.time.ZonedDateTime;
import java.util.stream.Collectors;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import java.time.ZonedDateTime;
import java.util.List;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
public class AdminsEndpoint extends AbstractServlet 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<PlexPlayer> getAuthenticatedResponse(List<PlexPlayer> admins)
{
return admins
.stream().peek(plexPlayer ->
{
plexPlayer.setPunishments(null);
plexPlayer.setNotes(null);
plexPlayer.setPermissions(null);
plexPlayer.setCommandSpy(false);
plexPlayer.setVanished(false);
}).toList();
}
private List<PlexPlayer> getUnauthenticatedResponse(List<PlexPlayer> admins)
{
return getAuthenticatedResponse(admins).stream().peek(plexPlayer ->
plexPlayer.setIps(null)).toList();
}
@GetMapping(endpoint = "/api/admins/") @GetMapping(endpoint = "/api/admins/")
@MappingHeaders(headers = "content-type;application/json") @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."); return adminsHTML("An IP address could not be detected. Please ensure you are connecting using IPv4.");
} }
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress); final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
final List<PlexPlayer> admins = Plex.get().getAdminList().getAllAdminPlayers();
if (player == null) if (player == null)
{ {
// This likely means they've never joined the server before. That's okay. We can just not return IPs. // 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")) if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
{ {
@ -45,7 +68,7 @@ public class AdminsEndpoint extends AbstractServlet
if (!player.getRankFromString().isAtLeast(Rank.ADMIN)) if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
{ {
// Don't return IPs either if the person is not an Admin or above. // 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")) 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 (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access"))
{ {
// If the person doesn't have permission, don't return IPs // 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) private String adminsHTML(String message)