From ec9cb09cd8e27a4e1c131f3dd9a8881aebad5dbe Mon Sep 17 00:00:00 2001 From: Zack Piispanen Date: Sat, 19 Oct 2013 22:13:09 -0400 Subject: [PATCH] REST status or v2/server/status now checks for a permission to display ips to the caller. The only way to get this enabled is to use securetokenendpoint config option. Refactored a tsplayer method. --- TShockAPI/Rest/RestManager.cs | 12 +++-- TShockAPI/Rest/RestPermissions.cs | 3 ++ TShockAPI/TSPlayer.cs | 75 +++++++++++++++++-------------- 3 files changed, 52 insertions(+), 38 deletions(-) diff --git a/TShockAPI/Rest/RestManager.cs b/TShockAPI/Rest/RestManager.cs index eeea7150..61c53c79 100644 --- a/TShockAPI/Rest/RestManager.cs +++ b/TShockAPI/Rest/RestManager.cs @@ -236,7 +236,7 @@ namespace TShockAPI var players = new ArrayList(); foreach (TSPlayer tsPlayer in TShock.Players.Where(p => null != p)) { - var p = PlayerFilter(tsPlayer, parameters); + var p = PlayerFilter(tsPlayer, parameters, ((tokenData.UserGroupName) != "" && TShock.Utils.GetGroup(tokenData.UserGroupName).HasPermission(RestPermissions.viewips))); if (null != p) players.Add(p); } @@ -859,18 +859,22 @@ namespace TShockAPI return group; } - private Dictionary PlayerFilter(TSPlayer tsPlayer, IParameterCollection parameters) + private Dictionary PlayerFilter(TSPlayer tsPlayer, IParameterCollection parameters, bool viewips = false) { var player = new Dictionary { {"nickname", tsPlayer.Name}, - {"username", null == tsPlayer.UserAccountName ? "" : tsPlayer.UserAccountName}, - {"ip", tsPlayer.IP}, + {"username", tsPlayer.UserAccountName ?? ""}, {"group", tsPlayer.Group.Name}, {"active", tsPlayer.Active}, {"state", tsPlayer.State}, {"team", tsPlayer.Team}, }; + + if (viewips) + { + player.Add("ip", tsPlayer.IP); + } foreach (IParameter filter in parameters) { if (player.ContainsKey(filter.Name) && !player[filter.Name].Equals(filter.Value)) diff --git a/TShockAPI/Rest/RestPermissions.cs b/TShockAPI/Rest/RestPermissions.cs index 81664e7e..396a2947 100644 --- a/TShockAPI/Rest/RestPermissions.cs +++ b/TShockAPI/Rest/RestPermissions.cs @@ -83,5 +83,8 @@ namespace Rests [Description("REST user can run raw TShock commands (the raw command permissions are also checked though).")] public static readonly string restrawcommand = "tshock.rest.command"; + + [Description("REST user can view the ips of players.")] + public static readonly string viewips = "tshock.rest.viewips"; } } diff --git a/TShockAPI/TSPlayer.cs b/TShockAPI/TSPlayer.cs index b0167100..b393a433 100755 --- a/TShockAPI/TSPlayer.cs +++ b/TShockAPI/TSPlayer.cs @@ -647,45 +647,52 @@ namespace TShockAPI public virtual bool SendTileSquare(int x, int y, int size = 10) { - try - { - int num = (size - 1)/2; - int m_x=0; - int m_y=0; + try + { + int num = (size - 1)/2; + int m_x = 0; + int m_y = 0; - if (x - num <0){ - m_x=0; - }else{ - m_x = x - num; - } + if (x - num < 0) + { + m_x = 0; + } + else + { + m_x = x - num; + } - if (y - num <0){ - m_y=0; - }else{ - m_y = y - num; - } + if (y - num < 0) + { + m_y = 0; + } + else + { + m_y = y - num; + } - if (m_x + size > Main.maxTilesX){ - m_x=Main.maxTilesX - size; - } + if (m_x + size > Main.maxTilesX) + { + m_x = Main.maxTilesX - size; + } - if (m_y + size > Main.maxTilesY){ - m_y=Main.maxTilesY - size; - } + if (m_y + size > Main.maxTilesY) + { + m_y = Main.maxTilesY - size; + } - SendData(PacketTypes.TileSendSquare, "", size, m_x, m_y); - return true; - } - catch (IndexOutOfRangeException) - { - - // This is expected if square exceeds array. - } - catch (Exception ex) - { - Log.Error(ex.ToString()); - } - return false; + SendData(PacketTypes.TileSendSquare, "", size, m_x, m_y); + return true; + } + catch (IndexOutOfRangeException) + { + // This is expected if square exceeds array. + } + catch (Exception ex) + { + Log.Error(ex.ToString()); + } + return false; } public bool GiveItemCheck(int type, string name, int width, int height, int stack, int prefix = 0)