2022-04-02 03:59:47 +00:00
|
|
|
package dev.plex.request.impl;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.gson.GsonBuilder;
|
|
|
|
import dev.plex.HTTPDModule;
|
|
|
|
import dev.plex.Plex;
|
|
|
|
import dev.plex.cache.DataUtils;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
|
|
|
import dev.plex.rank.enums.Rank;
|
|
|
|
import dev.plex.request.AbstractServlet;
|
|
|
|
import dev.plex.request.GetMapping;
|
|
|
|
import dev.plex.util.PlexLog;
|
2022-04-04 08:47:51 +00:00
|
|
|
import dev.plex.util.adapter.LocalDateTimeSerializer;
|
2022-04-02 03:59:47 +00:00
|
|
|
import jakarta.servlet.http.HttpServletRequest;
|
2022-04-04 08:47:51 +00:00
|
|
|
import java.time.LocalDateTime;
|
2022-04-02 20:14:10 +00:00
|
|
|
import java.util.stream.Collectors;
|
2022-04-02 20:51:14 +00:00
|
|
|
import org.bukkit.Bukkit;
|
|
|
|
import org.bukkit.OfflinePlayer;
|
2022-04-02 20:14:10 +00:00
|
|
|
|
2022-04-02 20:51:14 +00:00
|
|
|
public class AdminsEndpoint extends AbstractServlet
|
2022-04-02 04:14:12 +00:00
|
|
|
{
|
2022-04-15 18:55:40 +00:00
|
|
|
private static final String TITLE = "Admins - Plex HTTPD";
|
|
|
|
|
2022-04-02 03:59:47 +00:00
|
|
|
@GetMapping(endpoint = "/api/admins/")
|
2022-04-02 04:14:12 +00:00
|
|
|
public String getAdmins(HttpServletRequest request)
|
|
|
|
{
|
2022-04-02 21:50:15 +00:00
|
|
|
String ipAddress = request.getRemoteAddr();
|
2022-04-02 04:14:12 +00:00
|
|
|
if (ipAddress == null)
|
|
|
|
{
|
2022-04-15 18:55:40 +00:00
|
|
|
return createBasicHTML(TITLE, "An IP address could not be detected. Please ensure you are connecting using IPv4.");
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|
|
|
|
final PlexPlayer player = DataUtils.getPlayerByIP(ipAddress);
|
2022-04-02 04:14:12 +00:00
|
|
|
if (player == null)
|
|
|
|
{
|
2022-04-02 23:34:19 +00:00
|
|
|
// This likely means they've never joined the server before. That's okay. We can just not return IPs.
|
2022-04-15 18:55:40 +00:00
|
|
|
return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())));
|
2022-04-02 04:14:12 +00:00
|
|
|
}
|
|
|
|
if (Plex.get().getSystem().equalsIgnoreCase("ranks"))
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
PlexLog.debug("Plex-HTTPD using ranks check");
|
2022-04-02 04:14:12 +00:00
|
|
|
if (!player.getRankFromString().isAtLeast(Rank.ADMIN))
|
|
|
|
{
|
2022-04-02 23:34:19 +00:00
|
|
|
// Don't return IPs either if the person is not an Admin or above.
|
2022-04-15 18:55:40 +00:00
|
|
|
return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())));
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|
2022-04-02 04:14:12 +00:00
|
|
|
}
|
|
|
|
else if (Plex.get().getSystem().equalsIgnoreCase("permissions"))
|
|
|
|
{
|
2022-04-02 03:59:47 +00:00
|
|
|
PlexLog.debug("Plex-HTTPD using permissions check");
|
2022-04-09 03:20:14 +00:00
|
|
|
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(player.getUuid());
|
2022-04-02 20:14:10 +00:00
|
|
|
if (!HTTPDModule.getPermissions().playerHas(null, offlinePlayer, "plex.httpd.admins.access"))
|
2022-04-02 04:14:12 +00:00
|
|
|
{
|
2022-04-02 23:34:19 +00:00
|
|
|
// If the person doesn't have permission, don't return IPs
|
2022-04-15 18:55:40 +00:00
|
|
|
return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).peek(plexPlayer -> plexPlayer.setPunishments(Lists.newArrayList())).collect(Collectors.toList())));
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-15 18:55:40 +00:00
|
|
|
return createJSONHTML(TITLE, new GsonBuilder().registerTypeAdapter(LocalDateTime.class, new LocalDateTimeSerializer()).setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers()));
|
2022-04-02 03:59:47 +00:00
|
|
|
}
|
|
|
|
}
|