Module-HTTPD/src/main/java/dev/plex/request/impl/AdminsEndpoint.java

57 lines
2.7 KiB
Java
Raw Normal View History

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;
import jakarta.servlet.http.HttpServletRequest;
2022-04-02 20:14:10 +00:00
import java.util.UUID;
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-02 03:59:47 +00:00
@GetMapping(endpoint = "/api/admins/")
2022-04-02 04:14:12 +00:00
public String getAdmins(HttpServletRequest request)
{
String ipAddress = request.getRemoteAddr();
2022-04-02 04:14:12 +00:00
if (ipAddress == null)
{
return "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.
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(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-02 03:59:47 +00:00
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList()));
}
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");
final OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(UUID.fromString(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-02 03:59:47 +00:00
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers().stream().peek(plexPlayer -> plexPlayer.setIps(Lists.newArrayList())).collect(Collectors.toList()));
}
}
2022-04-02 20:14:10 +00:00
return new GsonBuilder().setPrettyPrinting().create().toJson(Plex.get().getAdminList().getAllAdminPlayers());
2022-04-02 03:59:47 +00:00
}
}