GroupsService.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using System.Timers;
  31. using log4net;
  32. using Nini.Config;
  33. using OpenMetaverse;
  34. using OpenSim.Data;
  35. using OpenSim.Framework;
  36. using OpenSim.Services.Interfaces;
  37. namespace OpenSim.Groups
  38. {
  39. public class GroupsService : GroupsServiceBase
  40. {
  41. private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  42. public const GroupPowers DefaultEveryonePowers =
  43. GroupPowers.AllowSetHome |
  44. GroupPowers.Accountable |
  45. GroupPowers.JoinChat |
  46. GroupPowers.AllowVoiceChat |
  47. GroupPowers.ReceiveNotices |
  48. GroupPowers.StartProposal |
  49. GroupPowers.VoteOnProposal;
  50. public const GroupPowers OfficersPowers = DefaultEveryonePowers |
  51. GroupPowers.AllowFly |
  52. GroupPowers.AllowLandmark |
  53. GroupPowers.AllowRez |
  54. GroupPowers.AssignMemberLimited |
  55. GroupPowers.ChangeIdentity |
  56. GroupPowers.ChangeMedia |
  57. GroupPowers.ChangeOptions |
  58. GroupPowers.DeedObject |
  59. GroupPowers.Eject |
  60. GroupPowers.FindPlaces |
  61. GroupPowers.Invite |
  62. GroupPowers.LandChangeIdentity |
  63. GroupPowers.LandDeed |
  64. GroupPowers.LandDivideJoin |
  65. GroupPowers.LandEdit |
  66. GroupPowers.AllowEnvironment |
  67. GroupPowers.LandEjectAndFreeze |
  68. GroupPowers.LandGardening |
  69. GroupPowers.LandManageAllowed |
  70. GroupPowers.LandManageBanned |
  71. GroupPowers.LandManagePasses |
  72. GroupPowers.LandOptions |
  73. GroupPowers.LandRelease |
  74. GroupPowers.LandSetSale |
  75. GroupPowers.MemberVisible |
  76. GroupPowers.ModerateChat |
  77. GroupPowers.ObjectManipulate |
  78. GroupPowers.ObjectSetForSale |
  79. GroupPowers.ReturnGroupOwned |
  80. GroupPowers.ReturnGroupSet |
  81. GroupPowers.ReturnNonGroup |
  82. GroupPowers.RoleProperties |
  83. GroupPowers.SendNotices |
  84. GroupPowers.SetLandingPoint;
  85. public const GroupPowers OwnerPowers = OfficersPowers |
  86. GroupPowers.Accountable |
  87. GroupPowers.AllowEditLand |
  88. GroupPowers.AssignMember |
  89. GroupPowers.ChangeActions |
  90. GroupPowers.CreateRole |
  91. GroupPowers.DeleteRole |
  92. GroupPowers.ExperienceAdmin |
  93. GroupPowers.ExperienceCreator |
  94. GroupPowers.GroupBanAccess |
  95. GroupPowers.HostEvent |
  96. GroupPowers.RemoveMember;
  97. #region Daily Cleanup
  98. private Timer m_CleanupTimer;
  99. public GroupsService(IConfigSource config, string configName)
  100. : base(config, configName)
  101. {
  102. }
  103. public GroupsService(IConfigSource config)
  104. : this(config, string.Empty)
  105. {
  106. // Once a day
  107. m_CleanupTimer = new Timer(24 * 60 * 60 * 1000);
  108. m_CleanupTimer.AutoReset = true;
  109. m_CleanupTimer.Elapsed += new ElapsedEventHandler(m_CleanupTimer_Elapsed);
  110. m_CleanupTimer.Enabled = true;
  111. m_CleanupTimer.Start();
  112. }
  113. private void m_CleanupTimer_Elapsed(object sender, ElapsedEventArgs e)
  114. {
  115. m_Database.DeleteOldNotices();
  116. m_Database.DeleteOldInvites();
  117. }
  118. #endregion
  119. public UUID CreateGroup(string RequestingAgentID, string name, string charter, bool showInList, UUID insigniaID, int membershipFee, bool openEnrollment,
  120. bool allowPublish, bool maturePublish, UUID founderID, out string reason)
  121. {
  122. reason = string.Empty;
  123. // Check if the group already exists
  124. if (m_Database.RetrieveGroup(name) != null)
  125. {
  126. reason = "A group with that name already exists";
  127. return UUID.Zero;
  128. }
  129. // Create the group
  130. GroupData data = new GroupData();
  131. data.GroupID = UUID.Random();
  132. data.Data = new Dictionary<string, string>();
  133. data.Data["Name"] = name;
  134. data.Data["Charter"] = charter;
  135. data.Data["InsigniaID"] = insigniaID.ToString();
  136. data.Data["FounderID"] = founderID.ToString();
  137. data.Data["MembershipFee"] = membershipFee.ToString();
  138. data.Data["OpenEnrollment"] = openEnrollment ? "1" : "0";
  139. data.Data["ShowInList"] = showInList ? "1" : "0";
  140. data.Data["AllowPublish"] = allowPublish ? "1" : "0";
  141. data.Data["MaturePublish"] = maturePublish ? "1" : "0";
  142. UUID ownerRoleID = UUID.Random();
  143. data.Data["OwnerRoleID"] = ownerRoleID.ToString();
  144. if (!m_Database.StoreGroup(data))
  145. return UUID.Zero;
  146. // Create Everyone role
  147. _AddOrUpdateGroupRole(RequestingAgentID, data.GroupID, UUID.Zero, "Everyone", "Everyone in the group is in the everyone role.", "Member of " + name, (ulong)DefaultEveryonePowers, true);
  148. // Create Officers role
  149. UUID officersRoleID = UUID.Random();
  150. _AddOrUpdateGroupRole(RequestingAgentID, data.GroupID, officersRoleID, "Officers", "The officers of the group, with more powers than regular members.", "Officer of " + name, (ulong)OfficersPowers, true);
  151. // Create Owner role
  152. _AddOrUpdateGroupRole(RequestingAgentID, data.GroupID, ownerRoleID, "Owners", "Owners of the group", "Owner of " + name, (ulong)OwnerPowers, true);
  153. // Add founder to group
  154. _AddAgentToGroup(RequestingAgentID, founderID.ToString(), data.GroupID, ownerRoleID);
  155. _AddAgentToGroup(RequestingAgentID, founderID.ToString(), data.GroupID, officersRoleID);
  156. return data.GroupID;
  157. }
  158. public void UpdateGroup(string RequestingAgentID, UUID groupID, string charter, bool showInList, UUID insigniaID, int membershipFee, bool openEnrollment, bool allowPublish, bool maturePublish)
  159. {
  160. GroupData data = m_Database.RetrieveGroup(groupID);
  161. if (data == null)
  162. return;
  163. // Check perms
  164. if (!HasPower(RequestingAgentID, groupID, GroupPowers.ChangeActions))
  165. {
  166. m_log.DebugFormat("[Groups]: ({0}) Attempt at updating group {1} denied because of lack of permission", RequestingAgentID, groupID);
  167. return;
  168. }
  169. data.GroupID = groupID;
  170. data.Data["Charter"] = charter;
  171. data.Data["ShowInList"] = showInList ? "1" : "0";
  172. data.Data["InsigniaID"] = insigniaID.ToString();
  173. data.Data["MembershipFee"] = membershipFee.ToString();
  174. data.Data["OpenEnrollment"] = openEnrollment ? "1" : "0";
  175. data.Data["AllowPublish"] = allowPublish ? "1" : "0";
  176. data.Data["MaturePublish"] = maturePublish ? "1" : "0";
  177. m_Database.StoreGroup(data);
  178. }
  179. public ExtendedGroupRecord GetGroupRecord(string RequestingAgentID, UUID GroupID)
  180. {
  181. GroupData data = m_Database.RetrieveGroup(GroupID);
  182. return _GroupDataToRecord(data);
  183. }
  184. public ExtendedGroupRecord GetGroupRecord(string RequestingAgentID, string GroupName)
  185. {
  186. GroupData data = m_Database.RetrieveGroup(GroupName);
  187. return _GroupDataToRecord(data);
  188. }
  189. public List<DirGroupsReplyData> FindGroups(string RequestingAgentID, string search)
  190. {
  191. List<DirGroupsReplyData> groups = new List<DirGroupsReplyData>();
  192. GroupData[] data = m_Database.RetrieveGroups(search);
  193. if (data != null && data.Length > 0)
  194. {
  195. foreach (GroupData d in data)
  196. {
  197. // Don't list group proxies
  198. if (d.Data.ContainsKey("Location") && d.Data["Location"] != string.Empty)
  199. continue;
  200. int nmembers = m_Database.MemberCount(d.GroupID);
  201. if(nmembers == 0)
  202. continue;
  203. DirGroupsReplyData g = new DirGroupsReplyData();
  204. if (d.Data.ContainsKey("Name"))
  205. g.groupName = d.Data["Name"];
  206. else
  207. {
  208. m_log.DebugFormat("[Groups]: Key Name not found");
  209. continue;
  210. }
  211. g.groupID = d.GroupID;
  212. g.members = nmembers;
  213. groups.Add(g);
  214. }
  215. }
  216. return groups;
  217. }
  218. public List<ExtendedGroupMembersData> GetGroupMembers(string RequestingAgentID, UUID GroupID)
  219. {
  220. List<ExtendedGroupMembersData> members = new List<ExtendedGroupMembersData>();
  221. GroupData group = m_Database.RetrieveGroup(GroupID);
  222. if (group == null)
  223. return members;
  224. // Unfortunately this doesn't quite work on legacy group data because of a bug
  225. // that's also being fixed here on CreateGroup. The OwnerRoleID sent to the DB was wrong.
  226. // See how to find the ownerRoleID a few lines below.
  227. UUID ownerRoleID = new UUID(group.Data["OwnerRoleID"]);
  228. RoleData[] roles = m_Database.RetrieveRoles(GroupID);
  229. if (roles == null)
  230. // something wrong with this group
  231. return members;
  232. List<RoleData> rolesList = new List<RoleData>(roles);
  233. // Let's find the "real" ownerRoleID
  234. RoleData ownerRole = rolesList.Find(r => r.Data["Powers"] == ((long)OwnerPowers).ToString());
  235. if (ownerRole != null)
  236. ownerRoleID = ownerRole.RoleID;
  237. // Check visibility?
  238. // When we don't want to check visibility, we pass it "all" as the requestingAgentID
  239. bool checkVisibility = !RequestingAgentID.Equals(UUID.Zero.ToString());
  240. if (checkVisibility)
  241. {
  242. // Is the requester a member of the group?
  243. bool isInGroup = false;
  244. if (m_Database.RetrieveMember(GroupID, RequestingAgentID) != null)
  245. isInGroup = true;
  246. if (!isInGroup) // reduce the roles to the visible ones
  247. rolesList = rolesList.FindAll(r => (UInt64.Parse(r.Data["Powers"]) & (ulong)GroupPowers.MemberVisible) != 0);
  248. }
  249. MembershipData[] datas = m_Database.RetrieveMembers(GroupID);
  250. if (datas == null || (datas != null && datas.Length == 0))
  251. return members;
  252. // OK, we have everything we need
  253. foreach (MembershipData d in datas)
  254. {
  255. RoleMembershipData[] rolememberships = m_Database.RetrieveMemberRoles(GroupID, d.PrincipalID);
  256. List<RoleMembershipData> rolemembershipsList = new List<RoleMembershipData>(rolememberships);
  257. ExtendedGroupMembersData m = new ExtendedGroupMembersData();
  258. // What's this person's current role in the group?
  259. UUID selectedRole = new UUID(d.Data["SelectedRoleID"]);
  260. RoleData selected = rolesList.Find(r => r.RoleID == selectedRole);
  261. if (selected != null)
  262. {
  263. m.Title = selected.Data["Title"];
  264. m.AgentPowers = UInt64.Parse(selected.Data["Powers"]);
  265. }
  266. m.AgentID = d.PrincipalID;
  267. m.AcceptNotices = d.Data["AcceptNotices"] == "1" ? true : false;
  268. m.Contribution = Int32.Parse(d.Data["Contribution"]);
  269. m.ListInProfile = d.Data["ListInProfile"] == "1" ? true : false;
  270. GridUserData gud = m_GridUserService.Get(d.PrincipalID);
  271. if (gud != null)
  272. {
  273. if (bool.Parse(gud.Data["Online"]))
  274. {
  275. m.OnlineStatus = @"Online";
  276. }
  277. else
  278. {
  279. int unixtime = int.Parse(gud.Data["Login"]);
  280. // The viewer is very picky about how these strings are formed. Eg. it will crash on malformed dates!
  281. m.OnlineStatus = (unixtime == 0) ? @"unknown" : Util.ToDateTime(unixtime).ToString("MM/dd/yyyy");
  282. }
  283. }
  284. // Is this person an owner of the group?
  285. m.IsOwner = (rolemembershipsList.Find(r => r.RoleID == ownerRoleID) != null) ? true : false;
  286. members.Add(m);
  287. }
  288. return members;
  289. }
  290. public bool AddGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers, out string reason)
  291. {
  292. reason = string.Empty;
  293. // check that the requesting agent has permissions to add role
  294. if (!HasPower(RequestingAgentID, groupID, GroupPowers.CreateRole))
  295. {
  296. m_log.DebugFormat("[Groups]: ({0}) Attempt at creating role in group {1} denied because of lack of permission", RequestingAgentID, groupID);
  297. reason = "Insufficient permission to create role";
  298. return false;
  299. }
  300. return _AddOrUpdateGroupRole(RequestingAgentID, groupID, roleID, name, description, title, powers, true);
  301. }
  302. public bool UpdateGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers)
  303. {
  304. // check perms
  305. if (!HasPower(RequestingAgentID, groupID, GroupPowers.ChangeActions))
  306. {
  307. m_log.DebugFormat("[Groups]: ({0}) Attempt at changing role in group {1} denied because of lack of permission", RequestingAgentID, groupID);
  308. return false;
  309. }
  310. return _AddOrUpdateGroupRole(RequestingAgentID, groupID, roleID, name, description, title, powers, false);
  311. }
  312. public void RemoveGroupRole(string RequestingAgentID, UUID groupID, UUID roleID)
  313. {
  314. // check perms
  315. if (!HasPower(RequestingAgentID, groupID, GroupPowers.DeleteRole))
  316. {
  317. m_log.DebugFormat("[Groups]: ({0}) Attempt at deleting role from group {1} denied because of lack of permission", RequestingAgentID, groupID);
  318. return;
  319. }
  320. // Can't delete Everyone and Owners roles
  321. if (roleID.IsZero())
  322. {
  323. m_log.DebugFormat("[Groups]: Attempt at deleting Everyone role from group {0} denied", groupID);
  324. return;
  325. }
  326. GroupData group = m_Database.RetrieveGroup(groupID);
  327. if (group == null)
  328. {
  329. m_log.DebugFormat("[Groups]: Attempt at deleting role from non-existing group {0}", groupID);
  330. return;
  331. }
  332. if (roleID == new UUID(group.Data["OwnerRoleID"]))
  333. {
  334. m_log.DebugFormat("[Groups]: Attempt at deleting Owners role from group {0} denied", groupID);
  335. return;
  336. }
  337. _RemoveGroupRole(groupID, roleID);
  338. }
  339. public List<GroupRolesData> GetGroupRoles(string RequestingAgentID, UUID GroupID)
  340. {
  341. // TODO: check perms
  342. return _GetGroupRoles(GroupID);
  343. }
  344. public List<ExtendedGroupRoleMembersData> GetGroupRoleMembers(string RequestingAgentID, UUID GroupID)
  345. {
  346. // TODO: check perms
  347. // Is the requester a member of the group?
  348. bool isInGroup = false;
  349. if (m_Database.RetrieveMember(GroupID, RequestingAgentID) != null)
  350. isInGroup = true;
  351. return _GetGroupRoleMembers(GroupID, isInGroup);
  352. }
  353. public bool AddAgentToGroup(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID, string token, out string reason)
  354. {
  355. reason = string.Empty;
  356. _AddAgentToGroup(RequestingAgentID, AgentID, GroupID, RoleID, token);
  357. return true;
  358. }
  359. public bool RemoveAgentFromGroup(string RequestingAgentID, string AgentID, UUID GroupID)
  360. {
  361. // check perms
  362. if (RequestingAgentID != AgentID && !HasPower(RequestingAgentID, GroupID, GroupPowers.Eject))
  363. return false;
  364. _RemoveAgentFromGroup(RequestingAgentID, AgentID, GroupID);
  365. return true;
  366. }
  367. public bool AddAgentToGroupInvite(string RequestingAgentID, UUID inviteID, UUID groupID, UUID roleID, string agentID)
  368. {
  369. // Check whether the invitee is already a member of the group
  370. MembershipData m = m_Database.RetrieveMember(groupID, agentID);
  371. if (m != null)
  372. return false;
  373. // Check permission to invite
  374. if (!HasPower(RequestingAgentID, groupID, GroupPowers.Invite))
  375. {
  376. m_log.DebugFormat("[Groups]: ({0}) Attempt at inviting to group {1} denied because of lack of permission", RequestingAgentID, groupID);
  377. return false;
  378. }
  379. // Check whether there are pending invitations and delete them
  380. InvitationData invite = m_Database.RetrieveInvitation(groupID, agentID);
  381. if (invite != null)
  382. m_Database.DeleteInvite(invite.InviteID);
  383. invite = new InvitationData();
  384. invite.InviteID = inviteID;
  385. invite.PrincipalID = agentID;
  386. invite.GroupID = groupID;
  387. invite.RoleID = roleID;
  388. invite.Data = new Dictionary<string, string>();
  389. return m_Database.StoreInvitation(invite);
  390. }
  391. public GroupInviteInfo GetAgentToGroupInvite(string RequestingAgentID, UUID inviteID)
  392. {
  393. InvitationData data = m_Database.RetrieveInvitation(inviteID);
  394. if (data == null)
  395. return null;
  396. GroupInviteInfo inviteInfo = new GroupInviteInfo();
  397. inviteInfo.AgentID = data.PrincipalID;
  398. inviteInfo.GroupID = data.GroupID;
  399. inviteInfo.InviteID = data.InviteID;
  400. inviteInfo.RoleID = data.RoleID;
  401. return inviteInfo;
  402. }
  403. public void RemoveAgentToGroupInvite(string RequestingAgentID, UUID inviteID)
  404. {
  405. m_Database.DeleteInvite(inviteID);
  406. }
  407. public bool AddAgentToGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  408. {
  409. //if (!m_Database.CheckOwnerRole(RequestingAgentID, GroupID, RoleID))
  410. // return;
  411. // check permissions
  412. bool limited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMemberLimited);
  413. bool unlimited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMember) || IsOwner(RequestingAgentID, GroupID);
  414. if (!limited && !unlimited)
  415. {
  416. m_log.DebugFormat("[Groups]: ({0}) Attempt at assigning {1} to role {2} denied because of lack of permission", RequestingAgentID, AgentID, RoleID);
  417. return false;
  418. }
  419. // AssignMemberLimited means that the person can assign another person to the same roles that she has in the group
  420. if (!unlimited && limited)
  421. {
  422. // check whether person's has this role
  423. RoleMembershipData rolemembership = m_Database.RetrieveRoleMember(GroupID, RoleID, RequestingAgentID);
  424. if (rolemembership == null)
  425. {
  426. m_log.DebugFormat("[Groups]: ({0}) Attempt at assigning {1} to role {2} denied because of limited permission", RequestingAgentID, AgentID, RoleID);
  427. return false;
  428. }
  429. }
  430. _AddAgentToGroupRole(RequestingAgentID, AgentID, GroupID, RoleID);
  431. return true;
  432. }
  433. public bool RemoveAgentFromGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  434. {
  435. // Don't remove from Everyone role!
  436. if (RoleID.IsZero())
  437. return false;
  438. // check permissions
  439. bool limited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMemberLimited);
  440. bool unlimited = HasPower(RequestingAgentID, GroupID, GroupPowers.AssignMember) || IsOwner(RequestingAgentID, GroupID);
  441. if (!limited && !unlimited)
  442. {
  443. m_log.DebugFormat("[Groups]: ({0}) Attempt at removing {1} from role {2} denied because of lack of permission", RequestingAgentID, AgentID, RoleID);
  444. return false;
  445. }
  446. // AssignMemberLimited means that the person can assign another person to the same roles that she has in the group
  447. if (!unlimited && limited)
  448. {
  449. // check whether person's has this role
  450. RoleMembershipData rolemembership = m_Database.RetrieveRoleMember(GroupID, RoleID, RequestingAgentID);
  451. if (rolemembership == null)
  452. {
  453. m_log.DebugFormat("[Groups]: ({0}) Attempt at removing {1} from role {2} denied because of limited permission", RequestingAgentID, AgentID, RoleID);
  454. return false;
  455. }
  456. }
  457. RoleMembershipData rolemember = m_Database.RetrieveRoleMember(GroupID, RoleID, AgentID);
  458. if (rolemember == null)
  459. return false;
  460. m_Database.DeleteRoleMember(rolemember);
  461. // Find another role for this person
  462. UUID newRoleID = UUID.Zero; // Everyone
  463. RoleMembershipData[] rdata = m_Database.RetrieveMemberRoles(GroupID, AgentID);
  464. if (rdata != null)
  465. foreach (RoleMembershipData r in rdata)
  466. {
  467. if (!r.RoleID.IsZero())
  468. {
  469. newRoleID = r.RoleID;
  470. break;
  471. }
  472. }
  473. MembershipData member = m_Database.RetrieveMember(GroupID, AgentID);
  474. if (member != null)
  475. {
  476. member.Data["SelectedRoleID"] = newRoleID.ToString();
  477. m_Database.StoreMember(member);
  478. }
  479. return true;
  480. }
  481. public List<GroupRolesData> GetAgentGroupRoles(string RequestingAgentID, string AgentID, UUID GroupID)
  482. {
  483. List<GroupRolesData> roles = new List<GroupRolesData>();
  484. // TODO: check permissions
  485. RoleMembershipData[] data = m_Database.RetrieveMemberRoles(GroupID, AgentID);
  486. if (data == null || (data != null && data.Length ==0))
  487. return roles;
  488. foreach (RoleMembershipData d in data)
  489. {
  490. RoleData rdata = m_Database.RetrieveRole(GroupID, d.RoleID);
  491. if (rdata == null) // hippos
  492. continue;
  493. GroupRolesData r = new GroupRolesData();
  494. r.Name = rdata.Data["Name"];
  495. r.Powers = UInt64.Parse(rdata.Data["Powers"]);
  496. r.RoleID = rdata.RoleID;
  497. r.Title = rdata.Data["Title"];
  498. roles.Add(r);
  499. }
  500. return roles;
  501. }
  502. public ExtendedGroupMembershipData SetAgentActiveGroup(string RequestingAgentID, string AgentID, UUID GroupID)
  503. {
  504. // TODO: check perms
  505. PrincipalData principal = new PrincipalData();
  506. principal.PrincipalID = AgentID;
  507. principal.ActiveGroupID = GroupID;
  508. m_Database.StorePrincipal(principal);
  509. return GetAgentGroupMembership(RequestingAgentID, AgentID, GroupID);
  510. }
  511. public ExtendedGroupMembershipData GetAgentActiveMembership(string RequestingAgentID, string AgentID)
  512. {
  513. // 1. get the principal data for the active group
  514. PrincipalData principal = m_Database.RetrievePrincipal(AgentID);
  515. if (principal == null)
  516. return null;
  517. return GetAgentGroupMembership(RequestingAgentID, AgentID, principal.ActiveGroupID);
  518. }
  519. public ExtendedGroupMembershipData GetAgentGroupMembership(string RequestingAgentID, string AgentID, UUID GroupID)
  520. {
  521. return GetAgentGroupMembership(RequestingAgentID, AgentID, GroupID, null);
  522. }
  523. private ExtendedGroupMembershipData GetAgentGroupMembership(string RequestingAgentID, string AgentID, UUID GroupID, MembershipData membership)
  524. {
  525. // 2. get the active group
  526. GroupData group = m_Database.RetrieveGroup(GroupID);
  527. if (group == null)
  528. return null;
  529. // 3. get the membership info if we don't have it already
  530. if (membership == null)
  531. {
  532. membership = m_Database.RetrieveMember(group.GroupID, AgentID);
  533. if (membership == null)
  534. return null;
  535. }
  536. // 4. get the active role
  537. UUID activeRoleID = new UUID(membership.Data["SelectedRoleID"]);
  538. RoleData role = m_Database.RetrieveRole(group.GroupID, activeRoleID);
  539. ExtendedGroupMembershipData data = new ExtendedGroupMembershipData();
  540. data.AcceptNotices = membership.Data["AcceptNotices"] == "1" ? true : false;
  541. data.AccessToken = membership.Data["AccessToken"];
  542. data.Active = true;
  543. data.ActiveRole = activeRoleID;
  544. data.AllowPublish = group.Data["AllowPublish"] == "1" ? true : false;
  545. data.Charter = group.Data["Charter"];
  546. data.Contribution = Int32.Parse(membership.Data["Contribution"]);
  547. data.FounderID = new UUID(group.Data["FounderID"]);
  548. data.GroupID = new UUID(group.GroupID);
  549. data.GroupName = group.Data["Name"];
  550. data.GroupPicture = new UUID(group.Data["InsigniaID"]);
  551. if (role != null)
  552. {
  553. data.GroupPowers = UInt64.Parse(role.Data["Powers"]);
  554. data.GroupTitle = role.Data["Title"];
  555. }
  556. data.ListInProfile = membership.Data["ListInProfile"] == "1" ? true : false;
  557. data.MaturePublish = group.Data["MaturePublish"] == "1" ? true : false;
  558. data.MembershipFee = Int32.Parse(group.Data["MembershipFee"]);
  559. data.OpenEnrollment = group.Data["OpenEnrollment"] == "1" ? true : false;
  560. data.ShowInList = group.Data["ShowInList"] == "1" ? true : false;
  561. return data;
  562. }
  563. public List<GroupMembershipData> GetAgentGroupMemberships(string RequestingAgentID, string AgentID)
  564. {
  565. List<GroupMembershipData> memberships = new List<GroupMembershipData>();
  566. // 1. Get all the groups that this person is a member of
  567. MembershipData[] mdata = m_Database.RetrieveMemberships(AgentID);
  568. if (mdata == null || (mdata != null && mdata.Length == 0))
  569. return memberships;
  570. foreach (MembershipData d in mdata)
  571. {
  572. GroupMembershipData gmember = GetAgentGroupMembership(RequestingAgentID, AgentID, d.GroupID, d);
  573. if (gmember != null)
  574. {
  575. memberships.Add(gmember);
  576. //m_log.DebugFormat("[XXX]: Member of {0} as {1}", gmember.GroupName, gmember.GroupTitle);
  577. //Util.PrintCallStack();
  578. }
  579. }
  580. return memberships;
  581. }
  582. public void SetAgentActiveGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  583. {
  584. MembershipData data = m_Database.RetrieveMember(GroupID, AgentID);
  585. if (data == null)
  586. return;
  587. data.Data["SelectedRoleID"] = RoleID.ToString();
  588. m_Database.StoreMember(data);
  589. }
  590. public void UpdateMembership(string RequestingAgentID, string AgentID, UUID GroupID, bool AcceptNotices, bool ListInProfile)
  591. {
  592. // TODO: check perms
  593. MembershipData membership = m_Database.RetrieveMember(GroupID, AgentID);
  594. if (membership == null)
  595. return;
  596. membership.Data["AcceptNotices"] = AcceptNotices ? "1" : "0";
  597. membership.Data["ListInProfile"] = ListInProfile ? "1" : "0";
  598. m_Database.StoreMember(membership);
  599. }
  600. public bool AddGroupNotice(string RequestingAgentID, UUID groupID, UUID noticeID, string fromName, string subject, string message,
  601. bool hasAttachment, byte attType, string attName, UUID attItemID, string attOwnerID)
  602. {
  603. // Check perms
  604. if (!HasPower(RequestingAgentID, groupID, GroupPowers.SendNotices))
  605. {
  606. m_log.DebugFormat("[Groups]: ({0}) Attempt at sending notice to group {1} denied because of lack of permission", RequestingAgentID, groupID);
  607. return false;
  608. }
  609. return _AddNotice(groupID, noticeID, fromName, subject, message, hasAttachment, attType, attName, attItemID, attOwnerID);
  610. }
  611. public GroupNoticeInfo GetGroupNotice(string RequestingAgentID, UUID noticeID)
  612. {
  613. NoticeData data = m_Database.RetrieveNotice(noticeID);
  614. if (data == null)
  615. return null;
  616. return _NoticeDataToInfo(data);
  617. }
  618. public List<ExtendedGroupNoticeData> GetGroupNotices(string RequestingAgentID, UUID groupID)
  619. {
  620. NoticeData[] data = m_Database.RetrieveNotices(groupID);
  621. List<ExtendedGroupNoticeData> infos = new List<ExtendedGroupNoticeData>();
  622. if (data == null || (data != null && data.Length == 0))
  623. return infos;
  624. foreach (NoticeData d in data)
  625. {
  626. ExtendedGroupNoticeData info = _NoticeDataToData(d);
  627. infos.Add(info);
  628. }
  629. return infos;
  630. }
  631. public void ResetAgentGroupChatSessions(string agentID)
  632. {
  633. }
  634. public bool hasAgentBeenInvitedToGroupChatSession(string agentID, UUID groupID)
  635. {
  636. return false;
  637. }
  638. public bool hasAgentDroppedGroupChatSession(string agentID, UUID groupID)
  639. {
  640. return false;
  641. }
  642. public void AgentDroppedFromGroupChatSession(string agentID, UUID groupID)
  643. {
  644. }
  645. public void AgentInvitedToGroupChatSession(string agentID, UUID groupID)
  646. {
  647. }
  648. #region Actions without permission checks
  649. protected void _AddAgentToGroup(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  650. {
  651. _AddAgentToGroup(RequestingAgentID, AgentID, GroupID, RoleID, string.Empty);
  652. }
  653. protected void _RemoveAgentFromGroup(string RequestingAgentID, string AgentID, UUID GroupID)
  654. {
  655. // 1. Delete membership
  656. m_Database.DeleteMember(GroupID, AgentID);
  657. // 2. Remove from rolememberships
  658. m_Database.DeleteMemberAllRoles(GroupID, AgentID);
  659. // 3. if it was active group, inactivate it
  660. PrincipalData principal = m_Database.RetrievePrincipal(AgentID);
  661. if (principal != null && principal.ActiveGroupID == GroupID)
  662. {
  663. principal.ActiveGroupID = UUID.Zero;
  664. m_Database.StorePrincipal(principal);
  665. }
  666. }
  667. protected void _AddAgentToGroup(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID, string accessToken)
  668. {
  669. // Check if it's already there
  670. MembershipData data = m_Database.RetrieveMember(GroupID, AgentID);
  671. if (data != null)
  672. return;
  673. // Add the membership
  674. data = new MembershipData();
  675. data.PrincipalID = AgentID;
  676. data.GroupID = GroupID;
  677. data.Data = new Dictionary<string, string>();
  678. data.Data["SelectedRoleID"] = RoleID.ToString();
  679. data.Data["Contribution"] = "0";
  680. data.Data["ListInProfile"] = "1";
  681. data.Data["AcceptNotices"] = "1";
  682. data.Data["AccessToken"] = accessToken;
  683. m_Database.StoreMember(data);
  684. // Add principal to everyone role
  685. _AddAgentToGroupRole(RequestingAgentID, AgentID, GroupID, UUID.Zero);
  686. // Add principal to role, if different from everyone role
  687. if (!RoleID.IsZero())
  688. _AddAgentToGroupRole(RequestingAgentID, AgentID, GroupID, RoleID);
  689. // Make this the active group
  690. PrincipalData pdata = new PrincipalData();
  691. pdata.PrincipalID = AgentID;
  692. pdata.ActiveGroupID = GroupID;
  693. m_Database.StorePrincipal(pdata);
  694. }
  695. protected bool _AddOrUpdateGroupRole(string RequestingAgentID, UUID groupID, UUID roleID, string name, string description, string title, ulong powers, bool add)
  696. {
  697. RoleData data = m_Database.RetrieveRole(groupID, roleID);
  698. if (add && data != null) // it already exists, can't create
  699. {
  700. m_log.DebugFormat("[Groups]: Group {0} already exists. Can't create it again", groupID);
  701. return false;
  702. }
  703. if (!add && data == null) // it doesn't exist, can't update
  704. {
  705. m_log.DebugFormat("[Groups]: Group {0} doesn't exist. Can't update it", groupID);
  706. return false;
  707. }
  708. if (add)
  709. data = new RoleData();
  710. data.GroupID = groupID;
  711. data.RoleID = roleID;
  712. data.Data = new Dictionary<string, string>();
  713. data.Data["Name"] = name;
  714. data.Data["Description"] = description;
  715. data.Data["Title"] = title;
  716. data.Data["Powers"] = powers.ToString();
  717. return m_Database.StoreRole(data);
  718. }
  719. protected void _RemoveGroupRole(UUID groupID, UUID roleID)
  720. {
  721. m_Database.DeleteRole(groupID, roleID);
  722. }
  723. protected void _AddAgentToGroupRole(string RequestingAgentID, string AgentID, UUID GroupID, UUID RoleID)
  724. {
  725. RoleMembershipData data = m_Database.RetrieveRoleMember(GroupID, RoleID, AgentID);
  726. if (data != null)
  727. return;
  728. data = new RoleMembershipData();
  729. data.GroupID = GroupID;
  730. data.PrincipalID = AgentID;
  731. data.RoleID = RoleID;
  732. m_Database.StoreRoleMember(data);
  733. // Make it the SelectedRoleID
  734. MembershipData membership = m_Database.RetrieveMember(GroupID, AgentID);
  735. if (membership == null)
  736. {
  737. m_log.DebugFormat("[Groups]: ({0}) No such member {0} in group {1}", AgentID, GroupID);
  738. return;
  739. }
  740. membership.Data["SelectedRoleID"] = RoleID.ToString();
  741. m_Database.StoreMember(membership);
  742. }
  743. protected List<GroupRolesData> _GetGroupRoles(UUID groupID)
  744. {
  745. List<GroupRolesData> roles = new List<GroupRolesData>();
  746. RoleData[] data = m_Database.RetrieveRoles(groupID);
  747. if (data == null || (data != null && data.Length == 0))
  748. return roles;
  749. foreach (RoleData d in data)
  750. {
  751. GroupRolesData r = new GroupRolesData();
  752. r.Description = d.Data["Description"];
  753. r.Members = m_Database.RoleMemberCount(groupID, d.RoleID);
  754. r.Name = d.Data["Name"];
  755. r.Powers = UInt64.Parse(d.Data["Powers"]);
  756. r.RoleID = d.RoleID;
  757. r.Title = d.Data["Title"];
  758. roles.Add(r);
  759. }
  760. return roles;
  761. }
  762. protected List<ExtendedGroupRoleMembersData> _GetGroupRoleMembers(UUID GroupID, bool isInGroup)
  763. {
  764. List<ExtendedGroupRoleMembersData> rmembers = new List<ExtendedGroupRoleMembersData>();
  765. RoleData[] rdata = new RoleData[0];
  766. if (!isInGroup)
  767. {
  768. rdata = m_Database.RetrieveRoles(GroupID);
  769. if (rdata == null || (rdata != null && rdata.Length == 0))
  770. return rmembers;
  771. }
  772. List<RoleData> rlist = new List<RoleData>(rdata);
  773. if (!isInGroup)
  774. rlist = rlist.FindAll(r => (UInt64.Parse(r.Data["Powers"]) & (ulong)GroupPowers.MemberVisible) != 0);
  775. RoleMembershipData[] data = m_Database.RetrieveRolesMembers(GroupID);
  776. if (data == null || (data != null && data.Length == 0))
  777. return rmembers;
  778. foreach (RoleMembershipData d in data)
  779. {
  780. if (!isInGroup)
  781. {
  782. RoleData rd = rlist.Find(_r => _r.RoleID == d.RoleID); // visible role
  783. if (rd == null)
  784. continue;
  785. }
  786. ExtendedGroupRoleMembersData r = new ExtendedGroupRoleMembersData();
  787. r.MemberID = d.PrincipalID;
  788. r.RoleID = d.RoleID;
  789. rmembers.Add(r);
  790. }
  791. return rmembers;
  792. }
  793. protected bool _AddNotice(UUID groupID, UUID noticeID, string fromName, string subject, string message,
  794. bool hasAttachment, byte attType, string attName, UUID attItemID, string attOwnerID)
  795. {
  796. NoticeData data = new NoticeData();
  797. data.GroupID = groupID;
  798. data.NoticeID = noticeID;
  799. data.Data = new Dictionary<string, string>();
  800. data.Data["FromName"] = fromName;
  801. data.Data["Subject"] = subject;
  802. data.Data["Message"] = message;
  803. data.Data["HasAttachment"] = hasAttachment ? "1" : "0";
  804. if (hasAttachment)
  805. {
  806. data.Data["AttachmentType"] = attType.ToString();
  807. data.Data["AttachmentName"] = attName;
  808. data.Data["AttachmentItemID"] = attItemID.ToString();
  809. data.Data["AttachmentOwnerID"] = attOwnerID;
  810. }
  811. data.Data["TMStamp"] = ((uint)Util.UnixTimeSinceEpoch()).ToString();
  812. return m_Database.StoreNotice(data);
  813. }
  814. #endregion
  815. #region structure translations
  816. ExtendedGroupRecord _GroupDataToRecord(GroupData data)
  817. {
  818. if (data == null)
  819. return null;
  820. ExtendedGroupRecord rec = new ExtendedGroupRecord();
  821. rec.AllowPublish = data.Data["AllowPublish"] == "1" ? true : false;
  822. rec.Charter = data.Data["Charter"];
  823. rec.FounderID = new UUID(data.Data["FounderID"]);
  824. rec.GroupID = data.GroupID;
  825. rec.GroupName = data.Data["Name"];
  826. rec.GroupPicture = new UUID(data.Data["InsigniaID"]);
  827. rec.MaturePublish = data.Data["MaturePublish"] == "1" ? true : false;
  828. rec.MembershipFee = Int32.Parse(data.Data["MembershipFee"]);
  829. rec.OpenEnrollment = data.Data["OpenEnrollment"] == "1" ? true : false;
  830. rec.OwnerRoleID = new UUID(data.Data["OwnerRoleID"]);
  831. rec.ShowInList = data.Data["ShowInList"] == "1" ? true : false;
  832. rec.ServiceLocation = data.Data["Location"];
  833. rec.MemberCount = m_Database.MemberCount(data.GroupID);
  834. rec.RoleCount = m_Database.RoleCount(data.GroupID);
  835. return rec;
  836. }
  837. GroupNoticeInfo _NoticeDataToInfo(NoticeData data)
  838. {
  839. GroupNoticeInfo notice = new GroupNoticeInfo();
  840. notice.GroupID = data.GroupID;
  841. notice.Message = data.Data["Message"];
  842. notice.noticeData = _NoticeDataToData(data);
  843. return notice;
  844. }
  845. ExtendedGroupNoticeData _NoticeDataToData(NoticeData data)
  846. {
  847. ExtendedGroupNoticeData notice = new ExtendedGroupNoticeData();
  848. notice.FromName = data.Data["FromName"];
  849. notice.NoticeID = data.NoticeID;
  850. notice.Subject = data.Data["Subject"];
  851. notice.Timestamp = uint.Parse((string)data.Data["TMStamp"]);
  852. notice.HasAttachment = data.Data["HasAttachment"] == "1" ? true : false;
  853. if (notice.HasAttachment)
  854. {
  855. notice.AttachmentName = data.Data["AttachmentName"];
  856. notice.AttachmentItemID = new UUID(data.Data["AttachmentItemID"].ToString());
  857. notice.AttachmentType = byte.Parse(data.Data["AttachmentType"].ToString());
  858. notice.AttachmentOwnerID = data.Data["AttachmentOwnerID"].ToString();
  859. }
  860. return notice;
  861. }
  862. #endregion
  863. #region permissions
  864. private bool HasPower(string agentID, UUID groupID, GroupPowers power)
  865. {
  866. RoleMembershipData[] rmembership = m_Database.RetrieveMemberRoles(groupID, agentID);
  867. if (rmembership is null || rmembership.Length == 0)
  868. return false;
  869. foreach (RoleMembershipData rdata in rmembership)
  870. {
  871. if(rdata is null)
  872. {
  873. m_log.Warn($"[GROUPSERVICE] null membership data entry in group {groupID} for agent {agentID}");
  874. continue;
  875. }
  876. RoleData role = m_Database.RetrieveRole(groupID, rdata.RoleID);
  877. if (role is null)
  878. {
  879. m_log.Warn($"[GROUPSERVICE] role with id {rdata.RoleID} is null");
  880. continue;
  881. }
  882. if ((UInt64.Parse(role.Data["Powers"]) & (ulong)power) != 0)
  883. return true;
  884. }
  885. return false;
  886. }
  887. private bool IsOwner(string agentID, UUID groupID)
  888. {
  889. GroupData group = m_Database.RetrieveGroup(groupID);
  890. if (group is null)
  891. return false;
  892. if(!UUID.TryParse(group.Data["OwnerRoleID"], out UUID ownerRoleID))
  893. return false;
  894. RoleMembershipData rmembership = m_Database.RetrieveRoleMember(groupID, ownerRoleID, agentID);
  895. return rmembership is not null;
  896. }
  897. #endregion
  898. }
  899. }