diff --git a/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnector.cs b/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnector.cs index 98a9d4627d..5fa5e86e96 100644 --- a/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnector.cs +++ b/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnector.cs @@ -71,12 +71,13 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oresult)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") + string result = oresult.ToString(); + if (!result.Equals("true", StringComparison.OrdinalIgnoreCase)) { - reason = ret["REASON"].ToString(); + reason = result; return false; } @@ -112,13 +113,13 @@ namespace OpenSim.Groups if (ret == null) return null; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return null; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return null; - return GroupsDataUtils.GroupRecord((Dictionary)ret["RESULT"]); + return GroupsDataUtils.GroupRecord((Dictionary)oRESULT); } public List GetGroupMembers(string RequestingAgentID, UUID GroupID, string token) @@ -134,12 +135,13 @@ namespace OpenSim.Groups if (ret == null) return members; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return members; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return members; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + + foreach (object v in ((Dictionary)oRESULT).Values) { ExtendedGroupMembersData m = GroupsDataUtils.GroupMembersData((Dictionary)v); members.Add(m); @@ -161,12 +163,13 @@ namespace OpenSim.Groups if (ret == null) return roles; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return roles; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return roles; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + + foreach (object v in ((Dictionary)oRESULT).Values) { GroupRolesData m = GroupsDataUtils.GroupRolesData((Dictionary)v); roles.Add(m); @@ -188,13 +191,13 @@ namespace OpenSim.Groups if (ret == null) return rmembers; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return rmembers; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return rmembers; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { ExtendedGroupRoleMembersData m = GroupsDataUtils.GroupRoleMembersData((Dictionary)v); rmembers.Add(m); @@ -227,13 +230,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object value)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") - return false; - - return true; + return value.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); } public bool VerifyNotice(UUID noticeID, UUID groupID) @@ -246,13 +246,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object value)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") - return false; - - return true; + return value.ToString().Equals("true", StringComparison.CurrentCultureIgnoreCase); } // diff --git a/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnectorModule.cs b/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnectorModule.cs index 580221fa63..0a34bc6b6c 100644 --- a/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnectorModule.cs +++ b/OpenSim/Addons/Groups/Hypergrid/GroupsServiceHGConnectorModule.cs @@ -42,6 +42,7 @@ using OpenMetaverse; using Mono.Addins; using log4net; using Nini.Config; +using System.Runtime.InteropServices; namespace OpenSim.Groups { @@ -680,14 +681,11 @@ namespace OpenSim.Groups { lock (m_NetworkConnectors) { - if (m_NetworkConnectors.ContainsKey(url)) - return m_NetworkConnectors[url]; - - GroupsServiceHGConnector c = new GroupsServiceHGConnector(url); - m_NetworkConnectors[url] = c; + ref GroupsServiceHGConnector connector = ref CollectionsMarshal.GetValueRefOrAddDefault(m_NetworkConnectors,url, out bool exists); + if(!exists) + connector = new GroupsServiceHGConnector(url); + return connector; } - - return m_NetworkConnectors[url]; } #endregion } diff --git a/OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs b/OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs index 070424153b..73a18541fd 100644 --- a/OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs +++ b/OpenSim/Addons/Groups/Hypergrid/HGGroupsServiceRobustConnector.cs @@ -168,24 +168,24 @@ namespace OpenSim.Groups { Dictionary result = new Dictionary(); - if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") - || !request.ContainsKey("AgentID") - || !request.ContainsKey("AccessToken") || !request.ContainsKey("Location")) + if (!request.TryGetValue("RequestingAgentID", out object oRequestingAgentID) || + !request.TryGetValue("GroupID", out object oGroupID) || + !request.TryGetValue("AgentID", out object oAgentID) || + !request.TryGetValue("AccessToken", out object oAccessToken) || + !request.TryGetValue("Location", out object oLocation)) NullResult(result, "Bad network data"); - else { - string RequestingAgentID = request["RequestingAgentID"].ToString(); - string agentID = request["AgentID"].ToString(); - UUID groupID = new UUID(request["GroupID"].ToString()); - string accessToken = request["AccessToken"].ToString(); - string location = request["Location"].ToString(); + string RequestingAgentID = oRequestingAgentID.ToString(); + string agentID = oAgentID.ToString(); + UUID groupID = new UUID(oGroupID.ToString()); + string accessToken = oAccessToken.ToString(); + string location = oLocation.ToString(); string name = string.Empty; - if (request.ContainsKey("Name")) - name = request["Name"].ToString(); + if (request.TryGetValue("Name", out object oName)) + name = oName.ToString(); - string reason = string.Empty; - bool success = m_GroupsService.CreateGroupProxy(RequestingAgentID, agentID, accessToken, groupID, location, name, out reason); + bool success = m_GroupsService.CreateGroupProxy(RequestingAgentID, agentID, accessToken, groupID, location, name, out string reason); result["REASON"] = reason; result["RESULT"] = success.ToString(); } @@ -255,16 +255,18 @@ namespace OpenSim.Groups { Dictionary result = new Dictionary(); - if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken")) + if (!request.TryGetValue("RequestingAgentID", out object oRequestingAgentID) || + !request.TryGetValue("GroupID", out object oGroupID) || + !request.TryGetValue("AccessToken", out object oAccessToken)) NullResult(result, "Bad network data"); else { - UUID groupID = new UUID(request["GroupID"].ToString()); - string requestingAgentID = request["RequestingAgentID"].ToString(); - string token = request["AccessToken"].ToString(); + UUID groupID = new UUID(oGroupID.ToString()); + string requestingAgentID = oRequestingAgentID.ToString(); + string token = oAccessToken.ToString(); List members = m_GroupsService.GetGroupMembers(requestingAgentID, groupID, token); - if (members == null || (members != null && members.Count == 0)) + if (members == null || members.Count == 0) { NullResult(result, "No members"); } @@ -291,16 +293,18 @@ namespace OpenSim.Groups { Dictionary result = new Dictionary(); - if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken")) + if (!request.TryGetValue("RequestingAgentID", out object oRequestingAgentID) || + !request.TryGetValue("GroupID", out object oGroupID) || + !request.TryGetValue("AccessToken", out object oAccessToken)) NullResult(result, "Bad network data"); else { - UUID groupID = new UUID(request["GroupID"].ToString()); - string requestingAgentID = request["RequestingAgentID"].ToString(); - string token = request["AccessToken"].ToString(); + UUID groupID = new UUID(oGroupID.ToString()); + string requestingAgentID = oRequestingAgentID.ToString(); + string token = oAccessToken.ToString(); List roles = m_GroupsService.GetGroupRoles(requestingAgentID, groupID, token); - if (roles == null || (roles != null && roles.Count == 0)) + if (roles == null || roles.Count == 0) { NullResult(result, "No members"); } @@ -325,16 +329,18 @@ namespace OpenSim.Groups { Dictionary result = new Dictionary(); - if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("AccessToken")) + if (!request.TryGetValue("RequestingAgentID", out object oRequestingAgentID) || + !request.TryGetValue("GroupID", out object oGroupID) || + !request.TryGetValue("AccessToken", out object oAccessToken)) NullResult(result, "Bad network data"); else { - UUID groupID = new UUID(request["GroupID"].ToString()); - string requestingAgentID = request["RequestingAgentID"].ToString(); - string token = request["AccessToken"].ToString(); + UUID groupID = new UUID(oGroupID.ToString()); + string requestingAgentID = oRequestingAgentID.ToString(); + string token = oAccessToken.ToString(); List rmembers = m_GroupsService.GetGroupRoleMembers(requestingAgentID, groupID, token); - if (rmembers == null || (rmembers != null && rmembers.Count == 0)) + if (rmembers == null || rmembers.Count == 0) { NullResult(result, "No members"); } @@ -359,31 +365,35 @@ namespace OpenSim.Groups { Dictionary result = new Dictionary(); - if (!request.ContainsKey("RequestingAgentID") || !request.ContainsKey("GroupID") || !request.ContainsKey("NoticeID") || - !request.ContainsKey("FromName") || !request.ContainsKey("Subject") || !request.ContainsKey("Message") || - !request.ContainsKey("HasAttachment")) + if (!request.TryGetValue("RequestingAgentID", out object oRequestingAgentID) || + !request.TryGetValue("GroupID", out object oGroupID) || + !request.TryGetValue("NoticeID", out object oNoticeID) || + !request.TryGetValue("FromName", out object oFromName) || + !request.TryGetValue("Subject", out object oSubject) || + !request.TryGetValue("Message", out object oMessage) || + !request.TryGetValue("HasAttachment", out object oHasAttachment)) NullResult(result, "Bad network data"); - else { - bool hasAtt = bool.Parse(request["HasAttachment"].ToString()); + bool hasAtt = bool.Parse(oHasAttachment.ToString()); byte attType = 0; string attName = string.Empty; string attOwner = string.Empty; UUID attItem = UUID.Zero; - if (request.ContainsKey("AttachmentType")) - attType = byte.Parse(request["AttachmentType"].ToString()); - if (request.ContainsKey("AttachmentName")) - attName = request["AttachmentType"].ToString(); - if (request.ContainsKey("AttachmentItemID")) - attItem = new UUID(request["AttachmentItemID"].ToString()); - if (request.ContainsKey("AttachmentOwnerID")) - attOwner = request["AttachmentOwnerID"].ToString(); + if (request.TryGetValue("AttachmentType", out object oAttachmentType)) + attType = byte.Parse(oAttachmentType.ToString()); - bool success = m_GroupsService.AddNotice(request["RequestingAgentID"].ToString(), new UUID(request["GroupID"].ToString()), - new UUID(request["NoticeID"].ToString()), request["FromName"].ToString(), request["Subject"].ToString(), - request["Message"].ToString(), hasAtt, attType, attName, attItem, attOwner); + if (request.TryGetValue("AttachmentName", out object oAttachmentName)) + attName = oAttachmentName.ToString(); + if (request.TryGetValue("AttachmentItemID", out object oAttachmentItemID)) + attItem = new UUID(oAttachmentItemID.ToString()); + if (request.TryGetValue("AttachmentOwnerID", out object oAttachmentOwnerID)) + attOwner = oAttachmentOwnerID.ToString(); + + bool success = m_GroupsService.AddNotice(oRequestingAgentID.ToString(), new UUID(oGroupID.ToString()), + new UUID(oNoticeID.ToString()), oFromName.ToString(), oSubject.ToString(), +oMessage.ToString(), hasAtt, attType, attName, attItem, attOwner); result["RESULT"] = success.ToString(); } diff --git a/OpenSim/Addons/Groups/Remote/GroupsServiceRemoteConnector.cs b/OpenSim/Addons/Groups/Remote/GroupsServiceRemoteConnector.cs index bd1498643d..eaa17094ba 100644 --- a/OpenSim/Addons/Groups/Remote/GroupsServiceRemoteConnector.cs +++ b/OpenSim/Addons/Groups/Remote/GroupsServiceRemoteConnector.cs @@ -25,19 +25,19 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; - +using log4net; +using Nini.Config; +using OpenMetaverse; using OpenSim.Framework; using OpenSim.Framework.ServiceAuth; using OpenSim.Server.Base; - -using OpenMetaverse; -using log4net; -using Nini.Config; +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using System.Data; +using System.Linq; +using System.Reflection; +using System.Text; namespace OpenSim.Groups { @@ -99,13 +99,16 @@ namespace OpenSim.Groups if (ret == null) return null; - if (ret["RESULT"].ToString() == "NULL") + if (!ret.TryGetValue("RESULT", out object oRESULT)) + return null; + + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) { reason = ret["REASON"].ToString(); return null; } - return GroupsDataUtils.GroupRecord((Dictionary)ret["RESULT"]); + return GroupsDataUtils.GroupRecord((Dictionary)oRESULT); } @@ -126,10 +129,13 @@ namespace OpenSim.Groups sendData["OP"] = "UPDATE"; Dictionary ret = MakeRequest("PUTGROUP", sendData); - if (ret == null || (ret != null && (!ret.ContainsKey("RESULT") || ret["RESULT"].ToString() == "NULL"))) + if (ret == null || !ret.TryGetValue("RESULT", out object oRESULT)) return null; - return GroupsDataUtils.GroupRecord((Dictionary)ret["RESULT"]); + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) + return null; + + return GroupsDataUtils.GroupRecord((Dictionary)oRESULT); } public ExtendedGroupRecord GetGroupRecord(string RequestingAgentID, UUID GroupID, string GroupName) @@ -147,10 +153,13 @@ namespace OpenSim.Groups Dictionary ret = MakeRequest("GETGROUP", sendData); - if (ret == null || (ret != null && (!ret.ContainsKey("RESULT") || ret["RESULT"].ToString() == "NULL"))) + if (ret == null || !ret.TryGetValue("RESULT", out object oRESULT)) return null; - return GroupsDataUtils.GroupRecord((Dictionary)ret["RESULT"]); + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) + return null; + + return GroupsDataUtils.GroupRecord((Dictionary)oRESULT); } public List FindGroups(string RequestingAgentIDstr, string query) @@ -168,13 +177,10 @@ namespace OpenSim.Groups if (ret == null) return hits; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT) || oRESULT.ToString() == "NULL") return hits; - if (ret["RESULT"].ToString() == "NULL") - return hits; - - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { DirGroupsReplyData m = GroupsDataUtils.DirGroupsReplyData((Dictionary)v); hits.Add(m); @@ -198,16 +204,16 @@ namespace OpenSim.Groups if (ret == null) return null; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return null; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT.ToString() == "NULL") { reason = ret["REASON"].ToString(); return null; } - return GroupsDataUtils.GroupMembershipData((Dictionary)ret["RESULT"]); + return GroupsDataUtils.GroupMembershipData((Dictionary)oRESULT); } @@ -232,13 +238,13 @@ namespace OpenSim.Groups if (ret == null) return null; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return null; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT.ToString() == "NULL") return null; - return GroupsDataUtils.GroupMembershipData((Dictionary)ret["RESULT"]); + return GroupsDataUtils.GroupMembershipData((Dictionary)oRESULT); } public List GetMemberships(string RequestingAgentID, string AgentID) @@ -254,13 +260,13 @@ namespace OpenSim.Groups if (ret == null) return memberships; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return memberships; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT.ToString() == "NULL") return memberships; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { GroupMembershipData m = GroupsDataUtils.GroupMembershipData((Dictionary)v); memberships.Add(m); @@ -282,13 +288,13 @@ namespace OpenSim.Groups if (ret == null) return members; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return members; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT.ToString() == "NULL") return members; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { ExtendedGroupMembersData m = GroupsDataUtils.GroupMembersData((Dictionary)v); members.Add(m); @@ -315,10 +321,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") + if (!oRESULT.ToString().Equals("true", StringComparison.OrdinalIgnoreCase)) { reason = ret["REASON"].ToString(); return false; @@ -343,13 +349,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") - return false; - - return true; + return !oRESULT.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); } public void RemoveGroupRole(string RequestingAgentID, UUID groupID, UUID roleID) @@ -373,13 +376,13 @@ namespace OpenSim.Groups if (ret == null) return roles; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return roles; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return roles; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { GroupRolesData m = GroupsDataUtils.GroupRolesData((Dictionary)v); roles.Add(m); @@ -400,13 +403,13 @@ namespace OpenSim.Groups if (ret == null) return rmembers; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return rmembers; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return rmembers; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { ExtendedGroupRoleMembersData m = GroupsDataUtils.GroupRoleMembersData((Dictionary)v); rmembers.Add(m); @@ -429,13 +432,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") - return false; - - return true; + return oRESULT.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); } public bool RemoveAgentFromGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID) @@ -452,13 +452,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") - return false; - - return true; + return oRESULT.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); } public List GetAgentGroupRoles(string RequestingAgentID, string AgentID, UUID GroupID) @@ -474,13 +471,13 @@ namespace OpenSim.Groups if (ret == null) return roles; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return roles; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return roles; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { GroupRolesData m = GroupsDataUtils.GroupRolesData((Dictionary)v); roles.Add(m); @@ -502,13 +499,13 @@ namespace OpenSim.Groups if (ret == null) return null; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return null; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return null; - return GroupsDataUtils.GroupMembershipData((Dictionary)ret["RESULT"]); + return GroupsDataUtils.GroupMembershipData((Dictionary)oRESULT); } public void SetAgentActiveGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID) @@ -549,13 +546,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") // it may return "NULL" - return false; - - return true; + return oRESULT.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); } public GroupInviteInfo GetAgentToGroupInvite(string RequestingAgentID, UUID inviteID) @@ -570,13 +564,13 @@ namespace OpenSim.Groups if (ret == null) return null; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return null; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return null; - return GroupsDataUtils.GroupInviteInfo((Dictionary)ret["RESULT"]); + return GroupsDataUtils.GroupInviteInfo((Dictionary)oRESULT); } public void RemoveAgentToGroupInvite(string RequestingAgentID, UUID inviteID) @@ -613,13 +607,10 @@ namespace OpenSim.Groups if (ret == null) return false; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return false; - if (ret["RESULT"].ToString().ToLower() != "true") - return false; - - return true; + return oRESULT.ToString().Equals("true", StringComparison.OrdinalIgnoreCase); } public GroupNoticeInfo GetGroupNotice(string RequestingAgentID, UUID noticeID) @@ -633,13 +624,14 @@ namespace OpenSim.Groups if (ret == null) return null; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return null; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return null; - return GroupsDataUtils.GroupNoticeInfo((Dictionary)ret["RESULT"]); + + return GroupsDataUtils.GroupNoticeInfo((Dictionary)oRESULT); } public List GetGroupNotices(string RequestingAgentID, UUID GroupID) @@ -654,13 +646,13 @@ namespace OpenSim.Groups if (ret == null) return notices; - if (!ret.ContainsKey("RESULT")) + if (!ret.TryGetValue("RESULT", out object oRESULT)) return notices; - if (ret["RESULT"].ToString() == "NULL") + if (oRESULT is string sRESULT && sRESULT.Equals("NULL", StringComparison.OrdinalIgnoreCase)) return notices; - foreach (object v in ((Dictionary)ret["RESULT"]).Values) + foreach (object v in ((Dictionary)oRESULT).Values) { ExtendedGroupNoticeData m = GroupsDataUtils.GroupNoticeData((Dictionary)v); notices.Add(m); diff --git a/OpenSim/Addons/Groups/Service/GroupsService.cs b/OpenSim/Addons/Groups/Service/GroupsService.cs index 3a36506c0f..959c9e1f66 100644 --- a/OpenSim/Addons/Groups/Service/GroupsService.cs +++ b/OpenSim/Addons/Groups/Service/GroupsService.cs @@ -274,33 +274,31 @@ namespace OpenSim.Groups UUID ownerRoleID = new UUID(group.Data["OwnerRoleID"]); RoleData[] roles = m_Database.RetrieveRoles(GroupID); - if (roles == null) - // something wrong with this group + if (roles == null || roles.Length == 0) return members; - List rolesList = new List(roles); + List rolesList = new List(roles); // Let's find the "real" ownerRoleID - RoleData ownerRole = rolesList.Find(r => r.Data["Powers"] == ((long)OwnerPowers).ToString()); + string OwnerPowersstr = ((long)OwnerPowers).ToString(); + RoleData ownerRole = rolesList.Find(r => OwnerPowersstr.Equals(r.Data["Powers"])); if (ownerRole != null) ownerRoleID = ownerRole.RoleID; // Check visibility? // When we don't want to check visibility, we pass it "all" as the requestingAgentID - bool checkVisibility = !RequestingAgentID.Equals(UUID.Zero.ToString()); + bool checkVisibility = !RequestingAgentID.Equals(UUID.ZeroString); if (checkVisibility) { // Is the requester a member of the group? - bool isInGroup = false; - if (m_Database.RetrieveMember(GroupID, RequestingAgentID) != null) - isInGroup = true; + bool isInGroup = m_Database.RetrieveMember(GroupID, RequestingAgentID) != null; if (!isInGroup) // reduce the roles to the visible ones rolesList = rolesList.FindAll(r => (UInt64.Parse(r.Data["Powers"]) & (ulong)GroupPowers.MemberVisible) != 0); } MembershipData[] datas = m_Database.RetrieveMembers(GroupID); - if (datas == null || (datas != null && datas.Length == 0)) + if (datas == null || datas.Length == 0) return members; // OK, we have everything we need @@ -323,27 +321,27 @@ namespace OpenSim.Groups } m.AgentID = d.PrincipalID; - m.AcceptNotices = d.Data["AcceptNotices"] == "1" ? true : false; + m.AcceptNotices = d.Data["AcceptNotices"] == "1"; m.Contribution = Int32.Parse(d.Data["Contribution"]); - m.ListInProfile = d.Data["ListInProfile"] == "1" ? true : false; + m.ListInProfile = d.Data["ListInProfile"] == "1"; GridUserData gud = m_GridUserService.Get(d.PrincipalID); if (gud != null) { if (bool.Parse(gud.Data["Online"])) { - m.OnlineStatus = @"Online"; + m.OnlineStatus = "Online"; } else { int unixtime = int.Parse(gud.Data["Login"]); // The viewer is very picky about how these strings are formed. Eg. it will crash on malformed dates! - m.OnlineStatus = (unixtime == 0) ? @"unknown" : Util.ToDateTime(unixtime).ToString("MM/dd/yyyy"); + m.OnlineStatus = (unixtime == 0) ? "unknown" : Util.ToDateTime(unixtime).ToString("MM/dd/yyyy"); } } // Is this person an owner of the group? - m.IsOwner = (rolemembershipsList.Find(r => r.RoleID == ownerRoleID) != null) ? true : false; + m.IsOwner = rolemembershipsList.Find(r => r.RoleID.Equals(ownerRoleID)) != null; members.Add(m); } @@ -353,17 +351,16 @@ namespace OpenSim.Groups public bool AddGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers, out string reason) { - reason = string.Empty; // check that the requesting agent has permissions to add role if (!HasPower(RequestingAgentID, groupID, GroupPowers.CreateRole)) { - m_log.DebugFormat("[Groups]: ({0}) Attempt at creating role in group {1} denied because of lack of permission", RequestingAgentID, groupID); + m_log.Debug($"[Groups]: ({RequestingAgentID}) Attempt at creating role in group {groupID} denied because of lack of permission"); reason = "Insufficient permission to create role"; return false; } + reason = string.Empty; return _AddOrUpdateGroupRole(RequestingAgentID, groupID, roleID, name, description, title, powers, true); - } public bool UpdateGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers) @@ -371,7 +368,7 @@ namespace OpenSim.Groups // check perms if (!HasPower(RequestingAgentID, groupID, GroupPowers.ChangeActions)) { - m_log.DebugFormat("[Groups]: ({0}) Attempt at changing role in group {1} denied because of lack of permission", RequestingAgentID, groupID); + m_log.Debug($"[Groups]: ({RequestingAgentID}) Attempt at changing role in group {groupID} denied because of lack of permission"); return false; } @@ -380,30 +377,30 @@ namespace OpenSim.Groups public void RemoveGroupRole(string RequestingAgentID, UUID groupID, UUID roleID) { - // check perms - if (!HasPower(RequestingAgentID, groupID, GroupPowers.DeleteRole)) - { - m_log.DebugFormat("[Groups]: ({0}) Attempt at deleting role from group {1} denied because of lack of permission", RequestingAgentID, groupID); - return; - } - // Can't delete Everyone and Owners roles if (roleID.IsZero()) { - m_log.DebugFormat("[Groups]: Attempt at deleting Everyone role from group {0} denied", groupID); + m_log.Debug($"[Groups]: Attempt at deleting Everyone role from group {groupID} denied"); + return; + } + + // check perms + if (!HasPower(RequestingAgentID, groupID, GroupPowers.DeleteRole)) + { + m_log.Debug($"[Groups]: ({RequestingAgentID}) Attempt at deleting role from group {groupID} denied because of lack of permission"); return; } GroupData group = m_Database.RetrieveGroup(groupID); if (group == null) { - m_log.DebugFormat("[Groups]: Attempt at deleting role from non-existing group {0}", groupID); + m_log.Debug($"[Groups]: Attempt at deleting role from non-existing group {groupID}"); return; } - if (roleID == new UUID(group.Data["OwnerRoleID"])) + if (roleID.ToString().Equals(group.Data["OwnerRoleID"])) { - m_log.DebugFormat("[Groups]: Attempt at deleting Owners role from group {0} denied", groupID); + m_log.Debug($"[Groups]: Attempt at deleting Owners role from group {groupID} denied"); return; } @@ -421,9 +418,7 @@ namespace OpenSim.Groups // TODO: check perms // Is the requester a member of the group? - bool isInGroup = false; - if (m_Database.RetrieveMember(GroupID, RequestingAgentID) != null) - isInGroup = true; + bool isInGroup = m_Database.RetrieveMember(GroupID, RequestingAgentID) != null; return _GetGroupRoleMembers(GroupID, isInGroup); } @@ -500,15 +495,19 @@ namespace OpenSim.Groups public bool AddAgentToGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID) { - //if (!m_Database.CheckOwnerRole(RequestingAgentID, GroupID, RoleID)) - // return; - // check permissions - bool limited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMemberLimited); - bool unlimited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMember) || IsOwner(RequestingAgentID, GroupID); + bool limited = false; + bool unlimited = IsOwner(RequestingAgentID, GroupID); + if(!unlimited) + { + ulong upowers =(ulong)GetAgentGroupPowers(RequestingAgentID, GroupID); + limited = (upowers & (ulong)GroupPowers.AssignMemberLimited) != 0; + unlimited = (upowers & (ulong)GroupPowers.AssignMember) != 0; + } + if (!limited && !unlimited) { - m_log.DebugFormat("[Groups]: ({0}) Attempt at assigning {1} to role {2} denied because of lack of permission", RequestingAgentID, AgentID, RoleID); + m_log.Debug($"[Groups]: ({RequestingAgentID}) Attempt at assigning {AgentID} to role {RoleID} denied because of lack of permission"); return false; } @@ -519,7 +518,7 @@ namespace OpenSim.Groups RoleMembershipData rolemembership = m_Database.RetrieveRoleMember(GroupID, RoleID, RequestingAgentID); if (rolemembership == null) { - m_log.DebugFormat("[Groups]: ({0}) Attempt at assigning {1} to role {2} denied because of limited permission", RequestingAgentID, AgentID, RoleID); + m_log.Debug($"[Groups]: ({RequestingAgentID}) Attempt at assigning {AgentID} to role {RoleID} denied because of limited permission"); return false; } } @@ -588,11 +587,11 @@ namespace OpenSim.Groups public List GetAgentGroupRoles(string RequestingAgentID, string AgentID, UUID GroupID) { - List roles = new List(); + List roles = []; // TODO: check permissions RoleMembershipData[] data = m_Database.RetrieveMemberRoles(GroupID, AgentID); - if (data == null || (data != null && data.Length ==0)) + if (data == null || data.Length ==0) return roles; foreach (RoleMembershipData d in data) @@ -601,11 +600,13 @@ namespace OpenSim.Groups if (rdata == null) // hippos continue; - GroupRolesData r = new GroupRolesData(); - r.Name = rdata.Data["Name"]; - r.Powers = UInt64.Parse(rdata.Data["Powers"]); - r.RoleID = rdata.RoleID; - r.Title = rdata.Data["Title"]; + GroupRolesData r = new GroupRolesData + { + Name = rdata.Data["Name"], + Powers = UInt64.Parse(rdata.Data["Powers"]), + RoleID = rdata.RoleID, + Title = rdata.Data["Title"] + }; roles.Add(r); } @@ -1053,19 +1054,20 @@ namespace OpenSim.Groups notice.AttachmentOwnerID = data.Data["AttachmentOwnerID"].ToString(); } - return notice; } #endregion #region permissions - private bool HasPower(string agentID, UUID groupID, GroupPowers power) + + public GroupPowers GetAgentGroupPowers(string agentID, UUID groupID) { RoleMembershipData[] rmembership = m_Database.RetrieveMemberRoles(groupID, agentID); if (rmembership is null || rmembership.Length == 0) - return false; + return 0; + ulong powers = 0; foreach (RoleMembershipData rdata in rmembership) { if(rdata is null) @@ -1080,7 +1082,36 @@ namespace OpenSim.Groups continue; } - if ((UInt64.Parse(role.Data["Powers"]) & (ulong)power) != 0) + if (role.Data.TryGetValue("Powers", out string psrt) && + ulong.TryParse(psrt, out ulong ut)) + powers |= ut; + } + return (GroupPowers)powers; + } + + private bool HasPower(string agentID, UUID groupID, GroupPowers power) + { + RoleMembershipData[] rmembership = m_Database.RetrieveMemberRoles(groupID, agentID); + if (rmembership is null || rmembership.Length == 0) + return false; + + ulong upower = (ulong)power; + foreach (RoleMembershipData rdata in rmembership) + { + if(rdata is null) + { + m_log.Warn($"[GROUPSERVICE] null membership data entry in group {groupID} for agent {agentID}"); + continue; + } + RoleData role = m_Database.RetrieveRole(groupID, rdata.RoleID); + if (role is null) + { + m_log.Warn($"[GROUPSERVICE] role with id {rdata.RoleID} is null"); + continue; + } + + if (role.Data.TryGetValue("Powers", out string psrt) && + ulong.TryParse(psrt, out ulong ut) && (ut & upower) != 0) return true; } return false;