GroupsService.cs 42 KB

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