GroupsService.cs 43 KB

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